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

57 lines
1.2 KiB
JavaScript
Raw Normal View History

import createContainer from '../../src/container';
QUnit.module( 'container', {
beforeEach() {
this.container = createContainer();
this.factory = this.sandbox.stub();
}
} );
QUnit.test( '#has', function ( assert ) {
this.container.set( 'foo', this.factory );
assert.ok( this.container.has( 'foo' ), 'The container checks the factory.' );
} );
QUnit.test( '#get', function ( assert ) {
const service = {};
this.factory.returns( service );
this.container.set( 'foo', this.factory );
assert.strictEqual( service, this.container.get( 'foo' ) );
assert.strictEqual(
this.container,
this.factory.getCall( 0 ).args[ 0 ],
'The container uses the factory.'
);
// ---
this.container.get( 'foo' );
assert.ok(
this.factory.calledOnce,
'It should memoize the result of the factory.'
);
// ---
assert.throws(
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
() => { this.container.get( 'bar' ); },
/The service "bar" hasn't been defined./,
'The container throws an error when no factory exists.'
);
} );
QUnit.test( '#get should handle values, not just functions', function ( assert ) {
this.container.set( 'foo', 'bar' );
assert.strictEqual(
'bar',
this.container.get( 'foo' ),
'The container understands string values.'
);
} );