tmdbrjs
Version:
A TypeScript wrapper for The Movie Database (TMDB) API
64 lines • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const BaseService_1 = require("../BaseService");
class TestService extends BaseService_1.BaseService {
async testGetById(id, options) {
return this.getByIdWithAppendToResponse('test/{id}', id, options);
}
}
(0, vitest_1.describe)('BaseService', () => {
let mockApiClient;
let testService;
(0, vitest_1.beforeEach)(() => {
mockApiClient = {
get: vitest_1.vi.fn(),
};
testService = new TestService(mockApiClient);
});
(0, vitest_1.describe)('getByIdWithAppendToResponse', () => {
(0, vitest_1.it)('should call API without append_to_response when no options provided', async () => {
const mockResponse = { id: 1, title: 'Test' };
vitest_1.vi.spyOn(mockApiClient, 'get').mockResolvedValue(mockResponse);
const result = await testService.testGetById('123');
(0, vitest_1.expect)(mockApiClient.get).toHaveBeenCalledWith('https://api.themoviedb.org/3/test/123');
(0, vitest_1.expect)(result).toEqual(mockResponse);
});
(0, vitest_1.it)('should call API with append_to_response when options provided', async () => {
const mockResponse = { id: 1, title: 'Test', credits: { cast: [] } };
vitest_1.vi.spyOn(mockApiClient, 'get').mockResolvedValue(mockResponse);
const result = await testService.testGetById('123', { include: ['credits'] });
(0, vitest_1.expect)(mockApiClient.get).toHaveBeenCalledWith('https://api.themoviedb.org/3/test/123?append_to_response=credits');
(0, vitest_1.expect)(result).toEqual(mockResponse);
});
(0, vitest_1.it)('should handle multiple append options', async () => {
const mockResponse = {
id: 1,
title: 'Test',
credits: { cast: [] },
videos: { results: [] },
};
vitest_1.vi.spyOn(mockApiClient, 'get').mockResolvedValue(mockResponse);
const result = await testService.testGetById('123', {
include: ['credits', 'videos'],
});
(0, vitest_1.expect)(mockApiClient.get).toHaveBeenCalledWith('https://api.themoviedb.org/3/test/123?append_to_response=credits%2Cvideos');
(0, vitest_1.expect)(result).toEqual(mockResponse);
});
(0, vitest_1.it)('should throw enhanced error for append_to_response failures', async () => {
const error = new Error('append_to_response validation failed');
vitest_1.vi.spyOn(mockApiClient, 'get').mockRejectedValue(error);
await (0, vitest_1.expect)(testService.testGetById('123', { include: ['credits'] })).rejects.toThrow('Failed to fetch test/{id} with append_to_response options: append_to_response validation failed');
});
(0, vitest_1.it)('should throw general error for other failures', async () => {
const error = new Error('Network error');
vitest_1.vi.spyOn(mockApiClient, 'get').mockRejectedValue(error);
await (0, vitest_1.expect)(testService.testGetById('123')).rejects.toThrow('Failed to fetch test/{id}: Network error');
});
(0, vitest_1.it)('should handle non-Error objects', async () => {
vitest_1.vi.spyOn(mockApiClient, 'get').mockRejectedValue('String error');
await (0, vitest_1.expect)(testService.testGetById('123')).rejects.toThrow('Failed to fetch test/{id}: Something went wrong');
});
});
});
//# sourceMappingURL=BaseService.spec.js.map