Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/provider/mmv.provider.Api.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { Api } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.provider.Api', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'Api constructor sense check', ( assert ) => {
24 const api = { get: function () {} };
25 const options = {};
26 const apiProvider = new Api( api, options );
27 const ApiProviderWithNoOptions = new Api( api );
28
29 assert.true( apiProvider instanceof Api );
30 assert.true( ApiProviderWithNoOptions instanceof Api );
31 } );
32
33 QUnit.test( 'apiGetWithMaxAge()', function ( assert ) {
34 const api = {};
35 let options = {};
36 let apiProvider = new Api( api, options );
37
38 api.get = this.sandbox.stub();
39 apiProvider.apiGetWithMaxAge( {} );
40 assert.false( 'maxage' in api.get.getCall( 0 ).args[ 0 ], 'maxage is not set by default' );
41 assert.false( 'smaxage' in api.get.getCall( 0 ).args[ 0 ], 'smaxage is not set by default' );
42
43 options = { maxage: 123 };
44 apiProvider = new Api( api, options );
45
46 api.get = this.sandbox.stub();
47 apiProvider.apiGetWithMaxAge( {} );
48 assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].maxage, 123, 'maxage falls back to provider default' );
49 assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].smaxage, 123, 'smaxage falls back to provider default' );
50
51 api.get = this.sandbox.stub();
52 apiProvider.apiGetWithMaxAge( {}, null, 456 );
53 assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].maxage, 456, 'maxage can be overridden' );
54 assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].smaxage, 456, 'smaxage can be overridden' );
55
56 api.get = this.sandbox.stub();
57 apiProvider.apiGetWithMaxAge( {}, null, null );
58 assert.false( 'maxage' in api.get.getCall( 0 ).args[ 0 ], 'maxage can be overridden to unset' );
59 assert.false( 'smaxage' in api.get.getCall( 0 ).args[ 0 ], 'smaxage can be overridden to unset' );
60 } );
61
62 QUnit.test( 'getCachedPromise success', ( assert ) => {
63 const api = { get: function () {} };
64 const apiProvider = new Api( api );
65 const oldMwLog = mw.log;
66 let promiseShouldBeCached = false;
67
68 mw.log = function () {
69 assert.true( false, 'mw.log should not have been called' );
70 };
71
72 const promiseSource = function ( result ) {
73 return function () {
74 assert.strictEqual( promiseShouldBeCached, false, 'promise was not cached' );
75 return $.Deferred().resolve( result );
76 };
77 };
78
79 apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).done( ( result ) => {
80 assert.strictEqual( result, 1, 'result comes from the promise source' );
81 } );
82
83 apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).done( ( result ) => {
84 assert.strictEqual( result, 2, 'result comes from the promise source' );
85 } );
86
87 promiseShouldBeCached = true;
88 apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).done( ( result ) => {
89 assert.strictEqual( result, 1, 'result comes from cache' );
90 } );
91
92 mw.log = oldMwLog;
93 } );
94
95 QUnit.test( 'getCachedPromise failure', ( assert ) => {
96 const api = { get: function () {} };
97 const apiProvider = new Api( api );
98 const oldMwLog = mw.log;
99 let promiseShouldBeCached = false;
100
101 mw.log = function () {
102 assert.true( true, 'mw.log was called' );
103 };
104
105 const promiseSource = function ( result ) {
106 return function () {
107 assert.strictEqual( promiseShouldBeCached, false, 'promise was not cached' );
108 return $.Deferred().reject( result );
109 };
110 };
111
112 apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).fail( ( result ) => {
113 assert.strictEqual( result, 1, 'result comes from the promise source' );
114 } );
115
116 apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).fail( ( result ) => {
117 assert.strictEqual( result, 2, 'result comes from the promise source' );
118 } );
119
120 promiseShouldBeCached = true;
121 apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).fail( ( result ) => {
122 assert.strictEqual( result, 1, 'result comes from cache' );
123 } );
124
125 mw.log = oldMwLog;
126 } );
127
128 QUnit.test( 'getErrorMessage', ( assert ) => {
129 const api = { get: function () {} };
130 const apiProvider = new Api( api );
131
132 const errorMessage = apiProvider.getErrorMessage( {
133 servedby: 'mw1194',
134 error: {
135 code: 'unknown_action',
136 info: 'Unrecognized value for parameter \'action\': FOO'
137 }
138 } );
139 assert.strictEqual( errorMessage,
140 'unknown_action: Unrecognized value for parameter \'action\': FOO',
141 'error message is parsed correctly' );
142
143 assert.strictEqual( apiProvider.getErrorMessage( {} ), 'unknown error', 'missing error message is handled' );
144 } );
145
146 QUnit.test( 'getQueryPage', ( assert ) => {
147 const api = { get: function () {} };
148 const apiProvider = new Api( api );
149 const done = assert.async( 5 );
150
151 const data = {
152 query: {
153 pages: [
154 {
155 title: 'File:Stuff.jpg'
156 }
157 ]
158 }
159 };
160
161 apiProvider.getQueryPage( data ).then( ( field ) => {
162 assert.strictEqual( field, data.query.pages[ 0 ], 'specified page is found' );
163 done();
164 } );
165
166 apiProvider.getQueryPage( {} ).fail( () => {
167 assert.true( true, 'promise rejected when data is missing' );
168 done();
169 } );
170
171 apiProvider.getQueryPage( { data: { query: {} } } ).fail( () => {
172 assert.true( true, 'promise rejected when pages are missing' );
173 done();
174 } );
175
176 apiProvider.getQueryPage( { data: { query: { pages: [] } } } ).fail( () => {
177 assert.true( true, 'promise rejected when pages are empty' );
178 done();
179 } );
180
181 apiProvider.getQueryPage( {
182 query: {
183 pages: [
184 {
185 title: 'File:Stuff.jpg'
186 },
187 {
188 title: 'File:OtherStuff.jpg'
189 }
190 ]
191 }
192 } ).fail( () => {
193 assert.true( true, 'promise rejected when data contains two entries' );
194 done();
195 } );
196
197 } );
198 }() );