Cdentinger has uploaded a new change for review. https://gerrit.wikimedia.org/r/227889
Change subject: adds geoIP tests ...................................................................... adds geoIP tests Change-Id: Ia9e47eb8efd25e2a76279cea7fda847c09fa9c01 --- A tests/qunit/subscribing/ext.centralNotice.geoIP.tests.js 1 file changed, 100 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice refs/changes/89/227889/1 diff --git a/tests/qunit/subscribing/ext.centralNotice.geoIP.tests.js b/tests/qunit/subscribing/ext.centralNotice.geoIP.tests.js new file mode 100644 index 0000000..8c867ab --- /dev/null +++ b/tests/qunit/subscribing/ext.centralNotice.geoIP.tests.js @@ -0,0 +1,100 @@ +( function ( me, $ ) { + 'use strict'; + + var badCookie = 'the car is on fire', + blankGeo = { country: '', region: '', city: '', lat: '', lon: '', af: 'vx' }, + cookieName = 'GeoIP', + madeAjaxCall = false, + realAjax = $.ajax; + + QUnit.module( 'ext.centralNotice.geoIP', QUnit.newMwEnvironment( { + setup: function () { + }, + teardown: function () { + $.ajax = realAjax; + } + } ) ); + + QUnit.test( 'validCookie', 3, function ( assert ) { + // Geo info should already be present here. + assert.ok( typeof window.Geo === 'object' ); + + // Nuke it. + window.Geo = null; + + // Set up the ajax smoke signal. + $.ajax = function () { + madeAjaxCall = true; + // .always() gets called, so support it. + var p = $.Deferred(); + p.resolve(); + return p.promise(); + } + + // Running again should reparse the cookie. + mw.geoIP.setWindowGeo(); + + // When the promise resolves, you will find these things to be true. + $.when( mw.geoIP.getPromise() ).then( function () { + // Should not make a background call when cookie is present. + assert.equal( madeAjaxCall, false ); + // Should re-add geo info. + assert.equal( typeof window.Geo, 'object' ); + } ); + + } ); + + QUnit.test( 'invalidCookie', 3, function ( assert ) { + // Save the cookie and kill it. + var goodCookie = $.cookie( cookieName ); + $.removeCookie( cookieName ); + + // Make a broken one. + $.cookie( cookieName, badCookie ); + + // Set the ajax call up to restore it. + $.ajax = function () { + madeAjaxCall = true; + $.removeCookie( cookieName ); + $.cookie( cookieName, goodCookie ); + var p = $.Deferred(); + p.resolve(); + return p.promise(); + } + + // Get busy. + window.Geo = null; + mw.geoIP.setWindowGeo(); + $.when( mw.geoIP.getPromise() ).then( function () { + assert.equal( madeAjaxCall, true ); + assert.equal( $.cookie( cookieName ), goodCookie ); + assert.equal( typeof window.Geo, 'object' ); + } ); + } ); + + QUnit.test( 'requestFails', 4, function ( assert ) { + // Nuke the cookie. + $.removeCookie( cookieName ); + + // Set the ajax call to fail and leave the geo data "blank" + $.ajax = function () { + madeAjaxCall = true; + var p = $.Deferred(); + p.resolve(); + return p.promise(); + } + + window.Geo = null; + mw.geoIP.setWindowGeo(); + $.when( mw.geoIP.getPromise() ).then( function () { + // TODO I don't think this callback should be firing. + assert.equal( JSON.stringify( window.Geo ), JSON.stringify( blankGeo ) ); + assert.equal( madeAjaxCall, true ); + assert.equal( $.cookie( cookieName ), null ); + assert.equal( typeof window.Geo, 'object' ); + }, function () { + // TODO I think it should be this one as the promise gets rejected. + } ); + } ); + +}( mediaWiki, jQuery ) ); -- To view, visit https://gerrit.wikimedia.org/r/227889 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia9e47eb8efd25e2a76279cea7fda847c09fa9c01 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/CentralNotice Gerrit-Branch: campaign_mixins Gerrit-Owner: Cdentinger <cdentin...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits