diff --git a/src/auth/test/auth.service.spec.ts b/src/auth/test/auth.service.spec.ts index 35e950b..5b0efcf 100644 --- a/src/auth/test/auth.service.spec.ts +++ b/src/auth/test/auth.service.spec.ts @@ -8,6 +8,7 @@ import config from '../../common/config'; import * as bcrypt from 'bcrypt'; import { Prisma } from '@prisma/client'; import { ConflictException, UnauthorizedException } from '@nestjs/common'; +import mockUser from '../../../test/mockUser'; describe('AuthService', () => { let service: AuthService; @@ -17,16 +18,6 @@ describe('AuthService', () => { let spyJwtService: JwtService; let spyPrismaService: DeepMockProxy; - const date = new Date(); - const user = { - id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21', - name: 'John', - email: 'john@doe.com', - passwordHash: 'hashedPassword', - createdAt: date, - updatedAt: date, - }; - beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [ @@ -70,8 +61,8 @@ describe('AuthService', () => { }; it('should create a new user', async () => { - jest.spyOn(bcrypt, 'hash').mockResolvedValue(user.passwordHash); - spyPrismaService.user.create.mockResolvedValue(user); + jest.spyOn(bcrypt, 'hash').mockResolvedValue(mockUser.passwordHash); + spyPrismaService.user.create.mockResolvedValue(mockUser); await service.signup(signUpData); expect(spyPrismaService.user.create).toHaveBeenCalledTimes(1); @@ -79,7 +70,7 @@ describe('AuthService', () => { data: { name: signUpData.name, email: signUpData.email, - passwordHash: user.passwordHash, + passwordHash: mockUser.passwordHash, }, select: null, }); @@ -110,14 +101,14 @@ describe('AuthService', () => { it('should allow an user to login', async () => { jest.spyOn(bcrypt, 'compareSync').mockResolvedValue(true); jest.spyOn(spyJwtService, 'signAsync').mockResolvedValue(testJWT); - spyPrismaService.user.findFirst.mockResolvedValue(user); + spyPrismaService.user.findFirst.mockResolvedValue(mockUser); expect(await service.login(loginInfo)).toStrictEqual(testJWT); expect(spyPrismaService.user.findFirst).toHaveBeenCalledTimes(1); }); it('should retrieve an unauthorized error', async () => { - spyPrismaService.user.findFirst.mockResolvedValue(user); + spyPrismaService.user.findFirst.mockResolvedValue(mockUser); jest.spyOn(bcrypt, 'compareSync').mockResolvedValue(false); await expect(service.login(loginInfo)).rejects.toThrow( @@ -128,13 +119,13 @@ describe('AuthService', () => { describe('validateUser', () => { const payload = { - id: user.id, - name: user.name, - email: user.email, + id: mockUser.id, + name: mockUser.name, + email: mockUser.email, }; it('should return a valid user', async () => { - spyPrismaService.user.findUnique.mockResolvedValue(user); - expect(await service.validateUser(payload)).toStrictEqual(user); + spyPrismaService.user.findUnique.mockResolvedValue(mockUser); + expect(await service.validateUser(payload)).toStrictEqual(mockUser); }); it('should throw an unauthorized error', async () => { diff --git a/src/user/test/user.service.spec.ts b/src/user/test/user.service.spec.ts new file mode 100644 index 0000000..41f00e9 --- /dev/null +++ b/src/user/test/user.service.spec.ts @@ -0,0 +1,59 @@ +import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; +import { UserService } from '../user.service'; +import { PrismaService } from '../../common/services/prisma.service'; +import { Test, TestingModule } from '@nestjs/testing'; +import { NotFoundException } from '@nestjs/common'; +import mockUser from '../../../test/mockUser'; + +describe('UserService', () => { + let service: UserService; + let spyPrismaService: DeepMockProxy; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + UserService, + { + provide: PrismaService, + useFactory: () => mockDeep(), + }, + ], + }).compile(); + + service = module.get(UserService); + spyPrismaService = module.get( + PrismaService, + ) as DeepMockProxy; + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('getUserById', () => { + it('should return the user with the requested id', async () => { + spyPrismaService.user.findUnique.mockResolvedValue(mockUser); + + expect(await service.getUserById(mockUser.id)).toStrictEqual(mockUser); + expect(spyPrismaService.user.findUnique).toHaveBeenCalledTimes(1); + expect(spyPrismaService.user.findUnique).toHaveBeenCalledWith({ + where: { id: mockUser.id }, + }); + }); + + it('should return the user from the database', async () => { + spyPrismaService.user.findUnique.mockResolvedValue(mockUser); + + expect(await service.getUserById(mockUser.id)).toStrictEqual(mockUser); + }); + + it('should throw a not found exception', async () => { + spyPrismaService.user.findUnique.mockResolvedValue(null); + + await expect(service.getUserById(mockUser.id)).rejects.toThrow( + NotFoundException, + ); + expect(spyPrismaService.user.findUnique).toHaveBeenCalledTimes(1); + }); + }); +}); diff --git a/test/mockUser.ts b/test/mockUser.ts new file mode 100644 index 0000000..44a8eb1 --- /dev/null +++ b/test/mockUser.ts @@ -0,0 +1,11 @@ +const date = new Date(); +const mockUser = { + id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21', + name: 'John', + email: 'john@doe.com', + passwordHash: 'hashedPassword', + createdAt: date, + updatedAt: date, +}; + +export default mockUser;