UNPKG

tmdbrjs

Version:

A TypeScript wrapper for The Movie Database (TMDB) API

552 lines 22.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const __1 = require(".."); (0, vitest_1.describe)('Client', () => { let tmdb; (0, vitest_1.beforeAll)(() => { tmdb = new __1.Client({ apiKey: '123' }); }); (0, vitest_1.describe)('movies', () => { const mockMovie = { adult: false, backdropPath: '/path/to/backdrop.jpg', belongsToCollection: 'Collection Name', budget: 1000000, genres: [ { id: 28, name: 'Action' }, { id: 12, name: 'Adventure' }, ], homepage: 'https://example.com', id: 123, imdbId: 'tt1234567', originalLanguage: 'en', originalTitle: 'Original Movie Title', overview: 'Movie overview', popularity: 7.5, posterPath: '/path/to/poster.jpg', productionCompanies: [ { id: 1, logoPath: '/path/to/logo.png', name: 'Production Company', originCountry: 'US', }, ], productionCountries: [ { iso_3166_1: 'US', name: 'United States', }, ], releaseDate: '2022-01-01', revenue: 2000000, runtime: 120, spokenLanguages: [ { english_name: 'English', iso_639_1: 'en', name: 'English', }, ], status: 'Released', tagline: 'Movie tagline', title: 'Movie Title', video: false, voteAverage: 7.5, voteCount: 1000, }; const mockMovieCredits = { id: 123, cast: [ { adult: false, alsoKnownAs: [], biography: '', birthday: null, deathday: null, gender: 1, homepage: null, id: 1, imdbId: null, knownForDepartment: 'Acting', name: 'Actor Name', placeOfBirth: null, popularity: 0, profilePath: null, cast_id: 1, character: 'Character Name', creditId: 'credit1', order: 1, }, ], crew: [ { adult: false, alsoKnownAs: [], biography: '', birthday: null, deathday: null, gender: 1, homepage: null, id: 2, imdbId: null, knownForDepartment: 'Directing', name: 'Director Name', placeOfBirth: null, popularity: 0, profilePath: null, creditId: 'credit2', department: 'Directing', job: 'Director', }, ], }; const mockReviews = { reviews: { id: 123, page: 1, results: [ { id: 'review1', author: 'Reviewer Name', content: 'Review content', createdAt: '2022-01-01', url: 'https://example.com/review', }, ], totalPages: 1, totalResults: 1, }, }; const mockSimilarMovies = { similarMovies: { id: 123, page: 1, results: [mockMovie], totalPages: 1, totalResults: 1, }, }; const mockVideos = { videos: { id: 123, results: [ { id: 'video1', key: 'video_key', name: 'Video Name', site: 'YouTube', size: 1080, type: 'Trailer', }, ], }, }; const mockMovieImages = { images: { id: 123, backdrops: [ { aspectRatio: 1.777, filePath: '/path/to/backdrop.jpg', height: 1080, iso6391: null, voteAverage: 7.5, voteCount: 100, width: 1920, }, ], posters: [ { aspectRatio: 0.666, filePath: '/path/to/poster.jpg', height: 1500, iso6391: null, voteAverage: 7.5, voteCount: 100, width: 1000, }, ], }, }; const mockPopularMovies = { page: 1, results: [mockMovie], totalPages: 1, totalResults: 1, }; (0, vitest_1.it)('should return popular movies', async () => { const response = mockPopularMovies; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.movies.getPopular(); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)('should return a movie object', async () => { const movieId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockMovie); const result = await tmdb.movies.getById(movieId); (0, vitest_1.expect)(result).toEqual(mockMovie); }); (0, vitest_1.it)('should append response with credits', async () => { const movieId = '123'; const options = { include: ['credits'], }; const response = { ...mockMovie, credits: mockMovieCredits, }; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.movies.getById(movieId, options); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)('should append response with multiple options', async () => { const movieId = '123'; const options = { include: ['credits', 'reviews', 'similar', 'videos', 'images'], }; const response = { ...mockMovie, credits: mockMovieCredits, reviews: mockReviews, similar: mockSimilarMovies, videos: mockVideos, images: mockMovieImages, }; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.movies.getById(movieId, options); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)('should return movie credits', async () => { const movieId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockMovieCredits); const result = await tmdb.movies.getCredits(movieId); (0, vitest_1.expect)(result).toEqual(mockMovieCredits); }); (0, vitest_1.it)('should return similar movies', async () => { const movieId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockSimilarMovies); const result = await tmdb.movies.getSimilar(movieId); (0, vitest_1.expect)(result).toEqual(mockSimilarMovies); }); (0, vitest_1.it)('should throw an error if API call fails', async () => { const movieId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockRejectedValue(new Error('API error')); await (0, vitest_1.expect)(tmdb.movies.getById(movieId)).rejects.toThrow('API error'); }); (0, vitest_1.it)('should throw an error if no API key is provided', async () => { const client = new __1.Client({ apiKey: '' }); const movieId = '123'; await (0, vitest_1.expect)(client.movies.getById(movieId)).rejects.toThrow('No API key provided'); }); }); (0, vitest_1.describe)('people', () => { const mockPerson = { adult: false, alsoKnownAs: ['Johnny'], biography: 'A great actor', birthday: '1990-01-01', deathday: null, gender: 1, homepage: null, id: 123, imdbId: 'nm123456', knownForDepartment: 'Acting', name: 'John Doe', placeOfBirth: 'New York', popularity: 7.5, profilePath: '/path/to/profile.jpg', }; const mockMovieCredits = { id: 123, cast: [ { ...mockPerson, cast_id: 1, character: 'Lead Role', creditId: 'credit1', order: 1, knownForDepartment: 'Acting', }, ], crew: [ { ...mockPerson, creditId: 'credit2', department: 'Directing', job: 'Director', }, ], }; const mockTvCredits = { id: 123, cast: [ { id: 1, name: 'TV Show 1', originalName: 'Original TV Show 1', character: 'Lead Role', creditId: 'credit1', episodeCount: 10, }, ], crew: [ { id: 2, name: 'TV Show 2', originalName: 'Original TV Show 2', department: 'Directing', job: 'Director', creditId: 'credit2', episodeCount: 5, }, ], }; const mockCombinedCredits = { id: 123, cast: [ { id: 1, name: 'Movie 1', originalName: 'Original Movie 1', character: 'Lead Role', creditId: 'credit1', mediaType: 'movie', }, { id: 2, name: 'TV Show 1', originalName: 'Original TV Show 1', character: 'Lead Role', creditId: 'credit2', mediaType: 'tv', episodeCount: 10, }, ], crew: [ { id: 3, name: 'Movie 2', originalName: 'Original Movie 2', department: 'Directing', job: 'Director', creditId: 'credit3', mediaType: 'movie', }, ], }; const mockPersonImages = { id: 123, profiles: [ { aspectRatio: 0.666, filePath: '/path/to/image.jpg', height: 1000, iso6391: null, voteAverage: 7.5, voteCount: 100, width: 667, }, ], }; const mockPopularPeople = { page: 1, results: [mockPerson], totalPages: 1, totalResults: 1, }; (0, vitest_1.it)('should return popular people', async () => { const response = mockPopularPeople; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.people.getPopular(); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)('should return a person object', async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockPerson); const result = await tmdb.people.getById(personId); (0, vitest_1.expect)(result).toEqual(mockPerson); }); (0, vitest_1.it)('should append response with movie credits', async () => { const personId = '123'; const options = { include: ['movieCredits'], }; const response = { ...mockPerson, movieCredits: mockMovieCredits, }; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.people.getById(personId, options); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)('should append response with multiple options', async () => { const personId = '123'; const options = { include: ['movieCredits', 'tvCredits', 'images'], }; const response = { ...mockPerson, movieCredits: mockMovieCredits, tvCredits: mockTvCredits, images: mockPersonImages, }; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response); const result = await tmdb.people.getById(personId, options); (0, vitest_1.expect)(result).toEqual(response); }); (0, vitest_1.it)("should return a person's movie credits", async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockMovieCredits); const result = await tmdb.people.getMovieCredits(personId); (0, vitest_1.expect)(result).toEqual(mockMovieCredits); }); (0, vitest_1.it)("should return a person's TV credits", async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockTvCredits); const result = await tmdb.people.getTvCredits(personId); (0, vitest_1.expect)(result).toEqual(mockTvCredits); }); (0, vitest_1.it)("should return a person's combined credits", async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockCombinedCredits); const result = await tmdb.people.getCombinedCredits(personId); (0, vitest_1.expect)(result).toEqual(mockCombinedCredits); }); (0, vitest_1.it)("should return a person's images", async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(mockPersonImages); const result = await tmdb.people.getImages(personId); (0, vitest_1.expect)(result).toEqual(mockPersonImages); }); (0, vitest_1.it)('should throw an error if API call fails', async () => { const personId = '123'; vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockRejectedValue(new Error('API error')); await (0, vitest_1.expect)(tmdb.people.getById(personId)).rejects.toThrow('API error'); }); (0, vitest_1.it)('should throw an error if no API key is provided', async () => { const client = new __1.Client({ apiKey: '' }); const personId = '123'; await (0, vitest_1.expect)(client.people.getById(personId)).rejects.toThrow('No API key provided'); }); }); (0, vitest_1.describe)('Client configuration and error handling', () => { (0, vitest_1.it)('should use default version when not specified', () => { const client = new __1.Client({ apiKey: '123' }); (0, vitest_1.expect)(client.getVersion()).toBe('3'); }); (0, vitest_1.it)('should use custom version when specified', () => { const client = new __1.Client({ apiKey: '123', version: '4' }); (0, vitest_1.expect)(client.getVersion()).toBe('4'); }); (0, vitest_1.it)('should use default language when not specified', () => { const client = new __1.Client({ apiKey: '123' }); (0, vitest_1.expect)(client.getLanguage()).toBe('en-US'); }); (0, vitest_1.it)('should use custom language when specified', () => { const client = new __1.Client({ apiKey: '123', language: 'fr-FR' }); (0, vitest_1.expect)(client.getLanguage()).toBe('fr-FR'); }); (0, vitest_1.it)('should use default base URL when not specified', async () => { const client = new __1.Client({ apiKey: '123' }); // Mock fetch to check the URL being called const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}), }); global.fetch = mockFetch; try { await client.movies.getPopular(); } catch { // Ignore the error, we just want to check the URL } (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.objectContaining({ href: vitest_1.expect.stringContaining('https://api.themoviedb.org/3/'), }), vitest_1.expect.any(Object)); }); (0, vitest_1.it)('should use custom base URL when specified', async () => { const client = new __1.Client({ apiKey: '123', baseUrl: 'https://custom.api.com' }); // Mock fetch to check the URL being called const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}), }); global.fetch = mockFetch; try { await client.movies.getPopular(); } catch { // Ignore the error, we just want to check the URL } (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.objectContaining({ href: vitest_1.expect.stringContaining('https://custom.api.com/3/'), }), vitest_1.expect.any(Object)); }); (0, vitest_1.it)('should throw error for 404 status', async () => { const client = new __1.Client({ apiKey: '123' }); const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: false, status: 404, }); global.fetch = mockFetch; await (0, vitest_1.expect)(client.movies.getPopular()).rejects.toThrow('Resource not found'); }); (0, vitest_1.it)('should throw error for 429 status', async () => { const client = new __1.Client({ apiKey: '123' }); const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: false, status: 429, }); global.fetch = mockFetch; await (0, vitest_1.expect)(client.movies.getPopular()).rejects.toThrow('Too many requests'); }); (0, vitest_1.it)('should throw error for other HTTP errors', async () => { const client = new __1.Client({ apiKey: '123' }); const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: false, status: 500, }); global.fetch = mockFetch; await (0, vitest_1.expect)(client.movies.getPopular()).rejects.toThrow('HTTP error! status: 500'); }); (0, vitest_1.it)('should include authorization header with bearer token', async () => { const client = new __1.Client({ apiKey: 'test-api-key' }); const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}), }); global.fetch = mockFetch; try { await client.movies.getPopular(); } catch { // Ignore the error, we just want to check the headers } (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.any(Object), vitest_1.expect.objectContaining({ headers: vitest_1.expect.objectContaining({ Authorization: 'Bearer test-api-key', }), })); }); (0, vitest_1.it)('should include language parameter in requests', async () => { const client = new __1.Client({ apiKey: '123', language: 'es-ES' }); const mockFetch = vitest_1.vi.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({}), }); global.fetch = mockFetch; try { await client.movies.getPopular(); } catch { // Ignore the error, we just want to check the URL parameters } (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith(vitest_1.expect.objectContaining({ href: vitest_1.expect.stringContaining('language=es-ES'), }), vitest_1.expect.any(Object)); }); }); }); //# sourceMappingURL=index.spec.js.map