ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/shtukbase-ui-react/test/controls.test.ts

test/controls.test.ts

58 líneas, 3.1 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // Controls are props (build contract, section 4): for every control in a component's header, the prop of the same name
  2. // (camelCase) draws what the HTML library draws for it: `data-<control>="<value>"` on the root, a bare attribute for a `bool`,
  3. // the inline custom property `--<id>-<control>` for a `range`. Read from the React headers, which SBR-R03 and the parity test
  4. // hold equal to the HTML ones.
  5. import { describe, expect, test } from 'bun:test';
  6. import { type Loaded, libraryComponents, load, parseHtml, render } from './lib.ts';
  7. /** Controls whose prop is not the control name: the name `style` is the inline style of every React element. */
  8. const PROP_NAMES: Record<string, string> = { 'chip.style': 'chipStyle' };
  9. const camel = (s: string) => s.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
  10. const propOf = (id: string, control: string) => PROP_NAMES[`${id}.${control}`] ?? camel(control);
  11. /** The props of the first state that is a props object (a state can be a function that draws several elements). */
  12. function baseProps(c: Loaded): Record<string, unknown> {
  13. for (const name of c.states.names) if (typeof c.states.entries[name] !== 'function') return { ...c.states.entries[name] };
  14. return {};
  15. }
  16. /** The root element of the markup a component draws. */
  17. function rootOf(c: Loaded, props: Record<string, unknown>) {
  18. const el = parseHtml(render(c.component, props)).find((n) => typeof n !== 'string');
  19. if (!el || typeof el === 'string') throw new Error(`${c.id} draws no element`);
  20. return el;
  21. }
  22. const customProps = (style: string | undefined) =>
  23. Object.fromEntries((style ?? '').split(';').map((p) => p.split(':').map((x) => x.trim())).filter((p) => p[0]?.startsWith('--')));
  24. for (const { id } of libraryComponents()) {
  25. describe(`${id}: controls are props`, async () => {
  26. const c = await load(id);
  27. const base = baseProps(c);
  28. for (const [control, def] of Object.entries(c.header.controls)) {
  29. const prop = propOf(id, control);
  30. const attr = `data-${control}`;
  31. if (def.kind === 'select' || def.kind === 'segment' || def.kind === 'swatch') {
  32. for (const option of def.options ?? []) {
  33. test(`${control}=${option} sets ${attr}`, () => {
  34. expect(rootOf(c, { ...base, [prop]: option }).attrs[attr]).toBe(String(option));
  35. });
  36. }
  37. } else if (def.kind === 'bool') {
  38. // a bool that is on by default is switched off to see the attribute go; one that is off is switched on
  39. const on = def.default !== true;
  40. test(`${control}=${on} ${on ? 'sets' : 'removes'} ${attr}`, () => {
  41. const root = rootOf(c, { ...base, [prop]: on });
  42. expect(attr in root.attrs).toBe(on);
  43. });
  44. } else if (def.kind === 'range') {
  45. const value = Math.round(((def.min ?? 0) + (def.max ?? 100)) / 2);
  46. test(`${control}=${value}${def.unit ?? ''} sets --${id}-${control}`, () => {
  47. const root = rootOf(c, { ...base, [prop]: value });
  48. expect(customProps(root.attrs.style)[`--${id}-${control}`]).toBe(`${value}${def.unit ?? ''}`);
  49. });
  50. }
  51. }
  52. });
  53. }