|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import getConfig, { setConfig } from '../../../utils/configuration/index.mjs'; |
| 5 | +import { generate, processChunk } from '../generate.mjs'; |
| 6 | + |
| 7 | +const createEntry = (api, name, { stabilityIndex = '2' } = {}) => { |
| 8 | + const heading = { |
| 9 | + type: 'heading', |
| 10 | + depth: 1, |
| 11 | + children: [{ type: 'text', value: name }], |
| 12 | + data: { name, text: name, slug: api }, |
| 13 | + }; |
| 14 | + |
| 15 | + return { |
| 16 | + api, |
| 17 | + path: `/${api}`, |
| 18 | + basename: api, |
| 19 | + heading, |
| 20 | + stability: |
| 21 | + stabilityIndex == null |
| 22 | + ? null |
| 23 | + : { |
| 24 | + data: { |
| 25 | + index: stabilityIndex, |
| 26 | + description: `${name} stable. Longer description.`, |
| 27 | + }, |
| 28 | + }, |
| 29 | + content: { |
| 30 | + type: 'root', |
| 31 | + children: [ |
| 32 | + heading, |
| 33 | + { |
| 34 | + type: 'paragraph', |
| 35 | + children: [{ type: 'text', value: `${name} body` }], |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +const collect = async generator => { |
| 43 | + const results = []; |
| 44 | + |
| 45 | + for await (const chunk of generator) { |
| 46 | + results.push(...chunk); |
| 47 | + } |
| 48 | + |
| 49 | + return results; |
| 50 | +}; |
| 51 | + |
| 52 | +const createWorker = seenItems => ({ |
| 53 | + async *stream(items) { |
| 54 | + seenItems.push(...items); |
| 55 | + yield items.map(({ head }) => ({ type: 'JSXElement', data: head })); |
| 56 | + }, |
| 57 | +}); |
| 58 | + |
| 59 | +describe('jsx-ast generate', () => { |
| 60 | + it('does not attach raw section entries to regular JSX content', async () => { |
| 61 | + await setConfig({}); |
| 62 | + |
| 63 | + const fs = createEntry('fs', 'File system'); |
| 64 | + const [content] = await processChunk([{ head: fs, entries: [fs] }], [0]); |
| 65 | + |
| 66 | + assert.equal(content.data.api, 'fs'); |
| 67 | + assert.equal('sectionEntries' in content, false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('respects jsx-ast synthetic page flags', async () => { |
| 71 | + await setConfig({}); |
| 72 | + |
| 73 | + const jsxAstConfig = getConfig('jsx-ast'); |
| 74 | + jsxAstConfig.generateAllPage = false; |
| 75 | + jsxAstConfig.generateIndexPage = false; |
| 76 | + jsxAstConfig.generateNotFoundPage = false; |
| 77 | + |
| 78 | + const seenItems = []; |
| 79 | + const results = await collect( |
| 80 | + generate( |
| 81 | + [createEntry('index', 'Index'), createEntry('fs', 'File system')], |
| 82 | + createWorker(seenItems) |
| 83 | + ) |
| 84 | + ); |
| 85 | + |
| 86 | + assert.deepEqual( |
| 87 | + seenItems.map(({ head }) => head.api), |
| 88 | + ['index', 'fs'] |
| 89 | + ); |
| 90 | + assert.deepEqual( |
| 91 | + results.map(({ data }) => data.api), |
| 92 | + ['index', 'fs'] |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments