yarn add --dev @automock/jest @automock/adapters.nestjs
// movie.service.spec.ts
describe('MovieService', () => {
let service: MovieService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MovieService],
}).compile();
service = module.get<MovieService>(MovieService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
// movie.service.spec.ts
import { TestBed } from '@automock/jest';
describe('MovieService', () => {
let movieService: MovieService;
let movieRepository: jest.Mocked<Repository<Movie>>;
let movieDetailRepository: jest.Mocked<Repository<MovieDetail>>;
let directorRepository: jest.Mocked<Repository<Director>>;
let genreRepository: jest.Mocked<Repository<Genre>>;
let userRepository: jest.Mocked<Repository<User>>;
let movieUserLikeRepository: jest.Mocked<Repository<MovieUserLike>>;
let dataSource: jest.Mocked<DataSource>;
let commonService: jest.Mocked<CommonService>;
let cacheManager: Cache;
beforeEach(async () => {
const { unit, unitRef } = TestBed.create(MovieService).compile();
movieService = unit;
movieRepository = unitRef.get(getRepositoryToken(Movie) as string);
movieDetailRepository = unitRef.get(
getRepositoryToken(MovieDetail) as string,
);
directorRepository = unitRef.get(getRepositoryToken(Director) as string);
genreRepository = unitRef.get(getRepositoryToken(Genre) as string);
userRepository = unitRef.get(getRepositoryToken(User) as string);
movieUserLikeRepository = unitRef.get(
getRepositoryToken(MovieUserLike) as string,
);
dataSource = unitRef.get(DataSource);
commonService = unitRef.get(CommonService);
cacheManager = unitRef.get(CACHE_MANAGER);
});
it('should be defined', () => {
expect(movieService).toBeDefined();
});
});
/* istanbul ignore next */
describe('findAll', () => {
let getMoviesMock: jest.SpyInstance;
let getLikedMovieMock: jest.SpyInstance;
beforeEach(() => {
getMoviesMock = jest.spyOn(movieService, 'getMovies');
getLikedMovieMock = jest.spyOn(movieService, 'getLikedMovies');
});
it('should return a list of movies withoud user likes', async () => {
const movies = [
{
id: 1,
title: 'Movie 1',
},
];
const dto = { title: 'Movie' } as GetMoviesDto;
const qb: any = {
where: jest.fn().mockReturnThis(),
getManyAndCount: jest.fn().mockResolvedValue([movies, 1]),
};
getMoviesMock.mockResolvedValue(qb);
jest
.spyOn(commonService, 'applyCursorPaginationParamsToQb')
.mockResolvedValue({ nextCursor: null } as any);
const result = await movieService.findAll(dto);
expect(getMoviesMock).toHaveBeenCalled();
expect(qb.where).toHaveBeenCalledWith('movie.title LIKE :title', {
title: '%Movie%',
});
expect(
commonService.applyCursorPaginationParamsToQb,
).toHaveBeenCalledWith(qb, dto);
expect(qb.getManyAndCount).toHaveBeenCalled();
expect(result).toEqual({
data: movies,
nextCursor: null,
count: 1,
});
});
it('should return a list of movies with user likes', async () => {
const movies = [
{
id: 1,
title: 'Movie 1',
},
{
id: 3,
title: 'Movie 3',
},
];
const likedMovies = [
{
movie: { id: 1 },
isLike: true,
},
{ movie: { id: 2 }, isLike: false },
];
const dto = { title: 'Movie' } as GetMoviesDto;
const qb: any = {
where: jest.fn().mockReturnThis(),
getManyAndCount: jest.fn().mockResolvedValue([movies, 1]),
};
getMoviesMock.mockResolvedValue(qb);
jest
.spyOn(commonService, 'applyCursorPaginationParamsToQb')
.mockReturnValue({
nextCursor: null,
} as any);
getLikedMovieMock.mockResolvedValue(likedMovies);
const userId = 1;
const result = await movieService.findAll(dto, userId);
expect(getMoviesMock).toHaveBeenCalled();
expect(qb.where).toHaveBeenCalledWith('movie.title LIKE :title', {
title: '%Movie%',
});
expect(
commonService.applyCursorPaginationParamsToQb,
).toHaveBeenCalledWith(qb, dto);
expect(qb.getManyAndCount).toHaveBeenCalled();
expect(getLikedMovieMock).toHaveBeenCalledWith(
movies.map((movie) => movie.id),
userId,
);
expect(result).toEqual({
data: [
{
id: 1,
title: 'Movie 1',
likeStatus: true,
},
{
id: 3,
title: 'Movie 3',
likeStatus: null,
},
],
nextCursor: null,
count: 1,
});
});
it('should return movies withoud title filter', async () => {
const movies = [{ id: 1, title: 'Movie 1' }];
const dto = {} as GetMoviesDto;
const qb: any = {
getManyAndCount: jest.fn().mockResolvedValue([movies, 1]),
};
getMoviesMock.mockResolvedValue(qb);
jest
.spyOn(commonService, 'applyCursorPaginationParamsToQb')
.mockResolvedValue({
nextCursor: null,
} as any);
const result = await movieService.findAll(dto);
expect(getMoviesMock).toHaveBeenCalled();
expect(qb.getManyAndCount).toHaveBeenCalledWith();
expect(result).toEqual({
data: movies,
nextCursor: null,
count: 1,
});
});
});
728x90
'코딩 > Nest.js' 카테고리의 다른 글
Testing (작성 중) (3) | 2024.11.03 |
---|---|
Swagger 2 (0) | 2024.10.27 |
Versioning (0) | 2024.10.27 |
Logging (2) | 2024.10.26 |
Task Scheduling (1) | 2024.10.26 |