generated from plutack/nextjsauth-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-request.test.ts
54 lines (44 loc) · 2 KB
/
validate-request.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { validateWithSchema } from '@/utils/validate-request';
import { z } from 'zod';
import ErrorWithStatus from '@/exception/custom-error';
describe('validateWithSchema', () => {
// Define a sample schema for testing
const testSchema = z.object({
name: z.string(),
age: z.number().int().positive(),
});
it('should validate and return data when input is valid', async () => {
const validInput = { name: 'John Doe', age: 30 };
const mockRequest = {
json: jest.fn().mockResolvedValue(validInput),
} as unknown as Request;
const result = await validateWithSchema(mockRequest, testSchema);
expect(result).toEqual(validInput);
expect(mockRequest.json).toHaveBeenCalled();
});
it('should throw ErrorWithStatus when input is invalid', async () => {
const invalidInput = { name: 'John Doe', age: 'thirty' };
const mockRequest = {
json: jest.fn().mockResolvedValue(invalidInput),
} as unknown as Request;
await expect(validateWithSchema(mockRequest, testSchema)).rejects.toThrow(ErrorWithStatus);
await expect(validateWithSchema(mockRequest, testSchema)).rejects.toThrow('Invalid fields');
expect(mockRequest.json).toHaveBeenCalled();
});
it('should throw ErrorWithStatus when required fields are missing', async () => {
const incompleteInput = { name: 'John Doe' };
const mockRequest = {
json: jest.fn().mockResolvedValue(incompleteInput),
} as unknown as Request;
await expect(validateWithSchema(mockRequest, testSchema)).rejects.toThrow(ErrorWithStatus);
await expect(validateWithSchema(mockRequest, testSchema)).rejects.toThrow('Invalid fields');
expect(mockRequest.json).toHaveBeenCalled();
});
it('should handle JSON parsing errors', async () => {
const mockRequest = {
json: jest.fn().mockRejectedValue(new Error('Invalid JSON')),
} as unknown as Request;
await expect(validateWithSchema(mockRequest, testSchema)).rejects.toThrow('Invalid JSON');
expect(mockRequest.json).toHaveBeenCalled();
});
});