mediawiki-extensions-Popups/tests/node-qunit/previewBehavior.test.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

import createPreviewBehavior from '../../src/previewBehavior';
import { createStubUser } from './stubs';
QUnit.module( 'ext.popups.preview.settingsBehavior', {
beforeEach() {
function newFromText( title ) {
return { getUrl() { return `url/${ title }`; } };
}
mediaWiki.Title = { newFromText };
/* global Map */ this.config = new Map();
},
afterEach() {
mediaWiki.Title = null;
}
} );
QUnit.test( 'it should set the settingsUrl on wgPopupsBetaFeature', function ( assert ) {
const user = createStubUser( /* isAnon = */ false ),
actions = {};
const cases = [
[ true, 'Special:Preferences#mw-prefsection-betafeatures' ],
[ false, 'Special:Preferences#mw-prefsection-rendering' ]
];
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 12:52:56 +00:00
cases.forEach( ( testCase ) => {
this.config.set( 'wgPopupsBetaFeature', testCase[ 0 ] );
const 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 ) {
const 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 ) {
const 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 ) {
const 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 ) {
const user = createStubUser( /* isAnon = */ true ),
actions = {};
actions.previewDwell = () => {};
actions.abandon = () => {};
actions.previewShow = () => {};
actions.linkClick = () => {};
const 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 );
} );