-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
53 lines (45 loc) · 1.4 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
/**
* @typedef {import('mdast').Root} Root
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {u} from 'unist-builder'
import {map} from 'unist-util-map'
test('map', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('unist-util-map')).sort(), [
'map'
])
})
await t.test('should map the specified node', async function () {
/** @type {Root} */
const tree = u('root', [u('paragraph', [u('text', '1')]), u('text', '2')])
assert.deepEqual(
map(tree, changeLeaf),
u('root', [u('paragraph', [u('text', 'CHANGED')]), u('text', 'CHANGED')])
)
})
await t.test('should map the specified node', async function () {
/** @type {Root} */
const tree = u('root', [u('paragraph', [u('text', '1')]), u('text', '2')])
assert.deepEqual(
map(tree, changeLeaf),
u('root', [u('paragraph', [u('text', 'CHANGED')]), u('text', 'CHANGED')])
)
})
await t.test('should work when passing an empty object', async function () {
assert.deepEqual(
// @ts-expect-error: check how not-a-node is handled.
map({}, function () {
return {value: 'test'}
}),
{value: 'test'}
)
})
})
/**
* @type {import('./index.js').MapFunction<Root>}
*/
function changeLeaf(node) {
return node.type === 'text' ? {...node, value: 'CHANGED'} : node
}