mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/MultimediaViewer
synced 2024-11-29 02:14:21 +00:00
d204ecc99a
This heavily relied on deferreds getting resolved synchroneously, which (for .then) is no longer the case with jQuery 3. There's also a difference in how chained .then's get resolved. These tests did not wait for the deferreds to get resolved async, so that had to change. Worse even: some functionality was tested by assuming a handler would not be called (= the test in that handler would not be called), which didn't make the test run, because we never even waited for it to be called. The changes in here are basically: * make mmv.bootstrap.js correctly use .then instead of .done & fail * let mmw.bootstrap.test.js tests wait for unresolved async promises * check call counts instead of running tests inside of callbacks * limit use of fake timer to the 1 test that needs it (it interferes with async, which otherwise don't get resolved) Bug: T164473 Change-Id: I3ca4f786890ad1646f71fe336afdb61e5c3b01c7
139 lines
4.2 KiB
JavaScript
139 lines
4.2 KiB
JavaScript
( function ( mw, $ ) {
|
|
var MTH = {};
|
|
|
|
MTH.enterFullscreenMock = function () {
|
|
this.first().addClass( 'jq-fullscreened' ).data( 'isFullscreened', true );
|
|
|
|
$( document ).trigger( $.Event( 'jq-fullscreen-change', { element: this, fullscreen: true } ) );
|
|
};
|
|
|
|
MTH.exitFullscreenMock = function () {
|
|
this.first().removeClass( 'jq-fullscreened' ).data( 'isFullscreened', false );
|
|
|
|
$( document ).trigger( $.Event( 'jq-fullscreen-change', { element: this, fullscreen: false } ) );
|
|
};
|
|
|
|
/**
|
|
* Returns the exception thrown by callback, or undefined if no exception was thrown.
|
|
*
|
|
* @param {Function} callback
|
|
* @return {Error}
|
|
*/
|
|
MTH.getException = function ( callback ) {
|
|
var ex;
|
|
try {
|
|
callback();
|
|
} catch ( e ) {
|
|
ex = e;
|
|
}
|
|
return ex;
|
|
};
|
|
|
|
/**
|
|
* Returns a fake local storage which is not saved between reloads.
|
|
*
|
|
* @param {Object} [initialData]
|
|
* @return {Object} Local storage-like object
|
|
*/
|
|
MTH.getFakeLocalStorage = function ( initialData ) {
|
|
var bag = new mw.Map();
|
|
bag.set( initialData );
|
|
|
|
return {
|
|
getItem: function ( key ) { return bag.get( key ); },
|
|
setItem: function ( key, value ) { bag.set( key, value ); },
|
|
removeItem: function ( key ) { bag.set( key, null ); }
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Returns a viewer object with all the appropriate placeholder functions.
|
|
*
|
|
* @return {mv.mmv.MultiMediaViewer} [description]
|
|
*/
|
|
MTH.getMultimediaViewer = function () {
|
|
return new mw.mmv.MultimediaViewer( {
|
|
imageQueryParameter: $.noop,
|
|
language: $.noop,
|
|
recordVirtualViewBeaconURI: $.noop,
|
|
extensions: function () {
|
|
return { jpg: 'default' };
|
|
}
|
|
} );
|
|
};
|
|
|
|
MTH.asyncPromises = [];
|
|
|
|
/**
|
|
* Given a method/function that returns a promise, this'll return a function
|
|
* that just wraps the original & returns the original result, but also
|
|
* executes an assert.async() right before it's called, and resolves that
|
|
* async after that promise has completed.
|
|
*
|
|
* Example usage: given a method `bootstrap.openImage` that returns a
|
|
* promise, just call it like this to wrap this functionality around it:
|
|
* `bootstrap.openImage = asyncMethod( bootstrap.openImage, bootstrap );`
|
|
*
|
|
* Now, every time some part of the code calls this function, it'll just
|
|
* execute as it normally would, but your tests won't finish until these
|
|
* functions (and any .then tacked on to them) have completed.
|
|
*
|
|
* This method will make sure your tests don't end prematurely (before the
|
|
* promises have been resolved), but that's it. If you need to run
|
|
* additional code after all promises have resolved, you can call the
|
|
* complementary `waitForAsync`, which will return a promise that doesn't
|
|
* resolve until all of these promises have.
|
|
*
|
|
* @param {Object} object
|
|
* @param {string} method
|
|
* @param {QUnit.assert} [assert]
|
|
* @return {Function}
|
|
*/
|
|
MTH.asyncMethod = function ( object, method, assert ) {
|
|
return function () {
|
|
// apply arguments to original promise
|
|
var promise = object[ method ].apply( object, arguments ),
|
|
done;
|
|
|
|
this.asyncPromises.push( promise );
|
|
|
|
if ( assert ) {
|
|
done = assert.async();
|
|
// use setTimeout to ensure `done` is not the first callback handler
|
|
// to execute (possibly ending the test's wait right before
|
|
// the result of the promise is executed)
|
|
setTimeout( promise.then.bind( null, done, done ) );
|
|
}
|
|
|
|
return promise;
|
|
}.bind( this );
|
|
};
|
|
|
|
/**
|
|
* Returns a promise that will not resolve until all of the promises that
|
|
* were created in functions upon which `asyncMethod` was called have
|
|
* resolved.
|
|
*
|
|
* @return {$.Promise}
|
|
*/
|
|
MTH.waitForAsync = function () {
|
|
var deferred = $.Deferred();
|
|
|
|
// it's possible that, before this function call, some code was executed
|
|
// that triggers async code that will eventually end up `asyncPromises`
|
|
// in order to give that code a chance to run, we'll add another promise
|
|
// to the array, that will only resolve at the end of the current call
|
|
// stack (using setTimeout)
|
|
this.asyncPromises.push( deferred.promise() );
|
|
setTimeout( deferred.resolve );
|
|
|
|
return QUnit.whenPromisesComplete.apply( null, this.asyncPromises ).then(
|
|
function () {
|
|
this.asyncPromises = [];
|
|
}.bind( this )
|
|
);
|
|
};
|
|
|
|
mw.mmv.testHelpers = MTH;
|
|
}( mediaWiki, jQuery ) );
|