包文件→ .shtukbase/packages/shtukbase-ui-react/test/controls.test.ts
test/controls.test.ts
58 行, 3.1 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Controls are props (build contract, section 4): for every control in a component's header, the prop of the same name
- // (camelCase) draws what the HTML library draws for it: `data-<control>="<value>"` on the root, a bare attribute for a `bool`,
- // the inline custom property `--<id>-<control>` for a `range`. Read from the React headers, which SBR-R03 and the parity test
- // hold equal to the HTML ones.
- import { describe, expect, test } from 'bun:test';
- import { type Loaded, libraryComponents, load, parseHtml, render } from './lib.ts';
- /** Controls whose prop is not the control name: the name `style` is the inline style of every React element. */
- const PROP_NAMES: Record<string, string> = { 'chip.style': 'chipStyle' };
- const camel = (s: string) => s.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
- const propOf = (id: string, control: string) => PROP_NAMES[`${id}.${control}`] ?? camel(control);
- /** The props of the first state that is a props object (a state can be a function that draws several elements). */
- function baseProps(c: Loaded): Record<string, unknown> {
- for (const name of c.states.names) if (typeof c.states.entries[name] !== 'function') return { ...c.states.entries[name] };
- return {};
- }
- /** The root element of the markup a component draws. */
- function rootOf(c: Loaded, props: Record<string, unknown>) {
- const el = parseHtml(render(c.component, props)).find((n) => typeof n !== 'string');
- if (!el || typeof el === 'string') throw new Error(`${c.id} draws no element`);
- return el;
- }
- const customProps = (style: string | undefined) =>
- Object.fromEntries((style ?? '').split(';').map((p) => p.split(':').map((x) => x.trim())).filter((p) => p[0]?.startsWith('--')));
- for (const { id } of libraryComponents()) {
- describe(`${id}: controls are props`, async () => {
- const c = await load(id);
- const base = baseProps(c);
- for (const [control, def] of Object.entries(c.header.controls)) {
- const prop = propOf(id, control);
- const attr = `data-${control}`;
- if (def.kind === 'select' || def.kind === 'segment' || def.kind === 'swatch') {
- for (const option of def.options ?? []) {
- test(`${control}=${option} sets ${attr}`, () => {
- expect(rootOf(c, { ...base, [prop]: option }).attrs[attr]).toBe(String(option));
- });
- }
- } else if (def.kind === 'bool') {
- // a bool that is on by default is switched off to see the attribute go; one that is off is switched on
- const on = def.default !== true;
- test(`${control}=${on} ${on ? 'sets' : 'removes'} ${attr}`, () => {
- const root = rootOf(c, { ...base, [prop]: on });
- expect(attr in root.attrs).toBe(on);
- });
- } else if (def.kind === 'range') {
- const value = Math.round(((def.min ?? 0) + (def.max ?? 100)) / 2);
- test(`${control}=${value}${def.unit ?? ''} sets --${id}-${control}`, () => {
- const root = rootOf(c, { ...base, [prop]: value });
- expect(customProps(root.attrs.style)[`--${id}-${control}`]).toBe(`${value}${def.unit ?? ''}`);
- });
- }
- }
- });
- }