mediawiki-extensions-Visual.../tests/ve2/dm/ve.dm.BranchNode.test.js
Trevor Parscal 9a3784301b Improved test coverage for ve.dm.BranchNode.splice and ve.dm.TwigNode.splice
* Changed splice to check all elements about to be inserted are allowed before inserting any of them so that catching an exception leaves you in a sane state
* Fixed the order of execution of parent class constructors in ve.dm.LeafNode and ve.dm.TwigNode so that canHaveChildren and canHaveGrandchildren produce correct values and added tests to ensure these methods are correctly inherited in subclasses
* Added tests that check for exceptions when adding nodes that can have children to nodes that can not have grandchildren
* Added test that check for events being emitted before and after splicing, including that beforeSplice should be emitted even in cases where a splice fails and throws an exception because the nodes are incompatible (but afterSplice is not called in this case) since beforeSplice might modify the nodes in some way before the compatibility tests are run

Change-Id: Id12aea995a42c26ff63a74ae3d31f2bf455759e3
2012-04-19 17:45:58 -07:00

122 lines
3.6 KiB
JavaScript

module( 've.dm.BranchNode' );
/* Stubs */
ve.dm.BranchNodeStub = function( children ) {
// Inheritance
ve.dm.BranchNode.call( this, 'stub', children );
};
ve.extendClass( ve.dm.BranchNodeStub, ve.dm.BranchNode );
/* Tests */
test( 'prototype.canHaveChildren', 1, function() {
var node = new ve.dm.BranchNodeStub();
strictEqual( node.canHaveChildren(), true );
} );
test( 'prototype.canHaveGrandchildren', 1, function() {
var node = new ve.dm.BranchNodeStub();
strictEqual( node.canHaveGrandchildren(), true );
} );
test( 'prototype.setRoot', 3, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub( [node1] ),
node3 = new ve.dm.BranchNodeStub( [node2] ),
node4 = new ve.dm.BranchNodeStub();
node3.setRoot( node4 );
strictEqual( node3.getRoot(), node4 );
strictEqual( node2.getRoot(), node4 );
strictEqual( node1.getRoot(), node4 );
} );
test( 'prototype.push', 4, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub(),
node3 = new ve.dm.BranchNodeStub( [node1] );
node3.on( 'beforeSplice', function() {
// Will be called 1 time
ok( true, 'beforeSplice was emitted' );
} );
node3.on( 'afterSplice', function() {
// Will be called 1 time
ok( true, 'afterSplice was emitted' );
} );
strictEqual( node3.push( node2 ), 2 );
deepEqual( node3.getChildren(), [node1, node2] );
} );
test( 'prototype.pop', 4, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub(),
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
node3.on( 'beforeSplice', function() {
// Will be called 1 time
ok( true, 'beforeSplice was emitted' );
} );
node3.on( 'afterSplice', function() {
// Will be called 1 time
ok( true, 'afterSplice was emitted' );
} );
strictEqual( node3.pop(), node2 );
deepEqual( node3.getChildren(), [node1] );
} );
test( 'prototype.unshift', 4, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub(),
node3 = new ve.dm.BranchNodeStub( [node1] );
node3.on( 'beforeSplice', function() {
// Will be called 1 time
ok( true, 'beforeSplice was emitted' );
} );
node3.on( 'afterSplice', function() {
// Will be called 1 time
ok( true, 'afterSplice was emitted' );
} );
strictEqual( node3.unshift( node2 ), 2 );
deepEqual( node3.getChildren(), [node2, node1] );
} );
test( 'prototype.shift', 4, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub(),
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
node3.on( 'beforeSplice', function() {
// Will be called 1 time
ok( true, 'beforeSplice was emitted' );
} );
node3.on( 'afterSplice', function() {
// Will be called 1 time
ok( true, 'afterSplice was emitted' );
} );
strictEqual( node3.shift(), node1 );
deepEqual( node3.getChildren(), [node2] );
} );
test( 'prototype.splice', 12, function() {
var node1 = new ve.dm.BranchNodeStub(),
node2 = new ve.dm.BranchNodeStub(),
node3 = new ve.dm.BranchNodeStub(),
node4 = new ve.dm.BranchNodeStub( [node1, node2] );
node4.on( 'beforeSplice', function() {
// Will be called 3 times
ok( true, 'beforeSplice was emitted' );
} );
node4.on( 'afterSplice', function() {
// Will be called 3 times
ok( true, 'afterSplice was emitted' );
} );
// Insert branch
deepEqual( node4.splice( 1, 0, node3 ), [] );
deepEqual( node4.getChildren(), [node1, node3, node2] );
// Remove branch
deepEqual( node4.splice( 1, 1 ), [node3] );
deepEqual( node4.getChildren(), [node1, node2] );
// Remove branch and insert branch
deepEqual( node4.splice( 1, 1, node3 ), [node2] );
deepEqual( node4.getChildren(), [node1, node3] );
} );