|
| 1 | +import { enableFetchMocks } from 'jest-fetch-mock'; |
| 2 | +import fetchMock from 'jest-fetch-mock'; |
| 3 | +import { WikipediaAPI } from '../api/apis/WikipediaAPI'; |
| 4 | +import { WikiModel } from '../models/WikiModel'; |
| 5 | +import { MediaTypeModel } from '../models/MediaTypeModel'; |
| 6 | + |
| 7 | +enableFetchMocks() |
| 8 | +let wikiInstance: WikipediaAPI; |
| 9 | + |
| 10 | +beforeEach(() => { |
| 11 | + fetchMock.resetMocks(); |
| 12 | + wikiInstance = new WikipediaAPI(new (jest.fn())()); |
| 13 | +}) |
| 14 | + |
| 15 | +test("searchByTitle behavior when given garbage data", async () => { |
| 16 | + fetchMock.mockResponseOnce(JSON.stringify({ |
| 17 | + data: "string" |
| 18 | + })) |
| 19 | + await expect(async () => await wikiInstance.searchByTitle("sample")).rejects.toThrow(); |
| 20 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 21 | +}); |
| 22 | + |
| 23 | +test("searchByTitle when fetch returns non-200", async () => { |
| 24 | + let sampleResponse = { |
| 25 | + data: "string" |
| 26 | + }; |
| 27 | + fetchMock.mockResponse(JSON.stringify(sampleResponse), {status: 400}); |
| 28 | + await expect(async () => await wikiInstance.searchByTitle("sample")).rejects.toThrow(); |
| 29 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 30 | +}); |
| 31 | + |
| 32 | +test("searchByTitle with successful request and valid response", async () => { |
| 33 | + let response = { |
| 34 | + "batchcomplete": "", |
| 35 | + "continue": { |
| 36 | + "sroffset": 20, |
| 37 | + "continue": "-||" |
| 38 | + }, |
| 39 | + "query": { |
| 40 | + "searchinfo": { |
| 41 | + "totalhits": 214380 |
| 42 | + }, |
| 43 | + "search": [ |
| 44 | + { |
| 45 | + "ns": 0, |
| 46 | + "title": "Jackson", |
| 47 | + "pageid": 15772, |
| 48 | + "size": 3524, |
| 49 | + "wordcount": 392, |
| 50 | + "timestamp": "2022-06-16T07:22:28Z" |
| 51 | + }, |
| 52 | + { |
| 53 | + "ns": 0, |
| 54 | + "title": "Michael Jackson", |
| 55 | + "pageid": 14995351, |
| 56 | + "size": 257103, |
| 57 | + "wordcount": 23309, |
| 58 | + "timestamp": "2022-06-29T01:15:25Z" |
| 59 | + }, |
| 60 | + { |
| 61 | + "ns": 0, |
| 62 | + "title": "Andrew Jackson", |
| 63 | + "pageid": 1623, |
| 64 | + "size": 200137, |
| 65 | + "wordcount": 22743, |
| 66 | + "timestamp": "2022-07-03T17:53:13Z" |
| 67 | + }, |
| 68 | + { |
| 69 | + "ns": 0, |
| 70 | + "title": "Janet Jackson", |
| 71 | + "pageid": 60070, |
| 72 | + "size": 206594, |
| 73 | + "wordcount": 21067, |
| 74 | + "timestamp": "2022-06-24T19:40:30Z" |
| 75 | + } |
| 76 | + ] |
| 77 | + } |
| 78 | + }; |
| 79 | + fetchMock.mockResponse(JSON.stringify(response)); |
| 80 | + let ret: MediaTypeModel[] = []; |
| 81 | + |
| 82 | + for (const result of response.query.search) { |
| 83 | + ret.push(new WikiModel({ |
| 84 | + type: 'wiki', |
| 85 | + title: result.title, |
| 86 | + englishTitle: result.title, |
| 87 | + year: '', |
| 88 | + dataSource: 'Wikipedia API', |
| 89 | + id: result.pageid, |
| 90 | + } as unknown as WikiModel)); |
| 91 | + } |
| 92 | + let dt = await wikiInstance.searchByTitle("Jackson"); |
| 93 | + expect(dt).toStrictEqual(ret); |
| 94 | + |
| 95 | +}) |
0 commit comments