From 57854d4176a5c098edb89a0a8bc3b0bc15d9018d Mon Sep 17 00:00:00 2001 From: joakin Date: Tue, 28 Feb 2017 18:08:38 +0100 Subject: [PATCH] Tests: Migrate userSettings.test.js to node-qunit Supporting changes: * Use mw.Map stub * Use assert.expect instead of number of assertions in QUnit.test (deprecated in newer QUnit) * Don't specify assert.expect( 1 ), because it is the default (no assertions will make the test fail). Change-Id: I64a3e76917e75b8c6d496f20e5b5dcafb338a46a --- tests/node-qunit/userSettings.test.js | 96 +++++++++++++++++++++ tests/qunit/ext.popups/userSettings.test.js | 93 -------------------- 2 files changed, 96 insertions(+), 93 deletions(-) create mode 100644 tests/node-qunit/userSettings.test.js delete mode 100644 tests/qunit/ext.popups/userSettings.test.js diff --git a/tests/node-qunit/userSettings.test.js b/tests/node-qunit/userSettings.test.js new file mode 100644 index 000000000..db1c4afb4 --- /dev/null +++ b/tests/node-qunit/userSettings.test.js @@ -0,0 +1,96 @@ +var stubs = require( './stubs' ), + createStubUser = stubs.createStubUser, + createStubMap = stubs.createStubMap, + createUserSettings = require( '../../src/userSettings' ); + +QUnit.module( 'ext.popups/userSettings', { + setup: function () { + var stubUser = createStubUser( /* isAnon = */ true ); + + this.storage = createStubMap(); + this.userSettings = createUserSettings( this.storage, stubUser ); + } +} ); + +QUnit.test( '#getIsEnabled should return false if Page Previews have been disabled', function ( assert ) { + assert.expect( 2 ); + + this.userSettings.setIsEnabled( false ); + + assert.notOk( this.userSettings.getIsEnabled() ); + + // --- + + this.userSettings.setIsEnabled( true ); + + assert.ok( + this.userSettings.getIsEnabled(), + '#getIsEnabled should return true if Page Previews have been enabled' + ); +} ); + +QUnit.test( '#hasIsEnabled', function ( assert ) { + var getStub; + + assert.expect( 3 ); + + assert.notOk( + this.userSettings.hasIsEnabled(), + '#hasIsEnabled should return false if the storage is empty.' + ); + + // --- + + this.userSettings.setIsEnabled( false ); + + assert.ok( + this.userSettings.hasIsEnabled(), + '#hasIsEnabled should return true even if "isEnabled" has been set to falsy.' + ); + + // --- + + getStub = this.sandbox.stub( this.storage, 'get' ).returns( false ); + + assert.notOk( + this.userSettings.hasIsEnabled(), + '#hasIsEnabled should return false if the storage is disabled.' + ); + + getStub.restore(); +} ); + +QUnit.test( '#getPreviewCount should return the count as a number', function ( assert ) { + assert.expect( 3 ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + 0, + '#getPreviewCount returns 0 when the storage is empty.' + ); + + // --- + + this.storage.set( 'ext.popups.core.previewCount', false ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + -1, + '#getPreviewCount returns -1 when the storage isn\'t available.' + ); + + // --- + + this.storage.set( 'ext.popups.core.previewCount', '111' ); + + assert.strictEqual( + this.userSettings.getPreviewCount(), + 111 + ); +} ); + +QUnit.test( '#setPreviewCount should store the count as a string', function ( assert ) { + this.userSettings.setPreviewCount( 222 ); + + assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '222' ); +} ); diff --git a/tests/qunit/ext.popups/userSettings.test.js b/tests/qunit/ext.popups/userSettings.test.js deleted file mode 100644 index 66b46a660..000000000 --- a/tests/qunit/ext.popups/userSettings.test.js +++ /dev/null @@ -1,93 +0,0 @@ -( function ( mw ) { - - QUnit.module( 'ext.popups/userSettings', { - setup: function () { - var stubUser = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ); - - this.storage = new mw.Map(); - this.userSettings = mw.popups.createUserSettings( this.storage, stubUser ); - } - } ); - - QUnit.test( '#getIsEnabled should return false if Page Previews have been disabled', 2, function ( assert ) { - this.userSettings.setIsEnabled( false ); - - assert.notOk( this.userSettings.getIsEnabled() ); - - // --- - - this.userSettings.setIsEnabled( true ); - - assert.ok( - this.userSettings.getIsEnabled(), - '#getIsEnabled should return true if Page Previews have been enabled' - ); - } ); - - QUnit.test( '#hasIsEnabled', 3, function ( assert ) { - var getStub; - - assert.notOk( - this.userSettings.hasIsEnabled(), - '#hasIsEnabled should return false if the storage is empty.' - ); - - // --- - - this.userSettings.setIsEnabled( false ); - - assert.ok( - this.userSettings.hasIsEnabled(), - '#hasIsEnabled should return true even if "isEnabled" has been set to falsy.' - ); - - // --- - - getStub = this.sandbox.stub( this.storage, 'get' ).returns( false ); - - assert.notOk( - this.userSettings.hasIsEnabled(), - '#hasIsEnabled should return false if the storage is disabled.' - ); - - getStub.restore(); - } ); - - QUnit.test( '#getPreviewCount should return the count as a number', function ( assert ) { - assert.expect( 3 ); - - assert.strictEqual( - this.userSettings.getPreviewCount(), - 0, - '#getPreviewCount returns 0 when the storage is empty.' - ); - - // --- - - this.storage.set( 'ext.popups.core.previewCount', false ); - - assert.strictEqual( - this.userSettings.getPreviewCount(), - -1, - '#getPreviewCount returns -1 when the storage isn\'t available.' - ); - - // --- - - this.storage.set( 'ext.popups.core.previewCount', '111' ); - - assert.strictEqual( - this.userSettings.getPreviewCount(), - 111 - ); - } ); - - QUnit.test( '#setPreviewCount should store the count as a string', function ( assert ) { - assert.expect( 1 ); - - this.userSettings.setPreviewCount( 222 ); - - assert.strictEqual( this.storage.get( 'ext.popups.core.previewCount' ), '222' ); - } ); - -}( mediaWiki ) );