From 59bd5e00325869178f27e3e8db4086f0bb34edad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Wed, 5 Mar 2014 01:15:28 +0000 Subject: [PATCH] Add tests for promise rejection error logging Change-Id: I20d0803f211cbc4a7f00b58d015b565ca5c3b49b Mingle:https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/268 --- resources/mmv/provider/mmv.provider.Image.js | 2 +- .../mmv/provider/mmv.provider.Api.test.js | 72 ++++++++++++++++++- .../mmv/provider/mmv.provider.Image.test.js | 9 ++- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/resources/mmv/provider/mmv.provider.Image.js b/resources/mmv/provider/mmv.provider.Image.js index c0c527f60..fac28b92b 100644 --- a/resources/mmv/provider/mmv.provider.Image.js +++ b/resources/mmv/provider/mmv.provider.Image.js @@ -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 ); } ); }; diff --git a/tests/qunit/mmv/provider/mmv.provider.Api.test.js b/tests/qunit/mmv/provider/mmv.provider.Api.test.js index 79a797be9..35e20d053 100644 --- a/tests/qunit/mmv/provider/mmv.provider.Api.test.js +++ b/tests/qunit/mmv/provider/mmv.provider.Api.test.js @@ -15,7 +15,7 @@ * along with MultimediaViewer. If not, see . */ -( 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 ) ); diff --git a/tests/qunit/mmv/provider/mmv.provider.Image.test.js b/tests/qunit/mmv/provider/mmv.provider.Image.test.js index cd0359f65..b6dea35ba 100644 --- a/tests/qunit/mmv/provider/mmv.provider.Image.test.js +++ b/tests/qunit/mmv/provider/mmv.provider.Image.test.js @@ -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(); } ); } );