From f3839189dc038f1e0bc7798bc802ccaedcb54350 Mon Sep 17 00:00:00 2001 From: joakin Date: Wed, 15 Feb 2017 16:18:59 +0100 Subject: [PATCH] Test: Migrate isEnabled.test.js to node-qunit Change-Id: I85a08725c9138f47a51cdc34c20bc3a60b61af34 --- tests/node-qunit/isEnabled.test.js | 68 ++++++++++++++++++++++++ tests/node-qunit/stubs.js | 17 ++++++ tests/qunit/ext.popups/isEnabled.test.js | 65 ---------------------- 3 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 tests/node-qunit/isEnabled.test.js create mode 100644 tests/node-qunit/stubs.js delete mode 100644 tests/qunit/ext.popups/isEnabled.test.js diff --git a/tests/node-qunit/isEnabled.test.js b/tests/node-qunit/isEnabled.test.js new file mode 100644 index 000000000..c43140f81 --- /dev/null +++ b/tests/node-qunit/isEnabled.test.js @@ -0,0 +1,68 @@ +/* global Map: false */ + +var stubs = require( './stubs' ), + isEnabled = require( '../../src/isEnabled' ); + +function createStubUserSettings( isEnabled ) { + return { + hasIsEnabled: function () { + return isEnabled !== undefined; + }, + getIsEnabled: function () { + return Boolean( isEnabled ); + } + }; +} + +QUnit.module( 'ext.popups#isEnabled (logged out)', { + setup: function () { + this.user = stubs.createStubUser( /* isAnon = */ true ); + } +} ); + +QUnit.test( 'is should handle logged out users', function ( assert ) { + var user = stubs.createStubUser( /* isAnon = */ true ), + cases, + i, testCase, + userSettings, + config = new Map(); + + cases = [ + [ undefined, true, 'When the user hasn\'t enabled or disabled the feature' ], + [ false, false, 'When the user has disabled the feature' ], + [ true, true, 'When the user has enabled the feature' ] + ]; + + for ( i = 0; i < cases.length; i++ ) { + testCase = cases[ i ]; + userSettings = createStubUserSettings( testCase[ 0 ] ); + + assert.equal( + isEnabled( user, userSettings, config ), + testCase[ 1 ], + testCase[ 2 ] + ); + } + + // --- + + config.set( 'wgPopupsBetaFeature', true ); + + assert.notOk( + isEnabled( user, userSettings, config ), + 'When Page Previews is enabled as a beta feature, then it\'s not enabled for logged out users.' + ); +} ); + +QUnit.test( 'it should handle logged in users', function ( assert ) { + var user = stubs.createStubUser( /* isAnon = */ false ), + userSettings = createStubUserSettings( false ), + config = new Map(); + + config.set( 'wgPopupsShouldSendModuleToUser', true ); + + assert.ok( + isEnabled( user, userSettings, config ), + 'If the user is logged in and Page Previews has booted, then it\'s enabled.' + ); +} ); diff --git a/tests/node-qunit/stubs.js b/tests/node-qunit/stubs.js new file mode 100644 index 000000000..8c10b79c1 --- /dev/null +++ b/tests/node-qunit/stubs.js @@ -0,0 +1,17 @@ +/** + * Creates a **minimal** stub that can be used in place of an `mw.User` + * instance. + * + * @param {boolean} isAnon The return value of the `#isAnon`. + * @return {Object} + */ +exports.createStubUser = function createStubUser( isAnon ) { + return { + isAnon: function () { + return isAnon; + }, + sessionId: function () { + return '0123456789'; + } + }; +}; diff --git a/tests/qunit/ext.popups/isEnabled.test.js b/tests/qunit/ext.popups/isEnabled.test.js deleted file mode 100644 index c4ba9b816..000000000 --- a/tests/qunit/ext.popups/isEnabled.test.js +++ /dev/null @@ -1,65 +0,0 @@ -( function ( mw ) { - - function createStubUserSettings( isEnabled ) { - return { - hasIsEnabled: function () { - return isEnabled !== undefined; - }, - getIsEnabled: function () { - return Boolean(isEnabled); - } - }; - } - - QUnit.module( 'ext.popups#isEnabled (logged out)', { - setup: function () { - this.user = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ); - } - } ); - - QUnit.test( 'is should handle logged out users', function ( assert ) { - var user = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ), - cases, - userSettings, - config = new mw.Map(); - - cases = [ - [ undefined, true, 'When the user hasn\'t enabled or disabled the feature' ], - [ false, false, 'When the user has disabled the feature' ], - [ true, true, 'When the user has enabled the feature' ] - ]; - - $.each( cases, function ( _, testCase ) { - userSettings = createStubUserSettings( testCase[ 0 ] ); - - assert.equal( - mw.popups.isEnabled( user, userSettings, config ), - testCase[ 1 ], - testCase[ 2 ] - ); - } ); - - // --- - - config.set( 'wgPopupsBetaFeature', true ); - - assert.notOk( - mw.popups.isEnabled( user, userSettings, config ), - 'When Page Previews is enabled as a beta feature, then it\'s not enabled for logged out users.' - ); - } ); - - QUnit.test( 'it should handle logged in users', function ( assert ) { - var user = mw.popups.tests.stubs.createStubUser( /* isAnon = */ false ), - userSettings = createStubUserSettings( false ), - config = new mw.Map(); - - config.set( 'wgPopupsShouldSendModuleToUser', true ); - - assert.ok( - mw.popups.isEnabled( user, userSettings, config ), - 'If the user is logged in and Page Previews has booted, then it\'s enabled.' - ); - } ); - -}( mediaWiki ) );