Add tests for promise rejection error logging

Change-Id: I20d0803f211cbc4a7f00b58d015b565ca5c3b49b
Mingle:https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/268
This commit is contained in:
Gergő Tisza 2014-03-05 01:15:28 +00:00
parent 44b9fa127a
commit 59bd5e0032
3 changed files with 78 additions and 5 deletions

View file

@ -62,7 +62,7 @@
}
return this.cache[cacheKey].fail( function ( error ) {
mw.log( provider.constructor + ' provider failed to load: ', error );
mw.log( provider.constructor.name + ' provider failed to load: ', error );
} );
};

View file

@ -15,7 +15,7 @@
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
( function ( mw ) {
( function ( mw, $ ) {
QUnit.module( 'mmv.provider.Api', QUnit.newMwEnvironment() );
QUnit.test( 'Api constructor sanity check', 2, function ( assert ) {
@ -28,6 +28,74 @@
assert.ok( ApiProviderWithNoOptions );
} );
QUnit.test( 'getCachedPromise success', 5, function ( assert ) {
var api = { get: function() {} },
apiProvider = new mw.mmv.provider.Api( api ),
oldMwLog = mw.log,
promiseSource,
promiseShouldBeCached = false;
mw.log = function () {
assert.ok( false, 'mw.log should not have been called' );
};
promiseSource = function ( result ) {
return function () {
assert.ok( !promiseShouldBeCached, 'promise was not cached' );
return $.Deferred().resolve( result );
};
};
apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).done( function ( result ) {
assert.strictEqual( result, 1, 'result comes from the promise source' );
} );
apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).done( function ( result ) {
assert.strictEqual( result, 2, 'result comes from the promise source' );
} );
promiseShouldBeCached = true;
apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).done( function ( result ) {
assert.strictEqual( result, 1, 'result comes from cache' );
} );
mw.log = oldMwLog;
} );
QUnit.test( 'getCachedPromise failure', 7, function ( assert ) {
var api = { get: function() {} },
apiProvider = new mw.mmv.provider.Api( api ),
oldMwLog = mw.log,
promiseSource,
promiseShouldBeCached = false;
mw.log = function () {
assert.ok( true, 'mw.log was called' );
};
promiseSource = function ( result ) {
return function () {
assert.ok( !promiseShouldBeCached, 'promise was not cached' );
return $.Deferred().reject( result );
};
};
apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).fail( function ( result ) {
assert.strictEqual( result, 1, 'result comes from the promise source' );
} );
apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).fail( function ( result ) {
assert.strictEqual( result, 2, 'result comes from the promise source' );
} );
promiseShouldBeCached = true;
apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).fail( function ( result ) {
assert.strictEqual( result, 1, 'result comes from cache' );
} );
mw.log = oldMwLog;
} );
QUnit.test( 'getErrorMessage', 2, function ( assert ) {
var api = { get: function() {} },
apiProvider = new mw.mmv.provider.Api( api ),
@ -174,4 +242,4 @@
QUnit.start();
} );
} );
}( mediaWiki ) );
}( mediaWiki, jQuery ) );

View file

@ -146,14 +146,19 @@
} );
} );
QUnit.asyncTest( 'Image load fail', 1, function ( assert ) {
var imageProvider = new mw.mmv.provider.Image();
QUnit.asyncTest( 'Image load fail', 2, function ( assert ) {
var imageProvider = new mw.mmv.provider.Image(),
oldMwLog = mw.log,
mwLogCalled = false;
imageProvider.imagePreloadingSupported = function () { return false; };
imageProvider.performance.recordEntry = $.noop;
mw.log = function () { mwLogCalled = true; };
imageProvider.get( 'doesntexist.png' ).fail( function() {
assert.ok( true, 'fail handler was called' );
assert.ok( mwLogCalled, 'mw.log was called' );
mw.log = oldMwLog;
QUnit.start();
} );
} );