Skip to content

Commit 4e8e670

Browse files
committed
Refactor code-style
1 parent fc9e252 commit 4e8e670

File tree

2 files changed

+92
-115
lines changed

2 files changed

+92
-115
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* This is because ATX headings with a depth of three or more could be
1515
* considered setext.
1616
*
17-
* @param {Heading} node
17+
* @param {Readonly<Heading>} node
1818
* Heading node to check.
1919
* @param {Style | null | undefined} [relative]
20-
* Relative style.
20+
* Relative style (preferred style).
2121
* @returns {Style | null}
2222
* Style, if it can be inferred, `null` otherwise.
2323
*/

test.js

Lines changed: 90 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,102 @@
11
/**
2-
* @typedef {import('mdast').Root} Root
32
* @typedef {import('mdast').Heading} Heading
43
*/
54

65
import assert from 'node:assert/strict'
76
import test from 'node:test'
87
import {fromMarkdown} from 'mdast-util-from-markdown'
98
import {headingStyle} from './index.js'
10-
import * as mod from './index.js'
119

12-
test('headingStyle', () => {
13-
assert.deepEqual(
14-
Object.keys(mod).sort(),
15-
['headingStyle'],
16-
'should expose the public api'
17-
)
18-
19-
assert.throws(() => {
20-
// @ts-ignore runtime.
21-
headingStyle()
22-
}, 'should fail without node')
23-
24-
assert.equal(
25-
// @ts-ignore runtime.
26-
headingStyle({
27-
type: 'heading',
28-
children: [{type: 'text', value: 'foo'}]
29-
}),
30-
null,
31-
'should NOT fail on undetectable nodes'
32-
)
33-
34-
assert.equal(
35-
headingStyle(parseFirstNode('# ATX')),
36-
'atx',
37-
'should detect atx'
38-
)
39-
40-
assert.equal(
41-
headingStyle(parseFirstNode('# ATX #')),
42-
'atx-closed',
43-
'should detect closed atx'
44-
)
45-
46-
assert.equal(
47-
headingStyle(parseFirstNode('ATX\n===')),
48-
'setext',
49-
'should detect closed setext'
50-
)
51-
52-
assert.equal(
53-
headingStyle(parseFirstNode('### ATX')),
54-
null,
55-
'should work on ambiguous nodes'
56-
)
57-
58-
assert.equal(
59-
headingStyle(parseFirstNode('### ATX'), 'atx'),
60-
'atx',
61-
'should work on ambiguous nodes (preference to atx)'
62-
)
63-
64-
assert.equal(
65-
headingStyle(parseFirstNode('### ATX'), 'setext'),
66-
'setext',
67-
'should work on ambiguous nodes (preference to setext)'
68-
)
69-
70-
assert.equal(
71-
headingStyle(parseFirstNode('###### ######')),
72-
'atx-closed',
73-
'should work on empty nodes (#1)'
74-
)
75-
76-
assert.equal(
77-
headingStyle(parseFirstNode('### ###')),
78-
'atx-closed',
79-
'should work on empty nodes (#2)'
80-
)
81-
82-
assert.equal(
83-
headingStyle(parseFirstNode('# #')),
84-
'atx-closed',
85-
'should work on empty nodes (#3)'
86-
)
87-
88-
assert.equal(
89-
headingStyle(parseFirstNode('###### '), 'atx'),
90-
'atx',
91-
'should work on empty nodes (#4)'
92-
)
93-
94-
assert.equal(
95-
headingStyle(parseFirstNode('### '), 'atx'),
96-
'atx',
97-
'should work on empty nodes (#5)'
98-
)
99-
100-
assert.equal(
101-
headingStyle(parseFirstNode('## ')),
102-
'atx',
103-
'should work on empty nodes (#6)'
104-
)
105-
106-
assert.equal(
107-
headingStyle(parseFirstNode('###### '), 'setext'),
108-
'setext',
109-
'should work on empty nodes (#7)'
110-
)
111-
112-
assert.equal(
113-
headingStyle(parseFirstNode('### '), 'setext'),
114-
'setext',
115-
'should work on empty nodes (#8)'
116-
)
117-
118-
assert.equal(
119-
headingStyle(parseFirstNode('## '), 'setext'),
120-
'atx',
121-
'should work on empty nodes (#9)'
122-
)
10+
test('headingStyle', async function (t) {
11+
await t.test('should expose the public api', async function () {
12+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
13+
'headingStyle'
14+
])
15+
})
16+
17+
await t.test('should fail without node', async function () {
18+
assert.throws(function () {
19+
// @ts-expect-error: check that an error is thrown at runtime.
20+
headingStyle()
21+
})
22+
})
23+
24+
await t.test('should NOT fail on undetectable nodes', async function () {
25+
assert.equal(
26+
// @ts-expect-error: check that an error is thrown at runtime.
27+
headingStyle({
28+
type: 'heading',
29+
children: [{type: 'text', value: 'foo'}]
30+
}),
31+
null
32+
)
33+
})
34+
35+
await t.test('should detect atx', async function () {
36+
assert.equal(headingStyle(parseFirstNode('# ATX')), 'atx')
37+
})
38+
39+
await t.test('should detect closed atx', async function () {
40+
assert.equal(headingStyle(parseFirstNode('# ATX #')), 'atx-closed')
41+
})
42+
43+
await t.test('should detect closed setext', async function () {
44+
assert.equal(headingStyle(parseFirstNode('ATX\n===')), 'setext')
45+
})
46+
47+
await t.test('should work on ambiguous nodes', async function () {
48+
assert.equal(headingStyle(parseFirstNode('### ATX')), null)
49+
})
50+
51+
await t.test(
52+
'should work on ambiguous nodes (preference to atx)',
53+
async function () {
54+
assert.equal(headingStyle(parseFirstNode('### ATX'), 'atx'), 'atx')
55+
}
56+
)
57+
58+
await t.test(
59+
'should work on ambiguous nodes (preference to setext)',
60+
async function () {
61+
assert.equal(headingStyle(parseFirstNode('### ATX'), 'setext'), 'setext')
62+
}
63+
)
64+
65+
await t.test('should work on empty nodes (#1)', async function () {
66+
assert.equal(headingStyle(parseFirstNode('###### ######')), 'atx-closed')
67+
})
68+
69+
await t.test('should work on empty nodes (#2)', async function () {
70+
assert.equal(headingStyle(parseFirstNode('### ###')), 'atx-closed')
71+
})
72+
73+
await t.test('should work on empty nodes (#3)', async function () {
74+
assert.equal(headingStyle(parseFirstNode('# #')), 'atx-closed')
75+
})
76+
77+
await t.test('should work on empty nodes (#4)', async function () {
78+
assert.equal(headingStyle(parseFirstNode('###### '), 'atx'), 'atx')
79+
})
80+
81+
await t.test('should work on empty nodes (#5)', async function () {
82+
assert.equal(headingStyle(parseFirstNode('### '), 'atx'), 'atx')
83+
})
84+
85+
await t.test('should work on empty nodes (#6)', async function () {
86+
assert.equal(headingStyle(parseFirstNode('## ')), 'atx')
87+
})
88+
89+
await t.test('should work on empty nodes (#7)', async function () {
90+
assert.equal(headingStyle(parseFirstNode('###### '), 'setext'), 'setext')
91+
})
92+
93+
await t.test('should work on empty nodes (#8)', async function () {
94+
assert.equal(headingStyle(parseFirstNode('### '), 'setext'), 'setext')
95+
})
96+
97+
await t.test('should work on empty nodes (#9)', async function () {
98+
assert.equal(headingStyle(parseFirstNode('## '), 'setext'), 'atx')
99+
})
123100
})
124101

125102
/**

0 commit comments

Comments
 (0)