File: ve.dm.BranchNode.test.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (132 lines) | stat: -rw-r--r-- 4,368 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*!
 * VisualEditor DataModel BranchNode tests.
 *
 * @copyright See AUTHORS.txt
 */

QUnit.module( 've.dm.BranchNode' );

/* Stubs */

ve.dm.BranchNodeStub = function VeDmBranchNodeStub() {
	// Parent constructor
	ve.dm.BranchNodeStub.super.apply( this, arguments );
};

OO.inheritClass( ve.dm.BranchNodeStub, ve.dm.BranchNode );

ve.dm.BranchNodeStub.static.name = 'branch-stub';

ve.dm.BranchNodeStub.static.matchTagNames = [];

// Throw in nodeAttached so BranchNodeStub better passes duck type scrutiny as a Document
// (e.g. for setDocument test below)
ve.dm.BranchNodeStub.prototype.nodeAttached = ve.Document.prototype.nodeAttached;

// Throw in nodeDetached so BranchNodeStub better passes duck type scrutiny as a Document
// (e.g. for setDocument test below)
ve.dm.BranchNodeStub.prototype.nodeDetached = ve.Document.prototype.nodeDetached;

ve.dm.nodeFactory.register( ve.dm.BranchNodeStub );

/* Tests */

QUnit.test( 'canHaveChildren', ( assert ) => {
	const node = new ve.dm.BranchNodeStub();
	assert.strictEqual( node.canHaveChildren(), true );
} );

QUnit.test( 'canHaveChildrenNotContent', ( assert ) => {
	const node = new ve.dm.BranchNodeStub();
	assert.strictEqual( node.canHaveChildrenNotContent(), true );
} );

QUnit.test( 'setRoot', ( assert ) => {
	const 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 );
	assert.strictEqual( node3.getRoot(), node4 );
	assert.strictEqual( node2.getRoot(), node4 );
	assert.strictEqual( node1.getRoot(), node4 );
} );

QUnit.test( 'setDocument', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub( {}, [ node1 ] ),
		node3 = new ve.dm.BranchNodeStub( {}, [ node2 ] ),
		node4 = new ve.dm.BranchNodeStub();
	node3.setDocument( node4 );
	assert.strictEqual( node3.getDocument(), node4 );
	assert.strictEqual( node2.getDocument(), node4 );
	assert.strictEqual( node1.getDocument(), node4 );
} );

QUnit.test( 'push', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub(),
		node3 = new ve.dm.BranchNodeStub( {}, [ node1 ] );
	node3.on( 'splice', () => {
		// Will be called 1 time
		assert.true( true, 'splice was emitted' );
	} );
	assert.strictEqual( node3.push( node2 ), 2 );
	assert.deepEqual( node3.getChildren(), [ node1, node2 ] );
} );

QUnit.test( 'pop', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub(),
		node3 = new ve.dm.BranchNodeStub( {}, [ node1, node2 ] );
	node3.on( 'splice', () => {
		// Will be called 1 time
		assert.true( true, 'splice was emitted' );
	} );
	assert.strictEqual( node3.pop(), node2 );
	assert.deepEqual( node3.getChildren(), [ node1 ] );
} );

QUnit.test( 'unshift', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub(),
		node3 = new ve.dm.BranchNodeStub( {}, [ node1 ] );
	node3.on( 'splice', () => {
		// Will be called 1 time
		assert.true( true, 'splice was emitted' );
	} );
	assert.strictEqual( node3.unshift( node2 ), 2 );
	assert.deepEqual( node3.getChildren(), [ node2, node1 ] );
} );

QUnit.test( 'shift', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub(),
		node3 = new ve.dm.BranchNodeStub( {}, [ node1, node2 ] );
	node3.on( 'splice', () => {
		// Will be called 1 time
		assert.true( true, 'splice was emitted' );
	} );
	assert.strictEqual( node3.shift(), node1 );
	assert.deepEqual( node3.getChildren(), [ node2 ] );
} );

QUnit.test( 'splice', ( assert ) => {
	const node1 = new ve.dm.BranchNodeStub(),
		node2 = new ve.dm.BranchNodeStub(),
		node3 = new ve.dm.BranchNodeStub(),
		node4 = new ve.dm.BranchNodeStub( {}, [ node1, node2 ] );
	node4.on( 'splice', () => {
		// Will be called 3 times
		assert.true( true, 'splice was emitted' );
	} );
	// Insert branch
	assert.deepEqual( node4.splice( 1, 0, node3 ), [] );
	assert.deepEqual( node4.getChildren(), [ node1, node3, node2 ] );
	// Remove branch
	assert.deepEqual( node4.splice( 1, 1 ), [ node3 ] );
	assert.deepEqual( node4.getChildren(), [ node1, node2 ] );
	// Remove branch and insert branch
	assert.deepEqual( node4.splice( 1, 1, node3 ), [ node2 ] );
	assert.deepEqual( node4.getChildren(), [ node1, node3 ] );
} );