Newer
Older
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { POST } from '@/app/api/signup/route'
import { db } from '@/lib/db'
import { NextResponse } from 'next/server'
jest.mock('@/lib/db')
jest.mock('bcrypt')
jest.mock('normalize-diacritics')
describe('POST function', () => {
it('creates a new user and returns the usernameOrEmail', async () => {
const req = {
json: jest.fn().mockResolvedValue({ username: 'testuser', email: 'test@example.com', password: 'password' }),
}
const hashedPassword = 'hashedpassword'
const normalizedUsername = 'testuser'
const usernameCheck = 'testuser'
const emailCheck = 'test@example.com'
// Mock the behavior of the dependencies
// const hashMock = jest.spyOn(hash, 'hash');
// const normalizeMock = jest.spyOn(normalize, 'normalize');
// const createMock = jest.spyOn(db.user, 'create');
// hashMock.mockResolvedValue(hashedPassword);
// normalizeMock.mockResolvedValue(normalizedUsername);
// createMock.mockResolvedValue({ email: emailCheck });
// const response = await POST(req as any);
// expect(req.json).toHaveBeenCalled();
// expect(hashMock).toHaveBeenCalledWith('password', 12);
// expect(normalizeMock).toHaveBeenCalledWith('testuser');
// expect(createMock).toHaveBeenCalledWith({
// data: {
// name: 'testuser',
// username: 'testuser',
// email: 'test@example.com',
// password: 'hashedpassword',
// },
// });
// expect(response).toEqual(NextResponse.json({ usernameOrEmail: 'test@example.com' }));
})
it('returns a NextResponse with status 422 when email already exists', async () => {
const req = {
json: jest.fn().mockResolvedValue({ username: 'testuser', email: 'test@example.com', password: 'password' }),
}
const existingUser = { email: 'test@example.com' }
// Mock the behavior of the dependencies
const findUniqueMock = jest.spyOn(db.user, 'findUnique')
// findUniqueMock.mockResolvedValue(existingUser);
const response = await POST(req as any)
expect(req.json).toHaveBeenCalled()
expect(findUniqueMock).toHaveBeenCalledWith({ where: { email: 'test@example.com' } })
expect(response).toEqual(
new NextResponse(JSON.stringify({ error: 'email already exists' }), { status: 422 })
)
})
it('returns a NextResponse with status 500 when an error occurs', async () => {
const req = {
json: jest.fn().mockResolvedValue({ username: 'testuser', email: 'test@example.com', password: 'password' }),
}
// Mock the behavior of the dependencies
const findUniqueMock = jest.spyOn(db.user, 'findUnique')
findUniqueMock.mockRejectedValue(new Error('Some database error'))
const response = await POST(req as any)
expect(req.json).toHaveBeenCalled()
expect(findUniqueMock).toHaveBeenCalledWith({ where: { email: 'test@example.com' } })
expect(response).toEqual(
new NextResponse(JSON.stringify({ error: 'Some database error' }), { status: 500 })
)
})
// Add more test cases for different scenarios
})