-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathutils.test.mjs
More file actions
80 lines (71 loc) · 2.61 KB
/
utils.test.mjs
File metadata and controls
80 lines (71 loc) · 2.61 KB
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
import test from 'node:test';
import assert from 'node:assert/strict';
import {
buildSideBarDocPages,
buildMetaBarProps,
} from '../utils/buildBarProps.mjs';
import buildContent from '../utils/buildContent.mjs';
import { createJSXElement } from '../utils/ast.mjs';
import { AST_NODE_TYPES } from '../constants.mjs';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
const sampleEntry = {
api: 'sample-api',
heading: {
depth: 2,
data: { name: 'SampleFunc', slug: 'sample-func', type: 'function' },
},
content: {
type: 'root',
children: [
{ type: 'text', value: 'Example text for testing reading time.' },
],
},
added_in: 'v1.0.0',
source_link: '/src/index.js',
changes: [
{
version: 'v1.1.0',
description: 'Improved performance',
'pr-url': 'https://github.com/org/repo/pull/123',
},
],
};
test('buildSideBarDocPages returns expected format', () => {
const grouped = new Map([['sample-api', [sampleEntry]]]);
const result = buildSideBarDocPages(grouped, [sampleEntry]);
assert.equal(result.length, 1);
assert.equal(result[0].title, 'SampleFunc');
assert.equal(result[0].doc, 'sample-api.html');
assert.deepEqual(result[0].headings, [['SampleFunc', '#sample-func']]);
});
test('buildMetaBarProps includes expected fields', () => {
const result = buildMetaBarProps(sampleEntry, [sampleEntry]);
assert.equal(result.addedIn, 'v1.0.0');
assert.deepEqual(result.viewAs, [['JSON', 'sample-api.json']]);
assert.ok(result.readingTime.startsWith('1 min'));
assert.ok(result.editThisPage.endsWith('sample-api.md'));
assert.deepEqual(result.headings, [{ depth: 2, value: 'SampleFunc' }]);
});
test('createJSXElement builds correct JSX tree', () => {
const el = createJSXElement('TestComponent', {
inline: false,
children: 'Some content',
dataAttr: { test: true },
});
assert.equal(el.type, AST_NODE_TYPES.MDX.JSX_BLOCK_ELEMENT);
assert.equal(el.name, 'TestComponent');
assert.ok(Array.isArray(el.children));
assert.ok(el.attributes.some(attr => attr.name === 'dataAttr'));
});
test('buildContent processes entries and includes JSX wrapper elements', () => {
const processor = unified().use(remarkParse).use(remarkStringify);
const tree = buildContent([sampleEntry], sampleEntry, {}, processor);
const article = tree.children.find(
child => child.name === AST_NODE_TYPES.JSX.ARTICLE
);
assert.ok(article);
assert.ok(article.children.some(c => c.name === AST_NODE_TYPES.JSX.SIDE_BAR));
assert.ok(article.children.some(c => c.name === AST_NODE_TYPES.JSX.FOOTER));
});