Skip to content

Commit f5a4418

Browse files
committed
Add rudimentary TS tests
- Mocks need rewriting as they are not being created from module. Not scalable.
1 parent 5b6e49f commit f5a4418

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/tests/OMDbAPI.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { enableFetchMocks } from 'jest-fetch-mock';
2+
import fetchMock from 'jest-fetch-mock';
3+
import { OMDbAPI } from '../api/apis/OMDbAPI';
4+
import { MovieModel } from '../models/MovieModel';
5+
import { MediaTypeModel } from '../models/MediaTypeModel';
6+
import MediaDbPlugin from '../main';
7+
import { MediaDbPluginSettings } from 'src/settings/Settings';
8+
9+
enableFetchMocks()
10+
let OMDbApiMock: OMDbAPI;
11+
12+
beforeEach(() => {
13+
fetchMock.resetMocks();
14+
let settingsMock: MediaDbPluginSettings = {
15+
OMDbKey: "string"
16+
} as MediaDbPluginSettings;
17+
let pluginMock: MediaDbPlugin = {} as MediaDbPlugin;
18+
pluginMock.settings = settingsMock;
19+
OMDbApiMock = new OMDbAPI(pluginMock);
20+
})
21+
22+
test("searchByTitle behavior when given garbage data", async () => {
23+
fetchMock.mockResponseOnce(JSON.stringify({
24+
data: "string"
25+
}))
26+
let res = await OMDbApiMock.searchByTitle("sample");
27+
expect(res).toEqual([]);
28+
expect(fetchMock).toHaveBeenCalledTimes(1);
29+
});
30+
31+
test("searchByTitle when fetch returns 401", async () => {
32+
let sampleResponse = {
33+
data: "string"
34+
};
35+
fetchMock.mockResponse(JSON.stringify(sampleResponse), { status: 401 });
36+
// TODO: Check API name and fix message
37+
// TODO: Externalize string
38+
await expect(async () => await OMDbApiMock.searchByTitle("sample")).rejects.toThrowError("MDB | Authentication for OMDbAPI failed. Check the API key.");
39+
expect(fetchMock).toHaveBeenCalledTimes(1);
40+
});
41+
42+
test("searchByTitle when fetch returns other non-200", async () => {
43+
let sampleResponse = {
44+
data: "string"
45+
};
46+
fetchMock.mockResponse(JSON.stringify(sampleResponse), { status: 400 });
47+
await expect(async () => await OMDbApiMock.searchByTitle("sample")).rejects.toThrow();
48+
expect(fetchMock).toHaveBeenCalledTimes(1);
49+
});
50+
51+
test("searchByTitle with successful request and valid response", async () => {
52+
let response = {
53+
"Search": [
54+
{
55+
"Title": "Guardians of the Galaxy",
56+
"Year": "2014",
57+
"imdbID": "tt2015381",
58+
"Type": "movie",
59+
"Poster": "https://m.media-amazon.com/images/M/MV5BMTAwMjU5OTgxNjZeQTJeQWpwZ15BbWU4MDUxNDYxODEx._V1_SX300.jpg"
60+
},
61+
{
62+
"Title": "Guardians of the Galaxy Vol. 2",
63+
"Year": "2017",
64+
"imdbID": "tt3896198",
65+
"Type": "movie",
66+
"Poster": "https://m.media-amazon.com/images/M/MV5BNjM0NTc0NzItM2FlYS00YzEwLWE0YmUtNTA2ZWIzODc2OTgxXkEyXkFqcGdeQXVyNTgwNzIyNzg@._V1_SX300.jpg"
67+
},
68+
{
69+
"Title": "Guardians of the Galaxy: Inferno",
70+
"Year": "2017",
71+
"imdbID": "tt7131308",
72+
"Type": "movie",
73+
"Poster": "https://m.media-amazon.com/images/M/MV5BZGQ0YzEyNWQtNGJiMi00NTAxLThkNDctNGY2ODkzYWMxZmZkXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg"
74+
},
75+
{
76+
"Title": "LEGO Marvel Super Heroes - Guardians of the Galaxy: The Thanos Threat",
77+
"Year": "2017",
78+
"imdbID": "tt7387224",
79+
"Type": "movie",
80+
"Poster": "https://m.media-amazon.com/images/M/MV5BMjhlYzVhNTMtMmFkYy00NDhiLTkyNDgtYzhhMTZiMzM2OTA5XkEyXkFqcGdeQXVyNjI2OTgxNzY@._V1_SX300.jpg"
81+
},
82+
],
83+
"totalResults": "4",
84+
"Response": "True"
85+
}
86+
fetchMock.mockResponse(JSON.stringify(response));
87+
let ret: MediaTypeModel[] = [];
88+
89+
for (const result of response.Search) {
90+
ret.push(new MovieModel({
91+
type: 'wiki',
92+
title: result.Title,
93+
englishTitle: result.Title,
94+
year: result.Year,
95+
dataSource: 'OMDbAPI',
96+
id: result.imdbID,
97+
} as unknown as MovieModel));
98+
}
99+
let dt = await OMDbApiMock.searchByTitle("Jackson");
100+
expect(dt).toStrictEqual(ret);
101+
102+
})
103+

0 commit comments

Comments
 (0)