|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | + |
| 3 | +import { labelTable } from './table-stack'; |
| 4 | + |
| 5 | +interface FakeCell { |
| 6 | + colSpan: number; |
| 7 | + textContent: string; |
| 8 | + dataset: Record<string, string>; |
| 9 | + attributes: Map<string, string>; |
| 10 | + hasAttribute(name: string): boolean; |
| 11 | + setAttribute(name: string, value: string): void; |
| 12 | +} |
| 13 | + |
| 14 | +function cell(text = '', colSpan = 1, dataset: Record<string, string> = {}): FakeCell { |
| 15 | + const attributes = new Map<string, string>(); |
| 16 | + return { |
| 17 | + colSpan, |
| 18 | + textContent: text, |
| 19 | + dataset, |
| 20 | + attributes, |
| 21 | + hasAttribute: (name) => attributes.has(name), |
| 22 | + setAttribute: (name, value) => void attributes.set(name, value), |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function table(headers: FakeCell[], rows: FakeCell[][]): HTMLTableElement { |
| 27 | + return { |
| 28 | + tHead: { rows: [{ cells: headers }] }, |
| 29 | + tBodies: [{ rows: rows.map((cells) => ({ cells })) }], |
| 30 | + } as unknown as HTMLTableElement; |
| 31 | +} |
| 32 | + |
| 33 | +describe('labelTable', () => { |
| 34 | + test('labels each cell with its column header', () => { |
| 35 | + const name = cell('Acme'); |
| 36 | + const status = cell('Public'); |
| 37 | + labelTable(table([cell(' Product \n'), cell('Visibility')], [[name, status]])); |
| 38 | + expect(name.attributes.get('data-label')).toBe('Product'); |
| 39 | + expect(status.attributes.get('data-label')).toBe('Visibility'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('leaves full-width cells and empty headers unlabelled', () => { |
| 43 | + const empty = cell('No products yet', 2); |
| 44 | + const actions = cell('Edit'); |
| 45 | + labelTable(table([cell('Product'), cell('')], [[empty], [cell('Acme'), actions]])); |
| 46 | + expect(empty.attributes.has('data-label')).toBe(false); |
| 47 | + expect(actions.attributes.has('data-label')).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test('counts header colspans so later cells keep their column', () => { |
| 51 | + const last = cell('3 days'); |
| 52 | + labelTable(table([cell('Name', 2), cell('Age')], [[cell('a'), cell('b'), last]])); |
| 53 | + expect(last.attributes.get('data-label')).toBe('Age'); |
| 54 | + }); |
| 55 | + |
| 56 | + test("keeps a label the template already set, and honours a header's own label", () => { |
| 57 | + const own = cell('x'); |
| 58 | + own.attributes.set('data-label', 'Custom'); |
| 59 | + const fromHeader = cell('y'); |
| 60 | + labelTable(table([cell('A'), cell('Sort', 1, { label: 'Severity' })], [[own, fromHeader]])); |
| 61 | + expect(own.attributes.get('data-label')).toBe('Custom'); |
| 62 | + expect(fromHeader.attributes.get('data-label')).toBe('Severity'); |
| 63 | + }); |
| 64 | + |
| 65 | + test('skips an empty header row a template left before the real one', () => { |
| 66 | + const value = cell('14 days'); |
| 67 | + const stray = { cells: [] }; |
| 68 | + const header = { cells: [cell('Patch SLA')] }; |
| 69 | + labelTable({ |
| 70 | + tHead: { rows: [stray, header] }, |
| 71 | + tBodies: [{ rows: [{ cells: [value] }] }], |
| 72 | + } as unknown as HTMLTableElement); |
| 73 | + expect(value.attributes.get('data-label')).toBe('Patch SLA'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments