-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
115 lines (103 loc) · 3.07 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
/**
* @typedef {import('mdast').Root} Root
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {definitions} from 'mdast-util-definitions'
import {fromMarkdown} from 'mdast-util-from-markdown'
test('definitions', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(
Object.keys(await import('mdast-util-definitions')).sort(),
['definitions']
)
})
await t.test('should fail without node', async function () {
assert.throws(function () {
// @ts-expect-error: check that an error is thrown at runtime.
definitions()
}, /mdast-util-definitions expected node/)
})
await t.test('should return a definition', async function () {
assert.deepEqual(
definitions(from('[example]: https://example.com "Example"'))('example'),
{
type: 'definition',
identifier: 'example',
label: 'example',
title: 'Example',
url: 'https://example.com',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 41, offset: 40}
}
}
)
})
await t.test('should return `undefined` when not found', async function () {
assert.equal(
definitions(from('[example]: https://example.com "Example"'))('foo'),
undefined
)
})
await t.test('should work on weird identifiers', async function () {
assert.deepEqual(
definitions(from('[__proto__]: https://proto.com "Proto"'))('__proto__'),
{
type: 'definition',
identifier: '__proto__',
label: '__proto__',
title: 'Proto',
url: 'https://proto.com',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 1, column: 39, offset: 38}
}
}
)
})
await t.test('should not polute the prototype', async function () {
/* eslint-disable no-use-extend-native/no-use-extend-native */
// @ts-expect-error: yes.
// type-coverage:ignore-next-line
assert.equal({}.type, undefined)
/* eslint-enable no-use-extend-native/no-use-extend-native */
})
await t.test(
'should work on weird identifiers when not found',
async function () {
assert.deepEqual(
definitions(from('[__proto__]: https://proto.com "Proto"'))('toString'),
undefined
)
}
)
await t.test(
'should prefer the first of duplicate definitions',
async function () {
const example = definitions(
from('[example]: https://one.com\n[example]: https://two.com')
)('example')
assert.deepEqual(example && example.url, 'https://one.com')
}
)
await t.test(
'should not return something for a missing identifier',
async function () {
assert.deepEqual(
definitions(
from('[example]: https://one.com\n[example]: https://two.com')
)(''),
undefined
)
}
)
})
/**
* @param {string} value
* @returns {Root}
*/
function from(value) {
// @ts-expect-error: To do: remove cast when `from-markdown` is released.
return fromMarkdown(value)
}