Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/mmv.testhelpers.js

raw symbols SCIP
1 const { MultimediaViewer } = require( 'mmv' );
2
3 ( function () {
4 const MTH = {};
5
6 /**
7 * Returns the exception thrown by callback, or undefined if no exception was thrown.
8 *
9 * @param {Function} callback
10 * @return {Error}
11 */
12 MTH.getException = function ( callback ) {
13 let ex;
14 try {
15 callback();
16 } catch ( e ) {
17 ex = e;
18 }
19 return ex;
20 };
21
22 /**
23 * Creates an mw.storage-like object.
24 *
25 * @param {Object} storage localStorage stub with getItem, setItem, removeItem methods
26 * @return {mw.SafeStorage} Local storage-like object
27 */
28 MTH.createLocalStorage = function ( storage ) {
29 return new ( Object.getPrototypeOf( mw.storage ) ).constructor( storage );
30 };
31
32 /**
33 * Returns an mw.storage that mimicks lack of localStorage support.
34 *
35 * @return {mw.SafeStorage} Local storage-like object
36 */
37 MTH.getUnsupportedLocalStorage = function () {
38 return MTH.createLocalStorage( undefined );
39 };
40
41 /**
42 * Returns an mw.storage that mimicks localStorage being disabled in browser.
43 *
44 * @return {mw.SafeStorage} Local storage-like object
45 */
46 MTH.getDisabledLocalStorage = function () {
47 const e = function () {
48 throw new Error( 'Error' );
49 };
50
51 return MTH.createLocalStorage( {
52 getItem: e,
53 setItem: e,
54 removeItem: e
55 } );
56 };
57
58 /**
59 * Returns a fake local storage which is not saved between reloads.
60 *
61 * @return {mw.SafeStorage} Local storage-like object
62 */
63 MTH.getFakeLocalStorage = function () {
64 const bag = new Map();
65 return MTH.createLocalStorage( {
66 getItem: ( key ) => bag.get( key ) || null,
67 setItem: ( key, value ) => bag.set( key, value ),
68 removeItem: ( key ) => bag.delete( key )
69 } );
70 };
71
72 /**
73 * Returns a viewer object with all the appropriate placeholder functions.
74 *
75 * @return {MultimediaViewer}
76 */
77 MTH.getMultimediaViewer = function () {
78 return new MultimediaViewer( {
79 imageQueryParameter: function () {},
80 language: function () {},
81 recordVirtualViewBeaconURI: function () {},
82 extensions: function () {
83 return { jpg: 'default' };
84 }
85 } );
86 };
87
88 MTH.asyncPromises = [];
89
90 /**
91 * Given a method/function that returns a promise, this'll return a function
92 * that just wraps the original & returns the original result, but also
93 * executes an assert.async() right before it's called, and resolves that
94 * async after that promise has completed.
95 *
96 * Example usage: given a method `bootstrap.openImage` that returns a
97 * promise, just call it like this to wrap this functionality around it:
98 * `bootstrap.openImage = asyncMethod( bootstrap.openImage, bootstrap );`
99 *
100 * Now, every time some part of the code calls this function, it'll just
101 * execute as it normally would, but your tests won't finish until these
102 * functions (and any .then tacked on to them) have completed.
103 *
104 * This method will make sure your tests don't end prematurely (before the
105 * promises have been resolved), but that's it. If you need to run
106 * additional code after all promises have resolved, you can call the
107 * complementary `waitForAsync`, which will return a promise that doesn't
108 * resolve until all of these promises have.
109 *
110 * @param {Object} object
111 * @param {string} method
112 * @param {QUnit.assert} [assert]
113 * @return {Function}
114 */
115 MTH.asyncMethod = function ( object, method, assert ) {
116 const helpers = this;
117 return function () {
118 // apply arguments to original promise
119 const promise = object[ method ].apply( object, arguments );
120
121 helpers.asyncPromises.push( promise );
122
123 if ( assert ) {
124 const done = assert.async();
125 // use setTimeout to ensure `done` is not the first callback handler
126 // to execute (possibly ending the test's wait right before
127 // the result of the promise is executed)
128 setTimeout( promise.then.bind( null, done, done ) );
129 }
130
131 return promise;
132 };
133 };
134
135 /**
136 * Returns a promise that will not resolve until all of the promises that
137 * were created in functions upon which `asyncMethod` was called have
138 * resolved.
139 *
140 * @return {jQuery.Promise}
141 */
142 MTH.waitForAsync = function () {
143 const deferred = $.Deferred();
144
145 // it's possible that, before this function call, some code was executed
146 // that triggers async code that will eventually end up `asyncPromises`
147 // in order to give that code a chance to run, we'll add another promise
148 // to the array, that will only resolve at the end of the current call
149 // stack (using setTimeout)
150 MTH.asyncPromises.push( deferred.promise() );
151 setTimeout( deferred.resolve );
152
153 return QUnit.whenPromisesComplete.apply( null, MTH.asyncPromises ).then(
154 () => {
155 MTH.asyncPromises = [];
156 }
157 );
158 };
159
160 module.exports = MTH;
161 }() );