diff --git a/__tests__/data/tmdb/tmdbAdapter.spec.ts b/__tests__/data/tmdb/tmdbAdapter.spec.ts index 39e95d26..1f404129 100644 --- a/__tests__/data/tmdb/tmdbAdapter.spec.ts +++ b/__tests__/data/tmdb/tmdbAdapter.spec.ts @@ -34,4 +34,14 @@ describe('getMovie', () => { expect(movie).toEqual(expected) }) + + describe('when movie is not found', () => { + it('should return undefined', async () => { + tmdbMock.mockSearchMovie(undefined) + + const movie = await tmdbAdapter.getMovie('movie title') + + expect(movie).toBeUndefined() + }) + }) }) diff --git a/__tests__/support/tmdbMock.ts b/__tests__/support/tmdbMock.ts index 8fda67f2..81a563a0 100644 --- a/__tests__/support/tmdbMock.ts +++ b/__tests__/support/tmdbMock.ts @@ -7,11 +7,11 @@ export class TmdbMock { private mockFetch: MockFetch ) {} - mockSearchMovie (movie: Movie, id = 1234) { + mockSearchMovie (movie: Movie | undefined, id = 1234) { this.mockFetch .mockImplementationOnce(async () => new Response(JSON.stringify({ page: 1, - results: [ + results: movie ? [ { id: id, original_title: movie.title, @@ -28,9 +28,9 @@ export class TmdbMock { vote_average: 7.282, vote_count: 3953, }, - ], + ] : [], total_pages: 1, - total_results: 9, + total_results: movie ? 1 : 0, }))) } diff --git a/src/controllers/cacheController.ts b/src/controllers/cacheController.ts index e8143291..04c149cf 100644 --- a/src/controllers/cacheController.ts +++ b/src/controllers/cacheController.ts @@ -29,6 +29,8 @@ export default class CacheController { async fillMovieDetails (movies: Movie[]): Promise { await Promise.all(movies.map>(async movie => { const tmdbMovie = await this.tmdbAdapter.getMovie(movie.title) + if (!tmdbMovie) return + movie.merge(tmdbMovie) })) } diff --git a/src/data/tmdb/tmdbAdapter.ts b/src/data/tmdb/tmdbAdapter.ts index 63b94a32..bccd85c2 100644 --- a/src/data/tmdb/tmdbAdapter.ts +++ b/src/data/tmdb/tmdbAdapter.ts @@ -4,9 +4,11 @@ import MovieResponse from './dtos/movieResponse.js' import SearchResponse from './dtos/searchResponse.js' export default class TmdbAdapter { - async getMovie (name: string): Promise { + async getMovie (name: string): Promise { const search = await this.searchMovie(name) + if (search.results.length === 0) return undefined + return await this.movieDetails(search.results[0].id) }