Skip to content

Commit

Permalink
test(api): ✅ Add unit test for searching movies
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasberggren committed Apr 23, 2024
1 parent 07f7698 commit 64f86af
Showing 1 changed file with 55 additions and 5 deletions.
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] }])
})
})

0 comments on commit 64f86af

Please sign in to comment.