Debian Codegraph @

skins/Vector/tests/jest/restSearchClient.test.js

raw symbols SCIP
1 /* global fetchMock */
2 const restSearchClient = require( '../../resources/skins.vector.search/restSearchClient.js' );
3 const jestFetchMock = require( 'jest-fetch-mock' );
4
5 const mockedRequests = !process.env.TEST_LIVE_REQUESTS;
6 const configMock = {
7 get: jest.fn().mockImplementation( ( key, fallback = null ) => {
8 if ( key === 'wgScriptPath' ) {
9 return '/w';
10 }
11 if ( key === 'wgScript' ) {
12 return '/w/index.php';
13 }
14 if ( key === 'wgVectorSearchApiUrl' ) {
15 return 'https://en.wikipedia.org/w/rest.php';
16 }
17 return fallback;
18 } ),
19 set: jest.fn()
20 };
21
22 describe( 'restApiSearchClient', () => {
23 beforeAll( () => {
24 jestFetchMock.enableFetchMocks();
25 } );
26
27 afterAll( () => {
28 jestFetchMock.disableFetchMocks();
29 } );
30
31 beforeEach( () => {
32 fetchMock.resetMocks();
33 if ( !mockedRequests ) {
34 fetchMock.disableMocks();
35 }
36 } );
37
38 test( '2 results', async () => {
39 const thumbUrl = '//upload.wikimedia.org/wikipedia/commons/0/01/MediaWiki-smaller-logo.png';
40 const restResponse = {
41 pages: [
42 {
43 id: 37298,
44 key: 'Media',
45 label: 'Media',
46 title: 'Media',
47 description: 'Wikimedia disambiguation page',
48 thumbnail: null,
49 url: '/w/index.php?title=Special%3ASearch&search=Media',
50 value: 37298
51 },
52 {
53 id: 323710,
54 key: 'MediaWiki',
55 label: 'MediaWiki',
56 title: 'MediaWiki',
57 description: 'wiki software',
58 thumbnail: {
59 width: 200,
60 height: 189,
61 url: thumbUrl
62 },
63 url: '/w/index.php?title=Special%3ASearch&search=MediaWiki',
64 value: 323710
65 }
66 ]
67 };
68 fetchMock.mockOnce( JSON.stringify( restResponse ) );
69
70 const searchResult = await restSearchClient( configMock ).fetchByTitle(
71 'media',
72 2
73 ).fetch;
74
75 const controller = new AbortController();
76
77 expect( searchResult.query ).toStrictEqual( 'media' );
78 expect( searchResult.results ).toBeTruthy();
79 expect( searchResult.results.length ).toBe( 2 );
80
81 expect( searchResult.results[ 0 ] ).toStrictEqual(
82 Object.assign( {}, restResponse.pages[ 0 ], {
83 // thumbnail: null -> thumbnail: undefined
84 thumbnail: undefined
85 } ) );
86 expect( searchResult.results[ 1 ] ).toStrictEqual( restResponse.pages[ 1 ] );
87
88 if ( mockedRequests ) {
89 expect( fetchMock ).toHaveBeenCalledTimes( 1 );
90 expect( fetchMock ).toHaveBeenCalledWith(
91 'https://en.wikipedia.org/w/rest.php/v1/search/title?q=media&limit=2',
92 { headers: { accept: 'application/json' }, signal: controller.signal }
93 );
94 }
95 } );
96
97 test( '0 results', async () => {
98 const restResponse = { pages: [] };
99 fetchMock.mockOnce( JSON.stringify( restResponse ) );
100
101 const searchResult = await restSearchClient( configMock ).fetchByTitle(
102 'thereIsNothingLikeThis'
103 ).fetch;
104
105 const controller = new AbortController();
106 expect( searchResult.query ).toStrictEqual( 'thereIsNothingLikeThis' );
107 expect( searchResult.results ).toBeTruthy();
108 expect( searchResult.results.length ).toBe( 0 );
109
110 if ( mockedRequests ) {
111 expect( fetchMock ).toHaveBeenCalledTimes( 1 );
112 expect( fetchMock ).toHaveBeenCalledWith(
113 'https://en.wikipedia.org/w/rest.php/v1/search/title?q=thereIsNothingLikeThis&limit=10',
114 { headers: { accept: 'application/json' }, signal: controller.signal }
115 );
116 }
117 } );
118
119 if ( mockedRequests ) {
120 test( 'network error', async () => {
121 fetchMock.mockRejectOnce( new Error( 'failed' ) );
122
123 await expect( restSearchClient( configMock ).fetchByTitle(
124 'anything'
125 ).fetch ).rejects.toThrow( 'failed' );
126 } );
127 }
128 } );