mediawiki-extensions-Visual.../modules/ve/test/ve.Factory.test.js
Trevor Parscal 61ddfb76e4 Make ve.Factory require static name property
Objective:
* Make ve.Factory behave like ve.NamedClassFactory
* Remove the only remaining use of ve.Factory (actions)
* Remove ve.NamedClassFactory

Change-Id: Ie302ef5ea31081de7ab0db6091058a59946aef4c
2013-10-03 14:52:19 -07:00

73 lines
1.7 KiB
JavaScript

/*!
* VisualEditor Factory tests.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.Factory' );
/* Stubs */
ve.FactoryObjectStub = function VeFactoryObjectStub( a, b, c, d ) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
};
ve.FactoryObjectStub.static = {};
ve.FactoryObjectStub.static.name = 'factory-object-stub';
/* Tests */
QUnit.test( 'register', 2, function ( assert ) {
var factory = new ve.Factory();
assert.throws(
function () {
factory.register( 'not-a-function' );
},
Error,
'Throws an exception when trying to register a non-function value as a constructor'
);
factory.register( ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
} );
QUnit.test( 'create', 3, function ( assert ) {
var obj,
factory = new ve.Factory();
assert.throws(
function () {
factory.create( 'factory-object-stub', 23, 'foo', { 'bar': 'baz' } );
},
Error,
'Throws an exception when trying to create a object of an unregistered type'
);
factory.register( ve.FactoryObjectStub );
obj = factory.create( 'factory-object-stub', 16, 'foo', { 'baz': 'quux' }, 5 );
assert.deepEqual(
obj,
new ve.FactoryObjectStub( 16, 'foo', { 'baz': 'quux' }, 5 ),
'Creates an object of the registered type and passes through arguments'
);
assert.strictEqual(
obj instanceof ve.FactoryObjectStub,
true,
'Creates an object that is an instanceof the registered constructor'
);
} );
QUnit.test( 'lookup', 1, function ( assert ) {
var factory = new ve.Factory();
factory.register( ve.FactoryObjectStub );
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
} );