package file→ .shtukbase/packages/shtukbase-ui-react/test/parity.test.ts
test/parity.test.ts
49 lines, 2.5 KB.
- // The React library against the HTML library: the same ids, the same headers, and for every state and status of every
- // component the React markup carries the markup of the HTML template (tags, text, attributes, classes, custom properties).
- // The HTML library is the package shtukbase-ui-html (task DSN-4); these tests read its component files from there.
- import { describe, expect, test } from 'bun:test';
- import { HTML_COMPONENTS, LEVEL_DIRS, compareFragment, htmlComponents, libraryComponents, load, renderState } from './lib.ts';
- const html = htmlComponents();
- // The package shtukbase-ui-html is built in parallel (DSN-4); until its components are on main there is nothing to compare with.
- // Set SB_UI_HTML_COMPONENTS to the folder of its components to run these tests against a working copy.
- if (html.length === 0) test.todo(`compare with the HTML library: no components found in ${HTML_COMPONENTS}`);
- describe.skipIf(html.length === 0)('the same set of components', () => {
- const mine = libraryComponents().map((c) => c.id).sort();
- const theirs = html.map((c) => c.id).sort();
- test('every HTML component has a React component', () => {
- expect(theirs.filter((id) => !mine.includes(id))).toEqual([]);
- });
- test('every React component is in the HTML library', () => {
- expect(mine.filter((id) => !theirs.includes(id))).toEqual([]);
- });
- test('each lies in the folder of its level', () => {
- const lib = libraryComponents();
- for (const c of html) expect(lib.find((l) => l.id === c.id)?.dir, c.id).toBe(LEVEL_DIRS[c.header.level]);
- });
- });
- for (const c of html) {
- describe(`${c.id}: the same as the HTML template`, async () => {
- const mine = await load(c.id);
- test('the header is the same', () => {
- for (const f of ['level', 'controls', 'states', 'status', 'uses', 'parts'] as const)
- expect(mine.header[f], `${c.id} header ${f}`).toEqual(c.header[f]);
- });
- test('a state for every state and status of the header, and no other', () => {
- const wanted = [...c.header.states, ...c.header.status].sort();
- expect([...mine.states.names].sort()).toEqual(wanted);
- });
- test('the templates are the states and statuses of the header', () => {
- expect(Object.keys(c.templates).sort()).toEqual([...c.header.states, ...c.header.status].sort());
- });
- for (const [name, template] of Object.entries(c.templates)) {
- test(`state ${name}`, () => {
- const diff = compareFragment(template, renderState(mine, name));
- expect(diff, diff.join('\n')).toEqual([]);
- });
- }
- });
- }