mediawiki-extensions-Visual.../modules/ve/test/dm/ve.dm.NodeFactory.test.js
Ed Sanders edcaaf9edc Use static.name once for ce and dm nodes
Add a static.name property to ce nodes and make sure both ce
and dm nodes always use the static.name property in constructors
and registration calls.

The result of this is that any given node type should now only
appear once in the code as a string.

Bug: 45701
Change-Id: Ibf31de16ab28ad58209c1443cd74f93dda278998
2013-03-07 17:19:39 -08:00

94 lines
2.7 KiB
JavaScript

/*!
* VisualEditor DataModel NodeFactory tests.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've.dm.NodeFactory' );
/* Stubs */
ve.dm.NodeFactoryNodeStub = function VeDmNodeFactoryNodeStub( a, b ) {
this.a = a;
this.b = b;
};
ve.inheritClass( ve.dm.NodeFactoryNodeStub, ve.dm.LeafNode );
ve.dm.NodeFactoryNodeStub.static.name = 'node-factory-node-stub';
ve.dm.NodeFactoryNodeStub.static.isContent = true;
ve.dm.NodeFactoryNodeStub.static.matchTagNames = [];
/* Tests */
QUnit.test( 'getChildNodeTypes', 2, function ( assert ) {
var factory = new ve.dm.NodeFactory();
assert.throws( function () {
factory.getChildNodeTypes( 'node-factory-node-stub', 23, { 'bar': 'baz' } );
},
Error,
'throws an exception when getting allowed child nodes of a node of an unregistered type'
);
factory.register( ve.dm.NodeFactoryNodeStub );
assert.deepEqual(
factory.getChildNodeTypes( 'node-factory-node-stub' ),
[],
'gets child type rules for registered nodes'
);
} );
QUnit.test( 'getParentNodeTypes', 2, function ( assert ) {
var factory = new ve.dm.NodeFactory();
assert.throws( function () {
factory.getParentNodeTypes( 'node-factory-node-stub', 23, { 'bar': 'baz' } );
},
Error,
'throws an exception when getting allowed parent nodes of a node of an unregistered type'
);
factory.register( ve.dm.NodeFactoryNodeStub );
assert.deepEqual(
factory.getParentNodeTypes( 'node-factory-node-stub' ),
null,
'gets parent type rules for registered nodes'
);
} );
QUnit.test( 'canNodeHaveChildren', 2, function ( assert ) {
var factory = new ve.dm.NodeFactory();
assert.throws( function () {
factory.canNodeHaveChildren( 'node-factory-node-stub', 23, { 'bar': 'baz' } );
},
Error,
'throws an exception when checking if a node of an unregistered type can have children'
);
factory.register( ve.dm.NodeFactoryNodeStub );
assert.strictEqual(
factory.canNodeHaveChildren( 'node-factory-node-stub' ),
false,
'gets child rules for registered nodes'
);
} );
QUnit.test( 'canNodeHaveGrandchildren', 2, function ( assert ) {
var factory = new ve.dm.NodeFactory();
assert.throws( function () {
factory.canNodeHaveGrandchildren( 'node-factory-node-stub', 23, { 'bar': 'baz' } );
},
Error,
'throws an exception when checking if a node of an unregistered type can have grandchildren'
);
factory.register( ve.dm.NodeFactoryNodeStub );
assert.strictEqual(
factory.canNodeHaveGrandchildren( 'node-factory-node-stub' ),
false,
'gets grandchild rules for registered nodes'
);
} );
QUnit.test( 'initialization', 1, function ( assert ) {
assert.ok( ve.dm.nodeFactory instanceof ve.dm.NodeFactory, 'factory is initialized at ve.dm.nodeFactory' );
} );