-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
220 lines (190 loc) · 5.93 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').InlineCode} InlineCode
* @typedef {import('unist').Node} UnistNode
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {findBefore} from 'unist-util-find-before'
test('`findBefore`', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('unist-util-find-before')).sort(),
['findBefore']
)
})
const tree = fromMarkdown('Some *emphasis*, **importance**, and `code`.')
assert(tree.type === 'root')
const paragraph = tree.children[0]
assert(paragraph.type === 'paragraph')
const head = paragraph.children[0]
assert(head.type === 'text')
const next = paragraph.children[1]
assert(next.type === 'emphasis')
/** @type {Emphasis} */
const emphasis = {type: 'emphasis', children: []}
/** @type {InlineCode} */
const inlineCode = {type: 'inlineCode', value: 'a'}
await t.test('should fail without parent', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
findBefore()
}, /Expected parent node/)
})
await t.test('should fail without parent node', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
findBefore(inlineCode)
}, /Expected parent node/)
})
await t.test('should fail without index (#1)', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
findBefore(emphasis)
}, /Expected child node or index/)
})
await t.test('should fail without index (#2)', async function () {
assert.throws(function () {
findBefore(emphasis, -1)
}, /Expected positive finite number as index/)
})
await t.test('should fail without index (#3)', async function () {
assert.throws(function () {
findBefore(emphasis, inlineCode)
}, /Expected child node or index/)
})
await t.test('should fail for invalid `test` (#1)', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
findBefore(emphasis, 1, false)
}, /Expected function, string, or object as test/)
})
await t.test('should fail for invalid `test` (#2)', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
findBefore(emphasis, 1, true)
}, /Expected function, string, or object as test/)
})
await t.test(
'should return the preceding node when without `test` (#1)',
async function () {
assert.equal(findBefore(paragraph, paragraph.children[1]), head)
}
)
await t.test(
'should return the preceding node when without `test` (#2)',
async function () {
assert.equal(findBefore(paragraph, 1), head)
}
)
await t.test(
'should return the preceding node when without `test` (#3)',
async function () {
assert.equal(findBefore(paragraph, 0), undefined)
}
)
await t.test(
'should return `node` when given a `node` and existing (#1)',
async function () {
assert.equal(findBefore(paragraph, 100, head), head)
}
)
await t.test(
'should return `node` when given a `node` and existing (#2)',
async function () {
assert.equal(findBefore(paragraph, paragraph.children[1], head), head)
}
)
await t.test(
'should return `node` when given a `node` and existing (#3)',
async function () {
assert.equal(findBefore(paragraph, 1, head), head)
}
)
await t.test(
'should return `node` when given a `node` and existing (#4)',
async function () {
assert.equal(findBefore(paragraph, head, head), undefined)
}
)
await t.test(
'should return `node` when given a `node` and existing (#5)',
async function () {
assert.equal(findBefore(paragraph, 0, head), undefined)
}
)
await t.test(
'should return `node` when given a `node` and existing (#6)',
async function () {
assert.equal(findBefore(paragraph, 1, next), undefined)
}
)
await t.test(
'should return a child when given a `type` and existing (#1)',
async function () {
assert.equal(findBefore(paragraph, 100, 'strong'), paragraph.children[3])
}
)
await t.test(
'should return a child when given a `type` and existing (#2)',
async function () {
assert.equal(findBefore(paragraph, 3, 'strong'), undefined)
}
)
await t.test(
'should return a child when given a `type` and existing (#3)',
async function () {
assert.equal(
findBefore(paragraph, paragraph.children[4], 'strong'),
paragraph.children[3]
)
}
)
await t.test(
'should return a child when given a `type` and existing (#4)',
async function () {
assert.equal(
findBefore(paragraph, paragraph.children[3], 'strong'),
undefined
)
}
)
await t.test(
'should return a child when given a `test` and existing (#1)',
async function () {
assert.equal(findBefore(paragraph, 100, check), paragraph.children[3])
}
)
await t.test(
'should return a child when given a `test` and existing (#2)',
async function () {
assert.equal(findBefore(paragraph, 3, check), undefined)
}
)
await t.test(
'should return a child when given a `test` and existing (#3)',
async function () {
assert.equal(
findBefore(paragraph, paragraph.children[4], check),
paragraph.children[3]
)
}
)
await t.test(
'should return a child when given a `test` and existing (#4)',
async function () {
assert.equal(
findBefore(paragraph, paragraph.children[3], check),
undefined
)
}
)
})
/**
* @param {UnistNode} _
* @param {number | undefined} n
*/
function check(_, n) {
return n === 3
}