tmdbrjs
Version:
A TypeScript wrapper for The Movie Database (TMDB) API
209 lines • 8.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const __1 = require("../..");
(0, vitest_1.describe)('Movies', () => {
let tmdb;
(0, vitest_1.beforeAll)(() => {
tmdb = new __1.Client({ apiKey: '123' });
});
(0, vitest_1.describe)('getPopular', () => {
(0, vitest_1.it)('should return a list of popular movies', async () => {
const response = {
results: [
{
id: '1',
title: 'Movie 1',
},
{
id: '2',
title: 'Movie 2',
},
],
};
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.describe)('getTopRated', () => {
(0, vitest_1.it)('should return a list of top rated movies', async () => {
const response = {
results: [
{
id: '1',
title: 'Movie 1',
},
{
id: '2',
title: 'Movie 2',
},
],
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getTopRated();
(0, vitest_1.expect)(result).toEqual(response);
});
});
(0, vitest_1.describe)('getById', () => {
(0, vitest_1.it)('should return a movie object', async () => {
const movieId = '123';
const response = {
id: movieId,
title: 'Movie 1',
release_date: '2021-01-01',
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getById(movieId);
(0, vitest_1.expect)(result).toEqual(response);
});
(0, vitest_1.it)('should append response with options', async () => {
const movieId = '123';
const options = {
include: ['credits'],
};
const response = {
id: movieId,
title: 'Movie 1',
credits: {
cast: [
{
id: 1,
name: 'Actor 1',
},
{
id: 2,
name: 'Actor 2',
},
],
},
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getById(movieId, options);
(0, vitest_1.expect)(result).toEqual(response);
// Verify that the URL was constructed correctly with snake_case
(0, vitest_1.expect)(tmdb.apiClient.get).toHaveBeenCalledWith(vitest_1.expect.stringContaining('append_to_response=credits'));
});
(0, vitest_1.it)('should convert multiple camelCase append options to snake_case', async () => {
const movieId = '123';
const options = {
include: ['credits', 'videos', 'images'],
};
const response = {
id: movieId,
title: 'Movie 1',
credits: { cast: [] },
videos: { results: [] },
images: { backdrops: [], posters: [] },
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getById(movieId, options);
(0, vitest_1.expect)(result).toEqual(response);
// Verify that the URL was constructed correctly with snake_case
// URL encoding converts commas to %2C
(0, vitest_1.expect)(tmdb.apiClient.get).toHaveBeenCalledWith(vitest_1.expect.stringMatching(/append_to_response=credits(%2C|,)videos(%2C|,)images/));
});
(0, vitest_1.it)('should support new append options', async () => {
const movieId = '123';
const options = {
include: ['recommendations', 'keywords', 'externalIds'],
};
const response = {
id: movieId,
title: 'Movie 1',
recommendations: { results: [] },
keywords: { keywords: [] },
externalIds: { imdbId: 'tt123' },
};
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.expect)(tmdb.apiClient.get).toHaveBeenCalledWith(vitest_1.expect.stringMatching(/append_to_response=recommendations(%2C|,)keywords(%2C|,)external_ids/));
});
(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('Failed to fetch movie/{id}: API error');
});
});
(0, vitest_1.describe)('getSimilar', () => {
(0, vitest_1.it)('should return a list of similar movies', async () => {
const movieId = '123';
const response = {
results: [
{
id: '1',
title: 'Movie 1',
},
{
id: '2',
title: 'Movie 2',
},
],
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getSimilar(movieId);
(0, vitest_1.expect)(result).toEqual(response);
});
});
(0, vitest_1.describe)('getCredits', () => {
(0, vitest_1.it)('should return a list of movie credits', async () => {
const movieId = '123';
const response = {
cast: [
{
id: 1,
name: 'Actor 1',
},
{
id: 2,
name: 'Actor 2',
},
],
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getCredits(movieId);
(0, vitest_1.expect)(result).toEqual(response);
});
});
(0, vitest_1.describe)('getDetails', () => {
(0, vitest_1.it)('should return movie details', async () => {
const movieId = '123';
const response = {
id: 123,
title: 'Test Movie',
overview: 'A test movie',
releaseDate: '2023-01-01',
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getDetails(movieId);
(0, vitest_1.expect)(result).toEqual(response);
(0, vitest_1.expect)(tmdb.apiClient.get).toHaveBeenCalledWith(`movie/${movieId}`);
});
});
(0, vitest_1.describe)('getMovieCredits', () => {
(0, vitest_1.it)('should return movie credits', async () => {
const movieId = '123';
const response = {
id: 123,
cast: [
{
id: 1,
name: 'Actor 1',
},
],
crew: [
{
id: 2,
name: 'Director 1',
},
],
};
vitest_1.vi.spyOn(tmdb.apiClient, 'get').mockResolvedValue(response);
const result = await tmdb.movies.getMovieCredits(movieId);
(0, vitest_1.expect)(result).toEqual(response);
(0, vitest_1.expect)(tmdb.apiClient.get).toHaveBeenCalledWith(`movie/${movieId}/credits`);
});
});
});
//# sourceMappingURL=movie.spec.js.map