-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api): ✅ Add unit test for searching movies
- Loading branch information
1 parent
07f7698
commit 64f86af
Showing
1 changed file
with
55 additions
and
5 deletions.
There are no files selected for viewing
60 changes: 55 additions & 5 deletions
60
apps/api/src/movies/movies-search-db/movies-search.db.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,71 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { createMock } from '@golevelup/ts-jest' | ||
|
||
import { movieFactory } from '../../database/factories/movie' | ||
import { PrismaService } from '../../database/prisma.service' | ||
import { movieTitleFactory } from '../../database/factories/movieTitle' | ||
|
||
import { MoviesSearchDbService } from './movies-search-db.service' | ||
|
||
describe('MoviesSearchDbService', () => { | ||
let service: MoviesSearchDbService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
let module: TestingModule | ||
let prismaService: PrismaService | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [MoviesSearchDbService, PrismaService] | ||
}).compile() | ||
}) | ||
.useMocker(createMock) | ||
.compile() | ||
|
||
service = module.get<MoviesSearchDbService>(MoviesSearchDbService) | ||
prismaService = module.get<PrismaService>(PrismaService) | ||
}) | ||
|
||
it.only('should call the database with the search title', async () => { | ||
const queryRawSpy = jest.spyOn(prismaService, '$queryRaw').mockResolvedValueOnce([]) | ||
|
||
const title = 'Any Title' | ||
await service.findByTitle(title) | ||
|
||
const queryRawCalls = queryRawSpy.mock.calls[0][0] | ||
|
||
const queryRawCallsString = (queryRawCalls as unknown as string[]).map(elem => elem.replace(/\s+/g, ' ')).join(' ') | ||
|
||
const expectedSqlQuery = ' SELECT m.*, t.* FROM "Movie" m ' + | ||
'JOIN "MovieTitle" t ON m.id = t."movieId" ' + | ||
'WHERE t.title % ORDER BY similarity(t.title, ) DESC LIMIT ;' | ||
expect(queryRawSpy).toHaveBeenCalledWith( | ||
[expect.any(String), expect.any(String), expect.any(String), expect.any(String)], | ||
title, title, 10) | ||
|
||
expect(queryRawCallsString).toBe(expectedSqlQuery) | ||
}) | ||
|
||
it('should handle empty results from the database', async () => { | ||
jest.spyOn(prismaService, '$queryRaw').mockResolvedValueOnce([]) | ||
|
||
const results = await service.findByTitle('Nonexistent Movie') | ||
|
||
expect(results).toEqual([]) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
it('should handle database errors by throwing', async () => { | ||
jest.spyOn(prismaService, '$queryRaw').mockRejectedValueOnce(new Error('Database error')) | ||
|
||
await expect(service.findByTitle('The Matrix')).rejects.toThrow('Database error') | ||
}) | ||
|
||
it('should post-process data correctly', async () => { | ||
const mockMovieData = movieFactory.build() | ||
const mockMovieTitleData = movieTitleFactory.build() | ||
|
||
jest.spyOn(prismaService, '$queryRaw').mockResolvedValueOnce([{ ...mockMovieData, ...mockMovieTitleData }]) | ||
|
||
const result = await service.findByTitle('Mock Title') | ||
|
||
expect(result).toEqual([{ ...mockMovieData, movieTitles: [mockMovieTitleData] }]) | ||
}) | ||
}) |