Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ve.BranchNode.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor BranchNode tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.BranchNode' );
8
9 /* Stubs */
10
11 ve.LabelledNodeStub = function VeLabelledNodeStub( label ) {
12 // Parent constructor
13 ve.LabelledNodeStub.super.call( this );
14 // Mixin constructor
15 OO.EventEmitter.call( this );
16 this.label = label;
17 };
18
19 OO.inheritClass( ve.LabelledNodeStub, ve.Node );
20 OO.mixinClass( ve.LabelledNodeStub, OO.EventEmitter );
21
22 ve.BranchNodeStub = function VeBranchNodeStub( label, children ) {
23 // Parent constructor
24 ve.BranchNodeStub.super.call( this, label );
25 // Mixin constructor
26 ve.BranchNode.call( this, children );
27 };
28
29 OO.inheritClass( ve.BranchNodeStub, ve.LabelledNodeStub );
30 OO.mixinClass( ve.BranchNodeStub, ve.BranchNode );
31
32 /* Tests */
33
34 QUnit.test( 'getChildren', ( assert ) => {
35 const node1 = new ve.BranchNodeStub( 'node1' ),
36 node2 = new ve.BranchNodeStub( 'node2', [ node1 ] );
37 assert.deepEqual( node1.getChildren(), [] );
38 assert.deepEqual( node2.getChildren(), [ node1 ] );
39 } );
40
41 QUnit.test( 'indexOf', ( assert ) => {
42 const node1 = new ve.BranchNodeStub( 'node1' ),
43 node2 = new ve.BranchNodeStub( 'node2' ),
44 node3 = new ve.BranchNodeStub( 'node3' ),
45 node4 = new ve.BranchNodeStub( 'node4', [ node1, node2, node3 ] );
46 assert.strictEqual( node4.indexOf( null ), -1 );
47 assert.strictEqual( node4.indexOf( node1 ), 0 );
48 assert.strictEqual( node4.indexOf( node2 ), 1 );
49 assert.strictEqual( node4.indexOf( node3 ), 2 );
50 } );
51
52 QUnit.test( 'setDocument', ( assert ) => {
53 const log = [],
54 doc = new ve.Document( new ve.BranchNodeStub( 'root' ) ),
55 list = new ve.BranchNodeStub( 'list', [
56 new ve.BranchNodeStub( 'listItem1', [
57 new ve.LabelledNodeStub( 'para1' )
58 ] ),
59 new ve.BranchNodeStub( 'listItem2', [
60 new ve.LabelledNodeStub( 'para2' )
61 ] )
62 ] ),
63 attachEvents = [ '+para1', '+listItem1', '+para2', '+listItem2', '+list' ],
64 detachEvents = [ '-para1', '-listItem1', '-para2', '-listItem2', '-list' ];
65
66 doc.connect( doc, {
67 nodeAttached: function ( node ) {
68 log.push( '+' + node.label );
69 },
70 nodeDetached: function ( node ) {
71 log.push( '-' + node.label );
72 }
73 } );
74 list.attach( doc.getDocumentNode() );
75 assert.deepEqual( log, attachEvents, 'Attach' );
76 log.length = 0;
77 list.detach();
78 assert.deepEqual( log, detachEvents, 'Detach' );
79 list.attach( doc.getDocumentNode() );
80 log.length = 0;
81 list.setDocument( new ve.Document( new ve.BranchNodeStub( 'root2' ) ) );
82 assert.deepEqual( log, detachEvents, 'Change to another doc' );
83 log.length = 0;
84 list.setDocument( doc );
85 assert.deepEqual( log, attachEvents, 'Change to this doc' );
86 } );