mediawiki-extensions-Visual.../modules/ve/test/ve.Factory.test.js
Trevor Parscal d8ee3c2c29 After much research on error objects, native = good, custom = bad
Stack traces, line numbers, etc. All the approaches I've seen are bad hacks. This is the best way to go.

Change-Id: Ib12e9d2ecfe610bcc89d046005e35cc13efa3d99
2012-08-08 10:48:53 -07:00

52 lines
1.3 KiB
JavaScript

/**
* VisualEditor Factory tests.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.Factory' );
/* Stubs */
ve.FactoryObjectStub = function ( a, b ) {
this.a = a;
this.b = b;
};
/* Tests */
QUnit.test( 'register', 1, function ( assert ) {
var factory = new ve.Factory();
assert.throws(
function () {
factory.register( 'factory-object-stub', 'not-a-function' );
},
Error,
'throws an exception when trying to register a non-function value as a constructor'
);
} );
QUnit.test( 'create', 2, function ( assert ) {
var factory = new ve.Factory();
assert.throws(
function () {
factory.create( 'factory-object-stub', 23, { 'bar': 'baz' } );
},
Error,
'throws an exception when trying to create a object of an unregistered type'
);
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
assert.deepEqual(
factory.create( 'factory-object-stub', 16, { 'baz': 'quux' } ),
new ve.FactoryObjectStub( 16, { 'baz': 'quux' } ),
'creates objects of a registered type and passes through arguments'
);
} );
QUnit.test( 'lookup', 1, function ( assert ) {
var factory = new ve.Factory();
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
} );