|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { mkdtemp, rm } from 'node:fs/promises'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 7 | + |
| 8 | +import { |
| 9 | + setConfig, |
| 10 | + default as getConfig, |
| 11 | +} from '@doc-kit/core/utils/configuration/index.mjs'; |
| 12 | + |
| 13 | +import { buildServer } from '../../../bundlers/vite.mjs'; |
| 14 | + |
| 15 | +const output = await mkdtemp(join(tmpdir(), 'doc-kit-render-label-test-')); |
| 16 | +const renderLabelModule = await buildServer({ |
| 17 | + entry: `export { renderLabel } from ${JSON.stringify( |
| 18 | + pathToFileURL(fileURLToPath(new URL('../renderLabel.jsx', import.meta.url))) |
| 19 | + .href |
| 20 | + )};`, |
| 21 | + virtualImports: {}, |
| 22 | + outDir: output, |
| 23 | + config: await setConfig({ |
| 24 | + target: ['html'], |
| 25 | + output, |
| 26 | + version: 'v22.0.0', |
| 27 | + changelog: [], |
| 28 | + generators: { |
| 29 | + html: {}, |
| 30 | + }, |
| 31 | + }).then(() => getConfig('html')), |
| 32 | +}); |
| 33 | +const { renderLabel } = await import(renderLabelModule); |
| 34 | + |
| 35 | +process.once('exit', () => rm(output, { recursive: true, force: true })); |
| 36 | + |
| 37 | +describe('renderLabel', () => { |
| 38 | + it('does not render elements for empty segments', () => { |
| 39 | + const result = renderLabel('`value`'); |
| 40 | + |
| 41 | + assert.equal(result.length, 3); |
| 42 | + assert.equal(result[0], null); |
| 43 | + assert.equal(result[1].type, 'code'); |
| 44 | + assert.equal(result[2], null); |
| 45 | + }); |
| 46 | + |
| 47 | + it('uses unique keys when segments have the same content', () => { |
| 48 | + const result = renderLabel('text `value` text `value`'); |
| 49 | + const elements = result.filter(Boolean); |
| 50 | + const keys = elements.map(segment => segment.key); |
| 51 | + |
| 52 | + assert.deepEqual(keys, [0, 1, 2, 3]); |
| 53 | + assert.deepEqual( |
| 54 | + elements.map(segment => [segment.type, segment.props.children]), |
| 55 | + [ |
| 56 | + ['span', 'text '], |
| 57 | + ['code', 'value'], |
| 58 | + ['span', ' text '], |
| 59 | + ['code', 'value'], |
| 60 | + ] |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
0 commit comments