|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { buildStabilityOverview } from '../index.mjs'; |
| 5 | + |
| 6 | +const fakeHead = (api, name, stabilityIndex, depth = 1) => ({ |
| 7 | + api, |
| 8 | + heading: { depth, data: { name, text: name, slug: api } }, |
| 9 | + stability: |
| 10 | + stabilityIndex == null |
| 11 | + ? null |
| 12 | + : { |
| 13 | + data: { |
| 14 | + index: String(stabilityIndex), |
| 15 | + description: `${name} stable. Long-form description.`, |
| 16 | + }, |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +const findChild = (node, tagName) => |
| 21 | + node.children.find(child => child.tagName === tagName); |
| 22 | + |
| 23 | +describe('buildStabilityOverview', () => { |
| 24 | + it('renders a header row and one body row per entry', () => { |
| 25 | + const table = buildStabilityOverview([ |
| 26 | + fakeHead('fs', 'fs', 2), |
| 27 | + fakeHead('crypto', 'crypto', 1), |
| 28 | + ]); |
| 29 | + |
| 30 | + assert.equal(table.tagName, 'table'); |
| 31 | + const headerRow = findChild(findChild(table, 'thead'), 'tr'); |
| 32 | + assert.deepEqual( |
| 33 | + headerRow.children.map(c => c.children[0].value), |
| 34 | + ['API', 'Stability'] |
| 35 | + ); |
| 36 | + |
| 37 | + assert.equal(findChild(table, 'tbody').children.length, 2); |
| 38 | + }); |
| 39 | + |
| 40 | + it('formats the stability cell as `(index) <first sentence>`', () => { |
| 41 | + const table = buildStabilityOverview([fakeHead('fs', 'fs', 1)]); |
| 42 | + |
| 43 | + const row = findChild(table, 'tbody').children[0]; |
| 44 | + const stabilityCell = row.children[1]; |
| 45 | + |
| 46 | + assert.equal(stabilityCell.children[0].value, '(1) fs stable'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('builds a relative link to the module HTML page', () => { |
| 50 | + const table = buildStabilityOverview([fakeHead('fs', 'fs', 2)]); |
| 51 | + |
| 52 | + const row = findChild(table, 'tbody').children[0]; |
| 53 | + const link = row.children[0].children[0]; |
| 54 | + |
| 55 | + assert.equal(link.tagName, 'a'); |
| 56 | + assert.equal(link.properties.href, 'fs.html'); |
| 57 | + assert.equal(link.children[0].value, 'fs'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders an empty body when no entries are passed', () => { |
| 61 | + const table = buildStabilityOverview([]); |
| 62 | + |
| 63 | + assert.equal(findChild(table, 'tbody').children.length, 0); |
| 64 | + }); |
| 65 | +}); |
0 commit comments