Skip to content

Commit 5b6e49f

Browse files
committed
Add sample tests in JS
1 parent 073c5cd commit 5b6e49f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

src/tests/OMDbAPI.test.js

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

0 commit comments

Comments
 (0)