Skip to content
Snippets Groups Projects
signup.test.ts 3.23 KiB
Newer Older
Yusuf Akgül's avatar
Yusuf Akgül committed
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
})