Skip to content

Commit e9a59cb

Browse files
committed
Refactor code-style in tests
1 parent 1f2dfb5 commit e9a59cb

File tree

1 file changed

+113
-157
lines changed

1 file changed

+113
-157
lines changed

test/index.js

Lines changed: 113 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,16 @@ import {toMarkdown} from 'mdast-util-to-markdown'
1515
import {gfm} from 'micromark-extension-gfm'
1616
import {u} from 'unist-builder'
1717
import {removePosition} from 'unist-util-remove-position'
18-
import {defaultHandlers, toMdast} from '../index.js'
19-
import {wrapNeeded} from '../lib/util/wrap.js'
18+
import {toMdast} from '../index.js'
19+
import * as mod from '../index.js'
2020

21-
test('custom nodes', (t) => {
21+
test('core', (t) => {
2222
t.deepEqual(
23-
wrapNeeded([
24-
{type: 'text', value: 'some '},
25-
{
26-
// @ts-expect-error - custom node type
27-
type: 'superscript',
28-
data: {hName: 'sup'},
29-
children: [{type: 'text', value: 'test'}]
30-
},
31-
{type: 'text', value: ' text'}
32-
]),
33-
wrapNeeded([
34-
{type: 'text', value: 'some '},
35-
{
36-
type: 'emphasis',
37-
children: [{type: 'text', value: 'test'}]
38-
},
39-
{type: 'text', value: ' text'}
40-
]),
41-
'should support `node.data.hName` to infer phrasing'
23+
Object.keys(mod).sort(),
24+
['defaultHandlers', 'defaultNodeHandlers', 'toMdast'],
25+
'should expose the public api'
4226
)
4327

44-
t.end()
45-
})
46-
47-
test('exports', (t) => {
48-
t.ok(defaultHandlers, 'should export `defaultHandlers`')
49-
t.end()
50-
})
51-
52-
test('core', (t) => {
5328
t.deepEqual(
5429
toMdast(u('root', [h('strong', 'Alpha')])),
5530
u('root', [u('strong', [u('text', 'Alpha')])]),
@@ -183,6 +158,105 @@ test('core', (t) => {
183158
'should support an `element` node w/o `children`'
184159
)
185160

161+
t.deepEqual(
162+
toMdast(h(null, [h('div', 'Alpha')]), {
163+
handlers: {
164+
div() {
165+
return {type: 'paragraph', children: [{type: 'text', value: 'Beta'}]}
166+
}
167+
}
168+
}),
169+
u('root', [u('paragraph', [u('text', 'Beta')])]),
170+
'should support `handlers`'
171+
)
172+
173+
t.deepEqual(
174+
toMdast(h(null, h('p', 'Alpha\nBeta'))),
175+
u('root', [u('paragraph', [u('text', 'Alpha Beta')])]),
176+
'should collapse newline to a single space'
177+
)
178+
179+
t.deepEqual(
180+
toMdast(h(null, h('p', 'Alpha\nBeta')), {newlines: true}),
181+
u('root', [u('paragraph', [u('text', 'Alpha\nBeta')])]),
182+
'should support `newlines: true`'
183+
)
184+
185+
const phrasingTree = h(null, [
186+
h('b', 'Importance'),
187+
' and ',
188+
h('i', 'emphasis'),
189+
'.'
190+
])
191+
192+
t.deepEqual(
193+
toMdast(phrasingTree),
194+
u('root', [
195+
u('strong', [u('text', 'Importance')]),
196+
u('text', ' and '),
197+
u('emphasis', [u('text', 'emphasis')]),
198+
u('text', '.')
199+
]),
200+
'should infer document if not needed'
201+
)
202+
203+
t.deepEqual(
204+
toMdast(phrasingTree, {document: true}),
205+
u('root', [
206+
u('paragraph', [
207+
u('strong', [u('text', 'Importance')]),
208+
u('text', ' and '),
209+
u('emphasis', [u('text', 'emphasis')]),
210+
u('text', '.')
211+
])
212+
]),
213+
'should support an explicit `document: true`'
214+
)
215+
216+
const referenceTree = toMdast(h(null, ['some ', h('sup', 'test'), ' text']), {
217+
handlers: {
218+
// @ts-expect-error - custom node type
219+
sup(state, element) {
220+
return {
221+
type: 'superscript',
222+
data: {},
223+
children: state.all(element)
224+
}
225+
}
226+
}
227+
})
228+
const explicitTree = toMdast(h(null, ['some ', h('sup', 'test'), ' text']), {
229+
handlers: {
230+
// @ts-expect-error - custom node type
231+
sup(state, element) {
232+
return {
233+
type: 'superscript',
234+
data: {hName: 'sup'},
235+
children: state.all(element)
236+
}
237+
}
238+
}
239+
})
240+
241+
// Reference check: `text`s are wrapped in `paragraph`s because the unknown
242+
// node is seen as “block”
243+
t.deepEqual(
244+
referenceTree.type === 'root' &&
245+
referenceTree.children.length === 3 &&
246+
referenceTree.children[0].type === 'paragraph',
247+
true,
248+
'should support `node.data.hName` to infer phrasing (1)'
249+
)
250+
251+
// Actual check: no `paragraph` is added because `hName` is added.
252+
t.deepEqual(
253+
explicitTree.type === 'root' &&
254+
explicitTree.children.length === 3 &&
255+
explicitTree.children[0].type !== 'paragraph',
256+
true,
257+
'should support `node.data.hName` to infer phrasing (2)'
258+
)
259+
186260
t.end()
187261
})
188262

@@ -195,7 +269,7 @@ test('fixtures', async (t) => {
195269
continue
196270
}
197271

198-
t.test(folder, async (st) => {
272+
t.test(folder, async (t) => {
199273
const configUrl = new URL(folder + '/index.json', fixtures)
200274
const inputUrl = new URL(folder + '/index.html', fixtures)
201275
const expectedUrl = new URL(folder + '/index.md', fixtures)
@@ -213,12 +287,13 @@ test('fixtures', async (t) => {
213287
const mdast = toMdast(hast, config)
214288
removePosition(mdast, true)
215289

216-
st.doesNotThrow(() => {
290+
t.doesNotThrow(() => {
217291
mdastAssert(mdast)
218292
}, 'should produce valid mdast nodes')
219293

220-
if (/^base\b/.test(folder)) {
221-
st.end()
294+
// Ignore the invalid base test.
295+
if (folder === 'base-invalid') {
296+
t.end()
222297
return
223298
}
224299

@@ -241,7 +316,7 @@ test('fixtures', async (t) => {
241316
}
242317

243318
if (!config || config.stringify !== false) {
244-
st.deepEqual(actual, expected, 'should produce the same documents')
319+
t.deepEqual(actual, expected, 'should produce the same documents')
245320
}
246321

247322
if (!config || config.tree !== false) {
@@ -250,135 +325,16 @@ test('fixtures', async (t) => {
250325
mdastExtensions: [gfmFromMarkdown()]
251326
})
252327
removePosition(expectedMdast, true)
253-
st.deepEqual(
328+
t.deepEqual(
254329
mdast,
255330
expectedMdast,
256331
'should produce the same tree as remark'
257332
)
258333
}
259334

260-
st.end()
335+
t.end()
261336
})
262337
}
263338

264339
t.end()
265340
})
266-
267-
test('handlers option', (t) => {
268-
/** @type {Options} */
269-
const options = {
270-
handlers: {
271-
div() {
272-
return {type: 'paragraph', children: [{type: 'text', value: 'Beta'}]}
273-
}
274-
}
275-
}
276-
277-
t.deepEqual(
278-
toMdast(
279-
{
280-
type: 'root',
281-
children: [
282-
{
283-
type: 'element',
284-
tagName: 'div',
285-
properties: {},
286-
children: [{type: 'text', value: 'Alpha'}]
287-
}
288-
]
289-
},
290-
options
291-
),
292-
{
293-
type: 'root',
294-
children: [{type: 'paragraph', children: [{type: 'text', value: 'Beta'}]}]
295-
},
296-
'use handlers passed as option'
297-
)
298-
299-
t.end()
300-
})
301-
302-
test('document option', (t) => {
303-
const tree = u('root', [
304-
h('b', 'Importance'),
305-
u('text', ' and '),
306-
h('i', 'emphasis'),
307-
u('text', '.')
308-
])
309-
310-
t.deepEqual(
311-
toMdast(tree),
312-
u('root', [
313-
u('strong', [u('text', 'Importance')]),
314-
u('text', ' and '),
315-
u('emphasis', [u('text', 'emphasis')]),
316-
u('text', '.')
317-
]),
318-
'should infer document if not needed'
319-
)
320-
321-
t.deepEqual(
322-
toMdast(tree, {document: true}),
323-
u('root', [
324-
u('paragraph', [
325-
u('strong', [u('text', 'Importance')]),
326-
u('text', ' and '),
327-
u('emphasis', [u('text', 'emphasis')]),
328-
u('text', '.')
329-
])
330-
]),
331-
'should support an explicit `document: true`'
332-
)
333-
334-
t.end()
335-
})
336-
337-
test('newlines option', (t) => {
338-
t.deepEqual(
339-
toMdast({
340-
type: 'root',
341-
children: [
342-
{
343-
type: 'element',
344-
tagName: 'p',
345-
properties: {},
346-
children: [{type: 'text', value: 'Alpha\nBeta'}]
347-
}
348-
]
349-
}),
350-
{
351-
type: 'root',
352-
children: [
353-
{type: 'paragraph', children: [{type: 'text', value: 'Alpha Beta'}]}
354-
]
355-
},
356-
'should collapse newline to a single space'
357-
)
358-
359-
t.deepEqual(
360-
toMdast(
361-
{
362-
type: 'root',
363-
children: [
364-
{
365-
type: 'element',
366-
tagName: 'p',
367-
properties: {},
368-
children: [{type: 'text', value: 'Alpha\nBeta'}]
369-
}
370-
]
371-
},
372-
{newlines: true}
373-
),
374-
{
375-
type: 'root',
376-
children: [
377-
{type: 'paragraph', children: [{type: 'text', value: 'Alpha\nBeta'}]}
378-
]
379-
},
380-
'should contain newlines'
381-
)
382-
383-
t.end()
384-
})

0 commit comments

Comments
 (0)