Skip to content

Commit

Permalink
Add tests for User module (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Odraxs authored Nov 29, 2023
1 parent 5eb2de6 commit 07eb72f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/auth/test/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,16 +18,6 @@ describe('AuthService', () => {
let spyJwtService: JwtService;
let spyPrismaService: DeepMockProxy<PrismaService>;

const date = new Date();
const user = {
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21',
name: 'John',
email: '[email protected]',
passwordHash: 'hashedPassword',
createdAt: date,
updatedAt: date,
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
Expand Down Expand Up @@ -70,16 +61,16 @@ 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);
expect(spyPrismaService.user.create).toHaveBeenCalledWith({
data: {
name: signUpData.name,
email: signUpData.email,
passwordHash: user.passwordHash,
passwordHash: mockUser.passwordHash,
},
select: null,
});
Expand Down Expand Up @@ -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(
Expand All @@ -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 () => {
Expand Down
59 changes: 59 additions & 0 deletions src/user/test/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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<PrismaService>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
UserService,
{
provide: PrismaService,
useFactory: () => mockDeep<PrismaService>(),
},
],
}).compile();

service = module.get<UserService>(UserService);
spyPrismaService = module.get(
PrismaService,
) as DeepMockProxy<PrismaService>;
});

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);
});
});
});
11 changes: 11 additions & 0 deletions test/mockUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const date = new Date();
const mockUser = {
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c21',
name: 'John',
email: '[email protected]',
passwordHash: 'hashedPassword',
createdAt: date,
updatedAt: date,
};

export default mockUser;

0 comments on commit 07eb72f

Please sign in to comment.