-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
182 lines (163 loc) · 4.7 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').PhrasingContent} PhrasingContent
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {modifyChildren} from 'unist-util-modify-children'
function noop() {}
test('modifyChildren', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('unist-util-modify-children')).sort(),
['modifyChildren']
)
})
await t.test('should throw without node', async function () {
assert.throws(function () {
// @ts-expect-error: check that a runtime error is thrown.
modifyChildren(noop)()
}, /Missing children in `parent`/)
})
await t.test('should throw without parent', async function () {
assert.throws(function () {
// @ts-expect-error: check that a runtime error is thrown.
modifyChildren(noop)({})
}, /Missing children in `parent`/)
})
await t.test('should call `fn` for each child in `parent`', function () {
/** @type {Array<PhrasingContent>} */
const children = [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
]
/** @type {Emphasis} */
const context = {type: 'emphasis', children}
let n = -1
modifyChildren(function (child, index, parent) {
n++
assert.strictEqual(child, children[n])
assert.strictEqual(index, n)
assert.strictEqual(parent, context)
})(context)
})
await t.test('should work when new children are added', function () {
/** @type {Array<PhrasingContent>} */
const children = [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'},
{type: 'text', value: '4'},
{type: 'text', value: '5'},
{type: 'text', value: '6'}
]
/** @type {Emphasis} */
const parent = {
type: 'emphasis',
children: [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
]
}
let n = -1
modifyChildren(
/**
* @param {PhrasingContent} child
* @param {Emphasis} parent
*/
function (child, index, parent) {
n++
if (index < 3) {
parent.children.push({
type: 'text',
value: String(parent.children.length)
})
}
assert.deepEqual(child, children[n])
assert.deepEqual(index, n)
}
)(parent)
})
await t.test('should skip forwards', function () {
/** @type {Array<PhrasingContent>} */
const children = [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
]
/** @type {Emphasis} */
const context = {
type: 'emphasis',
children: [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '3'}
]
}
let n = -1
modifyChildren(
/**
* @param {PhrasingContent} child
* @param {Emphasis} parent
*/
function (child, index, parent) {
assert.deepEqual(child, children[++n])
if ('value' in child && child.value === '1') {
parent.children.splice(index + 1, 0, {type: 'text', value: '2'})
return index + 1
}
}
)(context)
assert.deepEqual(context.children, children)
})
await t.test('should skip backwards', function () {
const calls = [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '-1'},
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
]
/** @type {Emphasis} */
const context = {
type: 'emphasis',
children: [
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
]
}
let n = -1
let inserted = false
modifyChildren(
/**
* @param {PhrasingContent} child
* @param {Emphasis} parent
*/
function (child, _, parent) {
assert.deepEqual(child, calls[++n])
if (!inserted && 'value' in child && child.value === '1') {
inserted = true
parent.children.unshift({type: 'text', value: '-1'})
return -1
}
}
)(context)
assert.deepEqual(context.children, [
{type: 'text', value: '-1'},
{type: 'text', value: '0'},
{type: 'text', value: '1'},
{type: 'text', value: '2'},
{type: 'text', value: '3'}
])
})
})