-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
103 lines (93 loc) · 2.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
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').PhrasingContent} PhrasingContent
* @typedef {import('mdast').Text} Text
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {visitChildren} from 'unist-util-visit-children'
function noop() {}
test('visitChildren', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('unist-util-visit-children')).sort(),
['visitChildren']
)
})
await t.test('should throw without arguments', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
visitChildren(noop)()
}, /Missing children in `parent`/)
})
await t.test('should throw without parent', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
visitChildren(noop)({})
}, /Missing children in `parent`/)
})
await t.test('should call `fn` for each child in `parent`', function () {
/** @type {Array<Text>} */
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
visitChildren(
/**
* @param {PhrasingContent} child
* @param {Emphasis} parent
*/
function (child, index, parent) {
n++
assert.equal(child, children[n])
assert.equal(index, n)
assert.equal(parent, context)
}
)(context)
})
await t.test('should work when new children are added', function () {
/** @type {Array<Text>} */
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
visitChildren(
/**
* @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)
})
})