mediawiki-extensions-Popups/tests/node-qunit/previewBehavior.test.js
joakin 1fff0d2ea7 Improve & fix action and integration tests
This change fixes some issues with assertions not running, removes
unnecessary promise dances, and improves legibility and some code
patterns in the action and integration node tests mainly.

Detailed changes:

* actions.js
  * Fully migrate out of jQuery 1 promises (no done/fail)
  * Fix linkDwell action not returning the fetch action promise

* actions.test.js
  * Simplify setupWait for the tests
    * It always autoresolves immediately the wait call to ensure speedy tests
    * No waitDeferreds or waitPromises array coordination, rely on action
      returned promises instead
  * Get rid of that = this in favor of arrow functions
  * Rename generic "p" promises to meaningful names
  * Add assert.expect for more solid tests (so that we don't skip assertions in
    the future if we change them)
  * Fix some assertions that weren't being run because of the incorrect promise
    being returned (p.then, and then just returning p)
  * Get rid of $.when stubbing in favor of waiting for the promise returned from
    the action
    * Get rid of hacky setTimeout(..., 0) to run assertions after the promises

* integration.test.js
  * Get rid of wait(0) calls to hack around asynchronous actions
    * Use the action returned promises instead of the waitPromises/Deferreds
  * Remove unused "el" parameter being passed to this.abandon in several tests
  * Remove unnecessary test helper this.abandonPreview (it was the same as
    this.abandon)
  * Clarify a bit the last and more complex test with some comments and variable
    name changes
  * Get rid of that=this in favor of arrow functions

* container.test.js
  * Get rid of that=this in favor of arrow functions

* previewBehavior.test.js
  * Get rid of that=this in favor of arrow functions
  * Get rid of $.each in favor of .forEach

Bug: T170807
Change-Id: I06fafd92a1712f143018e2ddff24fadf1a6882b3
2018-02-16 13:52:56 +01:00

98 lines
2.7 KiB
JavaScript

import createPreviewBehavior from '../../src/previewBehavior';
import { createStubUser } from './stubs';
QUnit.module( 'ext.popups.preview.settingsBehavior', {
beforeEach: function () {
function newFromText( title ) {
return { getUrl: function () { return 'url/' + title; } };
}
mediaWiki.Title = { newFromText: newFromText };
/* global Map */ this.config = new Map();
},
afterEach: function () {
mediaWiki.Title = null;
}
} );
QUnit.test( 'it should set the settingsUrl on wgPopupsBetaFeature', function ( assert ) {
var user = createStubUser( /* isAnon = */ false ),
actions = {},
cases;
cases = [
[ true, 'Special:Preferences#mw-prefsection-betafeatures' ],
[ false, 'Special:Preferences#mw-prefsection-rendering' ]
];
cases.forEach( ( testCase ) => {
var behavior;
this.config.set( 'wgPopupsBetaFeature', testCase[ 0 ] );
behavior = createPreviewBehavior( this.config, user, actions );
assert.deepEqual(
behavior.settingsUrl,
'url/' + testCase[ 1 ]
);
} );
} );
QUnit.test( 'it shouldn\'t set the settingsUrl if the user is logged out', function ( assert ) {
var user = createStubUser( /* isAnon = */ true ),
actions = {},
behavior = createPreviewBehavior( this.config, user, actions );
assert.strictEqual( behavior.settingsUrl, undefined );
} );
QUnit.test( 'it shouldn\'t set a showSettings handler if the user is logged in', function ( assert ) {
var user = createStubUser( /* isAnon = */ false ),
actions = {},
behavior = createPreviewBehavior( this.config, user, actions );
assert.strictEqual( behavior.showSettings, $.noop );
} );
QUnit.test( 'it should set a showSettings handler if the user is logged out', function ( assert ) {
var user = createStubUser( /* isAnon = */ true ),
event = {
preventDefault: this.sandbox.spy()
},
actions = {
showSettings: this.sandbox.spy()
},
behavior = createPreviewBehavior( this.config, user, actions );
behavior.showSettings( event );
assert.ok(
event.preventDefault.called,
'It should prevent the default action of the event.'
);
assert.ok(
actions.showSettings.called,
'It should dispatch the SETTINGS_SHOW action.'
);
} );
QUnit.test( 'it should mix in default actions', function ( assert ) {
var user = createStubUser( /* isAnon = */ true ),
actions = {},
behavior;
actions.previewDwell = function () {};
actions.abandon = function () {};
actions.previewShow = function () {};
actions.linkClick = function () {};
behavior = createPreviewBehavior( this.config, user, actions );
assert.strictEqual( behavior.previewDwell, actions.previewDwell );
assert.strictEqual( behavior.previewAbandon, actions.abandon );
assert.strictEqual( behavior.previewShow, actions.previewShow );
assert.strictEqual( behavior.click, actions.linkClick );
} );