mirror of
https://github.com/docmost/docmost.git
synced 2025-11-10 06:42:06 +10:00
write tests for UserService
This commit is contained in:
@ -9,7 +9,8 @@ import {
|
||||
UseGuards,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Req, UnauthorizedException,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
|
||||
@ -1,18 +1,77 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
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', () => {
|
||||
let service: UserService;
|
||||
let userService: UserService;
|
||||
let userRepository: any;
|
||||
|
||||
const mockUserRepository = () => ({
|
||||
findByEmail: jest.fn(),
|
||||
save: jest.fn(),
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [UserService],
|
||||
providers: [
|
||||
UserService,
|
||||
{
|
||||
provide: UserRepository,
|
||||
useFactory: mockUserRepository,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<UserService>(UserService);
|
||||
userService = module.get<UserService>(UserService);
|
||||
userRepository = module.get<UserRepository>(UserRepository);
|
||||
});
|
||||
|
||||
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