1+ import { enableFetchMocks } from 'jest-fetch-mock' ;
2+ import fetchMock from 'jest-fetch-mock' ;
3+ import { OMDbAPI } from '../api/apis/OMDbAPI' ;
4+ import MediaDbPlugin from '../main' ;
5+ import { MediaDbPluginSettings } from 'src/settings/Settings' ;
6+ import { MALAPI } from '../api/apis/MALAPI' ;
7+ import { LocGovAPI } from '../api/apis/LocGovAPI' ;
8+ import { MusicBrainzAPI } from '../api/apis/MusicBrainzAPI' ;
9+ import { SteamAPI } from '../api/apis/SteamAPI' ;
10+ import { WikipediaAPI } from '../api/apis/WikipediaAPI' ;
11+
12+ enableFetchMocks ( )
13+ let apiMock : OMDbAPI | MALAPI | LocGovAPI | MusicBrainzAPI | SteamAPI | WikipediaAPI ;
14+ let api : typeof OMDbAPI | typeof MALAPI | typeof LocGovAPI | typeof MusicBrainzAPI | typeof SteamAPI | typeof WikipediaAPI ;
15+
16+ describe . each (
17+ [
18+ { name : OMDbAPI } ,
19+ { name : MALAPI } ,
20+ { name : LocGovAPI } ,
21+ { name : MusicBrainzAPI } ,
22+ { name : SteamAPI } ,
23+ { name : WikipediaAPI }
24+ ]
25+ ) ( '$name.name' , ( { name : parameterizedApi } ) => {
26+ beforeAll ( ( ) => {
27+ api = parameterizedApi ;
28+ let settingsMock : MediaDbPluginSettings = {
29+ } as MediaDbPluginSettings ;
30+ let pluginMock = { } as MediaDbPlugin ;
31+ pluginMock . settings = settingsMock ;
32+ // TODO: add fake API key
33+ apiMock = new parameterizedApi ( pluginMock ) ;
34+ } )
35+
36+ beforeEach ( ( ) => {
37+ fetchMock . resetMocks ( ) ;
38+ } )
39+
40+ test ( "searchByTitle behavior when given garbage data" , async ( ) => {
41+ const garbageResponse = JSON . stringify ( {
42+ data : "string"
43+ } ) ;
44+ fetchMock . mockResponseOnce ( garbageResponse )
45+ let res ;
46+ // LocGovAPI throws an error because not implemented
47+ if ( parameterizedApi . name === "LocGovAPI" ) {
48+ await expect ( apiMock . searchByTitle ( "sample" ) ) . rejects . toThrow ( ) ;
49+
50+ } else {
51+ res = await apiMock . searchByTitle ( "sample" ) ;
52+ expect ( res ) . toEqual ( [ ] ) ;
53+ }
54+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
55+ } ) ;
56+
57+ test ( "searchByTitle when fetch returns 401" , async ( ) => {
58+ let sampleResponse = {
59+ data : "string"
60+ } ;
61+ fetchMock . mockResponse ( JSON . stringify ( sampleResponse ) , { status : 401 } ) ;
62+ // TODO: Check API name and fix message
63+ // TODO: Externalize string
64+ await expect ( async ( ) => await apiMock . searchByTitle ( "sample" ) ) . rejects . toThrow ( ) ;
65+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
66+ } ) ;
67+ } )
0 commit comments