Skip to content

Commit 6093b77

Browse files
committed
Refactor tests to use improved types
1 parent 4df5d41 commit 6093b77

File tree

6 files changed

+98
-32
lines changed

6 files changed

+98
-32
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage/
55
node_modules/
66
yarn.lock
77
!/index.d.ts
8+
!/test/types.d.ts

lib/state.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ function applyData(from, to) {
276276

277277
// To do: next major: take the children from the `root`, or inject the
278278
// raw/text/comment or so into the element?
279-
// if ('children' in node) {
279+
// if ('children' in from) {
280280
// // @ts-expect-error: assume `children` are allowed in elements.
281-
// result.children = node.children
281+
// result.children = from.children
282282
// } else {
283283
// // @ts-expect-error: assume `node` is allowed in elements.
284-
// result.children.push(node)
284+
// result.children.push(from)
285285
// }
286286
}
287287
}

test/core.js

+16-21
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ test('toHast', async function (t) {
1919

2020
await t.test('should throw on non-nodes', async function () {
2121
assert.throws(function () {
22-
// @ts-expect-error: check that non-node children throw at runtime.
23-
toHast({type: 'bar', children: [true]})
22+
toHast({
23+
type: 'paragraph',
24+
children: [
25+
// @ts-expect-error: check that non-node children throw at runtime.
26+
true
27+
]
28+
})
2429
})
2530
})
2631

@@ -255,10 +260,8 @@ test('toHast', async function (t) {
255260
const customMdast = {
256261
type: 'paragraph',
257262
children: [
258-
// @ts-expect-error: check how a custom literal is handled.
259-
{type: 'a', value: 'alpha'},
260-
// @ts-expect-error: check how a custom parent is handled.
261-
{type: 'b', children: [{type: 'image', url: 'bravo'}]},
263+
{type: 'alpha', value: 'alpha'},
264+
{type: 'bravo', children: [{type: 'image', url: 'bravo'}]},
262265
{type: 'text', value: 'charlie'}
263266
]
264267
}
@@ -297,29 +300,21 @@ test('toHast', async function (t) {
297300
await t.test('should support `unknownHandler`', async function () {
298301
assert.deepEqual(
299302
toHast(customMdast, {
300-
// To do: improved test.
301-
// @ts-expect-error `hast` expected, but this returns unknown mdast nodes.
302-
unknownHandler(_, /** @type {Nodes} */ node) {
303-
return node
303+
unknownHandler() {
304+
return {type: 'text', value: 'unknown!'}
304305
}
305306
}),
306-
h('p', [
307-
{type: 'a', value: 'alpha'},
308-
// To do: register custom?
309-
// @ts-expect-error: custom.
310-
{type: 'b', children: [{type: 'image', url: 'bravo'}]},
311-
'charlie'
312-
])
307+
h('p', ['unknown!', 'unknown!', 'charlie'])
313308
)
314309
})
315310

316311
await t.test('should support `passThrough`', async function () {
317312
assert.deepEqual(
318-
toHast(customMdast, {passThrough: ['a', 'b']}),
313+
toHast(customMdast, {passThrough: ['alpha', 'bravo']}),
319314
h('p', [
320-
{type: 'a', value: 'alpha'},
321-
// @ts-expect-error: custom.
322-
{type: 'b', children: [h('img', {src: 'bravo'})]},
315+
{type: 'alpha', value: 'alpha'},
316+
// @ts-expect-error: to do: remove when `hastscript` is released.
317+
{type: 'bravo', children: [h('img', {src: 'bravo'})]},
323318
'charlie'
324319
])
325320
)

test/toml.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import {toHast} from '../index.js'
44

55
test('toml', async function (t) {
66
await t.test('should ignore `toml`', async function () {
7-
assert.deepEqual(
8-
// To do: register in test types.
9-
// @ts-expect-error: check how a `toml` node is handled (not registered
10-
// normally, but supported here).
11-
toHast({type: 'toml', value: 'alpha'}),
12-
{type: 'root', children: []}
13-
)
7+
assert.deepEqual(toHast({type: 'toml', value: 'alpha'}), {
8+
type: 'root',
9+
children: []
10+
})
1411
})
1512
})

test/types.d.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type {
2+
Data as DataHast,
3+
ElementContent as ElementContentHast,
4+
Literal as LiteralHast,
5+
Parent as ParentHast
6+
} from 'hast'
7+
import type {Data, Literal, Parent, PhrasingContent} from 'mdast'
8+
9+
interface Alpha extends Literal {
10+
type: 'alpha'
11+
data?: AlphaData | undefined
12+
}
13+
14+
interface AlphaData extends Data {}
15+
16+
interface Bravo extends Parent {
17+
type: 'bravo'
18+
children: PhrasingContent[]
19+
data?: BravoData | undefined
20+
}
21+
22+
interface BravoData extends Data {}
23+
24+
interface Toml extends Literal {
25+
type: 'toml'
26+
data?: TomlData | undefined
27+
}
28+
29+
interface TomlData extends Data {}
30+
31+
interface AlphaHast extends LiteralHast {
32+
type: 'alpha'
33+
data?: AlphaDataHast | undefined
34+
}
35+
36+
interface AlphaDataHast extends DataHast {}
37+
38+
interface BravoHast extends ParentHast {
39+
type: 'bravo'
40+
children: ElementContentHast[]
41+
data?: BravoDataHast | undefined
42+
}
43+
44+
interface BravoDataHast extends DataHast {}
45+
46+
declare module 'hast' {
47+
interface ElementContentMap {
48+
alpha: AlphaHast
49+
bravo: BravoHast
50+
}
51+
52+
interface RootContentMap {
53+
alpha: AlphaHast
54+
bravo: BravoHast
55+
}
56+
}
57+
58+
declare module 'mdast' {
59+
interface FrontmatterContentMap {
60+
toml: Toml
61+
}
62+
63+
interface PhrasingContentMap {
64+
alpha: Alpha
65+
bravo: Bravo
66+
}
67+
68+
interface RootContentMap {
69+
alpha: Alpha
70+
bravo: Bravo
71+
toml: Toml
72+
}
73+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2020"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js", "index.d.ts"]
14+
"include": ["**/*.js", "test/types.d.ts", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)