-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
158 lines (126 loc) · 4.04 KB
/
test.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @typedef {import('mdast').Text} Text
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').Root} Root
* @typedef {import('unist').Node} Node
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {remove} from 'unist-util-remove'
test('remove', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('unist-util-remove')).sort(), [
'remove'
])
})
await t.test('should compare nodes by partial properties', function () {
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const children = [leaf1, leaf2]
/** @type {Emphasis} */
const tree = u('emphasis', children)
remove(tree, {value: '2'})
assert.deepEqual(tree, u('emphasis', [leaf1]))
})
await t.test('should remove parent nodes', function () {
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const parent = u('emphasis', [leaf1])
const children = [parent, leaf2]
/** @type {Root} */
const tree = u('root', children)
remove(tree, test)
assert.deepEqual(tree, u('root', [leaf2]))
/**
* @param {Node} node
* @returns {boolean}
*/
function test(node) {
return node === parent
}
})
await t.test('should not check root nodes', function () {
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
remove(tree, 'root')
assert.deepEqual(
tree,
u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
)
})
await t.test('should cascade (remove) root nodes', function () {
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
remove(tree, 'text')
assert.deepEqual(tree, u('root', []))
})
await t.test(
'should not cascade (remove) nodes that were empty initially',
function () {
/** @type {Root} */
const tree = u('root', [
u('emphasis', []),
u('emphasis', [u('text', 'x')])
])
remove(tree, 'text')
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
}
)
await t.test('should support type tests', function () {
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
remove(tree, {cascade: false}, 'text')
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
})
await t.test('should support function tests', function () {
/** @type {Emphasis} */
const tree = u('emphasis', [
u('emphasis', [u('text', '1')]),
u('text', '2')
])
remove(tree, {cascade: false}, test)
assert.deepEqual(tree, u('emphasis', [u('emphasis', []), u('text', '2')]))
/**
* @param {Node} node
* @returns {boolean}
*/
function test(node) {
return node.type === 'text' && 'value' in node && node.value === '1'
}
})
await t.test('should support `cascade = true`', function () {
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
remove(tree, {cascade: true}, 'text')
assert.deepEqual(tree, u('root', []))
})
await t.test('should support `cascade = false`', function () {
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const nodeChildren = [leaf1]
const node = u('emphasis', nodeChildren)
const siblings = [node, leaf2]
/** @type {Root} */
const tree = u('root', siblings)
remove(tree, {cascade: false}, 'text')
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
})
await t.test('should support the example from readme', function () {
/** @type {Root} */
const tree = u('root', [
u('text', '1'),
u('emphasis', [
u('text', '2'),
u('emphasis', [u('text', '3'), u('inlineCode', '4')]),
u('emphasis', [u('text', '5')])
]),
u('text', '6')
])
remove(tree, 'text')
assert.deepEqual(
tree,
u('root', [u('emphasis', [u('emphasis', [u('inlineCode', '4')])])])
)
})
})