-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathgenerate.test.mjs
More file actions
111 lines (93 loc) · 3.75 KB
/
Copy pathgenerate.test.mjs
File metadata and controls
111 lines (93 loc) · 3.75 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
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
import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, it } from 'node:test';
import { setConfig } from '../../../utils/configuration/index.mjs';
import buildContent from '../../jsx-ast/utils/buildContent.mjs';
import { buildNotFoundPage } from '../../jsx-ast/utils/synthetic/404.mjs';
import { generate } from '../generate.mjs';
const createEntry = (api, name, { depth = 1 } = {}) => {
const heading = {
type: 'heading',
depth,
children: [{ type: 'text', value: name }],
data: { name, text: name, slug: api },
};
return {
api,
path: `/${api}`,
basename: api,
heading,
stability: null,
content: {
type: 'root',
children: [
heading,
{
type: 'paragraph',
children: [{ type: 'text', value: `${name} body` }],
},
],
},
};
};
describe('web generate', () => {
it('omits View As links for synthetic pages only', async () => {
await setConfig({});
const fs = createEntry('fs', 'File system');
const notFoundPage = buildNotFoundPage();
const input = await Promise.all([
buildContent([fs], fs),
buildContent(notFoundPage.entries, notFoundPage.head),
]);
const [fsPage, notFoundResult] = await generate(input);
assert.match(fsPage.html, /View As/);
assert.match(fsPage.html, /href=fs\.json/);
assert.match(fsPage.html, /href=fs\.md/);
assert.doesNotMatch(notFoundResult.html, /View As/);
assert.doesNotMatch(notFoundResult.html, /href=404\.json/);
assert.doesNotMatch(notFoundResult.html, /href=404\.md/);
});
it('renders the configurable head without hardcoded defaults', async () => {
// `setConfig` resolves generator defaults; mutate the live config to apply
// per-generator overrides (the same object `getConfig('web')` returns).
const config = await setConfig({});
config.web.head = {
meta: [
{ name: 'description', content: 'Custom project docs' },
{ property: 'og:image', content: 'https://example.com/og.png' },
],
links: [{ rel: 'icon', href: 'https://example.com/favicon.ico' }],
html: ['<meta name="theme-color" content="#abcdef" />'],
};
const fs = createEntry('fs', 'File system');
const [fsPage] = await generate([await buildContent([fs], fs)]);
assert.match(fsPage.html, /Custom project docs/);
assert.match(fsPage.html, /https:\/\/example\.com\/og\.png/);
assert.match(fsPage.html, /href=https:\/\/example\.com\/favicon\.ico/);
assert.match(fsPage.html, /content=#abcdef/);
// Project-branding `head` config no longer leaks Node.js defaults.
assert.doesNotMatch(fsPage.html, /nodejs\.org/);
// Structural/theme tags stay hardcoded in the template regardless.
assert.match(fsPage.html, /property=og:type content=website/);
assert.match(fsPage.html, /href=https:\/\/fonts\.googleapis\.com/);
});
it('hydrates active status for right-side table of contents links', async () => {
const output = await mkdtemp(join(tmpdir(), 'doc-kit-web-'));
const config = await setConfig({});
config.web.output = output;
try {
const fs = createEntry('fs', 'File system');
const readFileEntry = createEntry('fs-read-file', 'File system readFile', { depth: 2 });
await generate([await buildContent([fs, readFileEntry], fs)]);
const clientBundle = await readFile(join(output, 'fs.js'), 'utf8');
for (const token of ['data-active-heading', 'aria-current', 'hashchange', 'scroll']) {
assert.match(clientBundle, new RegExp(token));
}
} finally {
config.web.output = undefined;
await rm(output, { recursive: true, force: true });
}
});
});