Skip to content

Commit

Permalink
Merge pull request #32 from JHWelch/handle-movie-not-found
Browse files Browse the repository at this point in the history
Handle movie not found
  • Loading branch information
JHWelch authored Aug 12, 2023
2 parents d39b16d + 0119c09 commit ca3d91a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
10 changes: 10 additions & 0 deletions __tests__/data/tmdb/tmdbAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
8 changes: 4 additions & 4 deletions __tests__/support/tmdbMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
})))
}

Expand Down
2 changes: 2 additions & 0 deletions src/controllers/cacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class CacheController {
async fillMovieDetails (movies: Movie[]): Promise<void> {
await Promise.all(movies.map<Promise<void>>(async movie => {
const tmdbMovie = await this.tmdbAdapter.getMovie(movie.title)
if (!tmdbMovie) return

movie.merge(tmdbMovie)
}))
}
Expand Down
4 changes: 3 additions & 1 deletion src/data/tmdb/tmdbAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Movie> {
async getMovie (name: string): Promise<Movie | undefined> {
const search = await this.searchMovie(name)

if (search.results.length === 0) return undefined

return await this.movieDetails(search.results[0].id)
}

Expand Down

0 comments on commit ca3d91a

Please sign in to comment.