mirror of
https://github.com/vitaliiromanenko/popcorn-md.git
synced 2026-07-22 07:45:25 +00:00
feat: add unit tests for TMDb API service classes including movie and genre management
This commit is contained in:
parent
14be05e7e0
commit
66c2adefda
3 changed files with 201 additions and 0 deletions
34
tests/tmdbAPIService.test.ts
Normal file
34
tests/tmdbAPIService.test.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { TMDbAPIService } from "../src/api/tmdbAPIService";
|
||||
|
||||
const mockFetch = jest.fn()
|
||||
|
||||
global.fetch = mockFetch as any;
|
||||
|
||||
const settings = {
|
||||
apiKey: "FAKE_KEY",
|
||||
language: 'en-US'
|
||||
};
|
||||
|
||||
describe("TMDbAPIService", () =>{
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it("should append apiKey and language to requests", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({success: true}),
|
||||
});
|
||||
|
||||
const service = new TMDbAPIService(settings);
|
||||
const result = await service.checkAPIKey();
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
expect.stringContaining("api_key=FAKE_KEY")
|
||||
);
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
expect.stringContaining("language=en-US")
|
||||
);
|
||||
});
|
||||
});
|
||||
52
tests/tmdbGenreService.test.ts
Normal file
52
tests/tmdbGenreService.test.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import { TMDbGanreService } from "../src/api/tmdbGenreService";
|
||||
|
||||
const mockFetch = jest.fn()
|
||||
|
||||
global.fetch = mockFetch as any;
|
||||
|
||||
const settings = {
|
||||
apiKey: "FAKE_KEY",
|
||||
language: 'en-US'
|
||||
};
|
||||
|
||||
describe("TMDbGanreService", () =>{
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it("should initialize genre map", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
genres: [{id: 1, name: "Action"}, {id: 2, name: "Drama"}],
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbGanreService(settings);
|
||||
await service.init();
|
||||
|
||||
const mapped = service.mapGenreIds([1, 2, 3]);
|
||||
|
||||
expect(mapped).toEqual([
|
||||
{id: 1, name: "Action"},
|
||||
{id: 2, name: "Drama"},
|
||||
{id: 3, name: "Unknown"},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should return empty array when pass empty array", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
genres: [{id: 1, name: "Action"}, {id: 2, name: "Drama"}],
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbGanreService(settings);
|
||||
await service.init();
|
||||
|
||||
const mapped = service.mapGenreIds([]);
|
||||
|
||||
expect(mapped).toEqual([]);
|
||||
});
|
||||
});
|
||||
115
tests/tmdbMovieService.test.ts
Normal file
115
tests/tmdbMovieService.test.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import { TMDbMovieService } from "../src/api/tmdbMovieService";
|
||||
|
||||
const mockFetch = jest.fn()
|
||||
|
||||
global.fetch = mockFetch as any;
|
||||
|
||||
const settings = {
|
||||
apiKey: "FAKE_KEY",
|
||||
language: 'en-US'
|
||||
};
|
||||
|
||||
describe("TMDbGanreService", () =>{
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it("should search by IMDb id", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
movie_results: [
|
||||
{ id: 123, title: "The Matrix", adult: false, genre_ids: [1] },
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const result = await service.searchMovie("tt0133093");
|
||||
|
||||
expect(result.results[0]?.title).toBe("The Matrix");
|
||||
expect(result.total_results).toBe(1);
|
||||
});
|
||||
|
||||
it("should return empty array by wrong IMDb id", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
"movie_results": [],
|
||||
"person_results": [],
|
||||
"tv_results": [],
|
||||
"tv_episode_results": [],
|
||||
"tv_season_results": []
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const result = await service.searchMovie("tt0000000");
|
||||
|
||||
expect(result.results).toEqual([]);
|
||||
expect(result.total_results).toBe(0);
|
||||
});
|
||||
|
||||
it("should search by name", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
page: 1,
|
||||
results: [{ id: 456, title: "Inception" }],
|
||||
total_pages: 1,
|
||||
total_results: 1,
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const result = await service.searchMovie("Inception");
|
||||
|
||||
expect(result.results[0]?.title).toBe("Inception");
|
||||
expect(result.total_results).toBe(1);
|
||||
});
|
||||
|
||||
it("should return empty array by wrong name", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
page: 1,
|
||||
results: [],
|
||||
total_pages: 1,
|
||||
total_results: 0,
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const result = await service.searchMovie("rhueif");
|
||||
|
||||
expect(result.results).toEqual([]);
|
||||
expect(result.total_results).toBe(0);
|
||||
});
|
||||
|
||||
it("should search by id", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ id: 789, title: "Interstellar" }),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const movie = await service.getMovieById(789);
|
||||
expect(movie).not.toBeNull();
|
||||
expect(movie!.title).toBe("Interstellar");
|
||||
});
|
||||
|
||||
it("should return null by wrong id", async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
"success": false,
|
||||
"status_code": 34,
|
||||
"status_message": "The resource you requested could not be found."
|
||||
}),
|
||||
});
|
||||
|
||||
const service = new TMDbMovieService(settings);
|
||||
const movie = await service.getMovieById(78933493049);
|
||||
expect(movie).toBeNull();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue