mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-20 11:41:04 +10:00
write tests for UserService
This commit is contained in:
@ -9,7 +9,8 @@ import {
|
|||||||
UseGuards,
|
UseGuards,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Req, UnauthorizedException,
|
Req,
|
||||||
|
UnauthorizedException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
import { CreateUserDto } from './dto/create-user.dto';
|
import { CreateUserDto } from './dto/create-user.dto';
|
||||||
|
|||||||
@ -1,18 +1,77 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
|
import { UserRepository } from './repositories/user.repository';
|
||||||
|
import { User } from './entities/user.entity';
|
||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
|
import { CreateUserDto } from './dto/create-user.dto';
|
||||||
|
|
||||||
describe('UserService', () => {
|
describe('UserService', () => {
|
||||||
let service: UserService;
|
let userService: UserService;
|
||||||
|
let userRepository: any;
|
||||||
|
|
||||||
|
const mockUserRepository = () => ({
|
||||||
|
findByEmail: jest.fn(),
|
||||||
|
save: jest.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [UserService],
|
providers: [
|
||||||
|
UserService,
|
||||||
|
{
|
||||||
|
provide: UserRepository,
|
||||||
|
useFactory: mockUserRepository,
|
||||||
|
},
|
||||||
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
service = module.get<UserService>(UserService);
|
userService = module.get<UserService>(UserService);
|
||||||
|
userRepository = module.get<UserRepository>(UserRepository);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(service).toBeDefined();
|
expect(userService).toBeDefined();
|
||||||
|
expect(userRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('create', () => {
|
||||||
|
const createUserDto: CreateUserDto = {
|
||||||
|
name: 'John Doe',
|
||||||
|
email: 'test@test.com',
|
||||||
|
password: 'password',
|
||||||
|
};
|
||||||
|
|
||||||
|
it('should throw an error if a user with this email already exists', async () => {
|
||||||
|
userRepository.findByEmail.mockResolvedValue(new User());
|
||||||
|
await expect(userService.create(createUserDto)).rejects.toThrow(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create the user if it does not already exist', async () => {
|
||||||
|
const savedUser = {
|
||||||
|
...createUserDto,
|
||||||
|
id: expect.any(String),
|
||||||
|
createdAt: expect.any(Date),
|
||||||
|
updatedAt: expect.any(Date),
|
||||||
|
lastLoginAt: expect.any(Date),
|
||||||
|
locale: 'en',
|
||||||
|
emailVerifiedAt: null,
|
||||||
|
avatar_url: null,
|
||||||
|
timezone: null,
|
||||||
|
settings: null,
|
||||||
|
lastLoginIp: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
userRepository.findByEmail.mockResolvedValue(undefined);
|
||||||
|
userRepository.save.mockResolvedValue(savedUser);
|
||||||
|
|
||||||
|
const result = await userService.create(createUserDto);
|
||||||
|
expect(result).toMatchObject(savedUser);
|
||||||
|
|
||||||
|
expect(userRepository.save).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining(createUserDto),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user