|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import { describe, it, mock } from 'node:test'; |
| 2 | +import { describe, it } from 'node:test'; |
3 | 3 |
|
4 | | -import { SemVer } from 'semver'; |
5 | | - |
6 | | -import { setConfig } from '../../../../utils/configuration/index.mjs'; |
7 | | -import * as generatorsExports from '../../../../utils/generators.mjs'; |
8 | | - |
9 | | -mock.module('reading-time', { |
10 | | - defaultExport: () => ({ text: '5 min read' }), |
11 | | -}); |
12 | | - |
13 | | -mock.module('../../../../utils/generators.mjs', { |
14 | | - namedExports: { |
15 | | - ...generatorsExports, |
16 | | - getCompatibleVersions: () => [ |
17 | | - { version: '18.0.0', isLts: true, isCurrent: false }, |
18 | | - { version: '19.0.0', isLts: false, isCurrent: true }, |
19 | | - ], |
20 | | - leftHandAssign: Object.assign, |
21 | | - getVersionFromSemVer: version => `${version.major}.x`, |
22 | | - getVersionURL: (version, api) => `/api/${version}/${api}`, |
23 | | - }, |
24 | | -}); |
25 | | - |
26 | | -const { |
27 | | - extractTextContent, |
28 | | - buildMetaBarProps, |
29 | | - formatVersionOptions, |
30 | | - buildSideBarProps, |
31 | | -} = await import('../buildBarProps.mjs'); |
32 | | - |
33 | | -await setConfig({ |
34 | | - version: 'v17.0.0', |
35 | | - changelog: [ |
36 | | - { version: new SemVer('16.0.0'), isLts: true, isCurrent: false }, |
37 | | - { version: new SemVer('17.0.0'), isLts: false, isCurrent: true }, |
38 | | - ], |
39 | | -}); |
| 4 | +const { extractTextContent, extractHeadings } = |
| 5 | + await import('../buildBarProps.mjs'); |
40 | 6 |
|
41 | 7 | describe('extractTextContent', () => { |
42 | 8 | it('combines text and code node values from entries', () => { |
@@ -66,104 +32,55 @@ describe('extractTextContent', () => { |
66 | 32 | }); |
67 | 33 | }); |
68 | 34 |
|
69 | | -describe('buildMetaBarProps', () => { |
70 | | - it('creates meta bar properties from entries', () => { |
71 | | - const head = { |
72 | | - basename: 'fs', |
73 | | - path: '/fs', |
74 | | - added: 'v1.0.0', |
75 | | - }; |
76 | | - |
| 35 | +describe('extractHeadings', () => { |
| 36 | + it('extracts headings from entries that qualify for ToC', () => { |
77 | 37 | const entries = [ |
78 | 38 | { |
79 | | - content: { |
80 | | - type: 'root', |
81 | | - children: [{ type: 'text', value: 'Content' }], |
| 39 | + heading: { |
| 40 | + depth: 2, |
| 41 | + data: { |
| 42 | + text: 'fs.readFile(path)', |
| 43 | + name: 'readFile', |
| 44 | + slug: 'fs-readfile', |
| 45 | + type: 'method', |
| 46 | + }, |
82 | 47 | }, |
| 48 | + stability: { data: { index: '2' } }, |
| 49 | + }, |
| 50 | + { |
83 | 51 | heading: { |
84 | 52 | depth: 2, |
85 | 53 | data: { |
86 | | - text: 'Heading', |
87 | | - name: 'Heading', |
88 | | - slug: 'heading', |
89 | | - depth: 2, |
| 54 | + text: 'fs.writeFile(path)', |
| 55 | + name: 'writeFile', |
| 56 | + slug: 'fs-writefile', |
| 57 | + type: 'method', |
90 | 58 | }, |
91 | 59 | }, |
| 60 | + stability: null, |
92 | 61 | }, |
93 | 62 | ]; |
94 | 63 |
|
95 | | - const result = buildMetaBarProps(head, entries); |
96 | | - |
97 | | - assert.equal(result.addedIn, 'v1.0.0'); |
98 | | - assert.equal(result.readingTime, '5 min read'); |
99 | | - assert.deepEqual(result.viewAs, [ |
100 | | - ['JSON', 'fs.json'], |
101 | | - ['MD', 'fs.md'], |
102 | | - ]); |
103 | | - assert.equal( |
104 | | - result.editThisPage, |
105 | | - 'https://github.com/nodejs/node/edit/main/doc/api/fs.md' |
106 | | - ); |
107 | | - assert.ok(Array.isArray(result.headings)); |
108 | | - }); |
109 | | - |
110 | | - it('falls back to introduced_in if added is missing', () => { |
111 | | - const head = { |
112 | | - api: 'fs', |
113 | | - introduced_in: 'v2.0.0', |
114 | | - }; |
115 | | - |
116 | | - const entries = []; |
| 64 | + const result = extractHeadings(entries); |
117 | 65 |
|
118 | | - const result = buildMetaBarProps(head, entries); |
119 | | - assert.equal(result.addedIn, 'v2.0.0'); |
| 66 | + assert.equal(result.length, 2); |
| 67 | + assert.equal(result[0].slug, 'fs-readfile'); |
| 68 | + assert.equal(result[0].depth, 2); |
| 69 | + assert.equal(result[0].stability, 2); |
| 70 | + assert.equal(result[1].stability, 2); |
120 | 71 | }); |
121 | | -}); |
122 | | - |
123 | | -describe('formatVersionOptions', () => { |
124 | | - it('formats version options with proper labels', () => { |
125 | | - const versions = [ |
126 | | - { version: new SemVer('16.0.0'), isLts: true, isCurrent: false }, |
127 | | - { version: new SemVer('17.0.0'), isLts: false, isCurrent: true }, |
128 | | - { version: new SemVer('18.0.0'), isLts: false, isCurrent: false }, |
129 | | - ]; |
130 | 72 |
|
131 | | - const result = formatVersionOptions(versions, '/http'); |
132 | | - |
133 | | - assert.deepStrictEqual(result, [ |
134 | | - { |
135 | | - value: 'https://nodejs.org/docs/latest-v16.x/api/http.html', |
136 | | - label: 'v16.x (LTS)', |
137 | | - }, |
138 | | - { |
139 | | - value: 'https://nodejs.org/docs/latest-v17.x/api/http.html', |
140 | | - label: 'v17.x (Current)', |
141 | | - }, |
| 73 | + it('filters out entries with empty heading text', () => { |
| 74 | + const entries = [ |
142 | 75 | { |
143 | | - value: 'https://nodejs.org/docs/latest-v18.x/api/http.html', |
144 | | - label: 'v18.x', |
| 76 | + heading: { |
| 77 | + depth: 2, |
| 78 | + data: { text: '', name: '', slug: '', type: 'method' }, |
| 79 | + }, |
145 | 80 | }, |
146 | | - ]); |
147 | | - }); |
148 | | -}); |
149 | | - |
150 | | -describe('buildSideBarProps', () => { |
151 | | - it('creates sidebar properties with versions and navigation', () => { |
152 | | - const entry = { |
153 | | - path: 'http', |
154 | | - basename: 'http', |
155 | | - introduced_in: 'v0.10.0', |
156 | | - }; |
157 | | - |
158 | | - const docPages = [ |
159 | | - ['HTTP', 'http.html'], |
160 | | - ['HTTPS', 'https.html'], |
161 | 81 | ]; |
162 | 82 |
|
163 | | - const result = buildSideBarProps(entry, docPages); |
164 | | - |
165 | | - assert.equal(result.currentVersion, 'v17.0.0'); |
166 | | - assert.equal(result.pathname, 'http.html'); |
167 | | - assert.deepEqual(result.docPages, docPages); |
| 83 | + const result = extractHeadings(entries); |
| 84 | + assert.equal(result.length, 0); |
168 | 85 | }); |
169 | 86 | }); |
0 commit comments