-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbuildBarProps.test.mjs
More file actions
169 lines (147 loc) · 4.22 KB
/
buildBarProps.test.mjs
File metadata and controls
169 lines (147 loc) · 4.22 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
import { SemVer } from 'semver';
import { setConfig } from '../../../../utils/configuration/index.mjs';
import * as generatorsExports from '../../../../utils/generators.mjs';
mock.module('reading-time', {
defaultExport: () => ({ text: '5 min read' }),
});
mock.module('../../../../utils/generators.mjs', {
namedExports: {
...generatorsExports,
getCompatibleVersions: () => [
{ version: '18.0.0', isLts: true, isCurrent: false },
{ version: '19.0.0', isLts: false, isCurrent: true },
],
leftHandAssign: Object.assign,
getVersionFromSemVer: version => `${version.major}.x`,
getVersionURL: (version, api) => `/api/${version}/${api}`,
},
});
const {
extractTextContent,
buildMetaBarProps,
formatVersionOptions,
buildSideBarProps,
} = await import('../buildBarProps.mjs');
await setConfig({
version: 'v17.0.0',
changelog: [
{ version: new SemVer('16.0.0'), isLts: true, isCurrent: false },
{ version: new SemVer('17.0.0'), isLts: false, isCurrent: true },
],
});
describe('extractTextContent', () => {
it('combines text and code node values from entries', () => {
const entries = [
{
content: {
type: 'root',
children: [
{ type: 'text', value: 'Hello ' },
{ type: 'code', value: 'world' },
],
},
},
{
content: {
type: 'root',
children: [
{ type: 'text', value: 'Another ' },
{ type: 'code', value: 'example' },
],
},
},
];
const result = extractTextContent(entries);
assert.equal(result, 'Hello worldAnother example');
});
});
describe('buildMetaBarProps', () => {
it('creates meta bar properties from entries', () => {
const head = {
basename: 'fs',
path: '/fs',
added: 'v1.0.0',
};
const entries = [
{
content: {
type: 'root',
children: [{ type: 'text', value: 'Content' }],
},
heading: {
depth: 2,
data: {
text: 'Heading',
name: 'Heading',
slug: 'heading',
depth: 2,
},
},
},
];
const result = buildMetaBarProps(head, entries);
assert.equal(result.addedIn, 'v1.0.0');
assert.equal(result.readingTime, '5 min read');
assert.deepEqual(result.viewAs, [
['JSON', 'fs.json'],
['MD', 'fs.md'],
]);
assert.equal(
result.editThisPage,
'https://github.com/nodejs/node/edit/main/doc/api/fs.md'
);
assert.ok(Array.isArray(result.headings));
});
it('falls back to introduced_in if added is missing', () => {
const head = {
api: 'fs',
introduced_in: 'v2.0.0',
};
const entries = [];
const result = buildMetaBarProps(head, entries);
assert.equal(result.addedIn, 'v2.0.0');
});
});
describe('formatVersionOptions', () => {
it('formats version options with proper labels', () => {
const versions = [
{ version: new SemVer('16.0.0'), isLts: true, isCurrent: false },
{ version: new SemVer('17.0.0'), isLts: false, isCurrent: true },
{ version: new SemVer('18.0.0'), isLts: false, isCurrent: false },
];
const result = formatVersionOptions(versions, '/http');
assert.deepStrictEqual(result, [
{
value: 'https://nodejs.org/docs/latest-v16.x/api/http.html',
label: 'v16.x (LTS)',
},
{
value: 'https://nodejs.org/docs/latest-v17.x/api/http.html',
label: 'v17.x (Current)',
},
{
value: 'https://nodejs.org/docs/latest-v18.x/api/http.html',
label: 'v18.x',
},
]);
});
});
describe('buildSideBarProps', () => {
it('creates sidebar properties with versions and navigation', () => {
const entry = {
path: 'http',
basename: 'http',
introduced_in: 'v0.10.0',
};
const docPages = [
['HTTP', 'http.html'],
['HTTPS', 'https.html'],
];
const result = buildSideBarProps(entry, docPages);
assert.equal(result.currentVersion, 'v17.0.0');
assert.equal(result.pathname, 'http.html');
assert.deepEqual(result.docPages, docPages);
});
});