ShtukbaseStandard
package file→ .shtukbase/packages/explore-kit/test/explore.test.ts

test/explore.test.ts

159 lines, 7.4 KB.

  1. // The pure core of the explore kit: the control schema, values, the URL, storage and presets.
  2. // Run: `bun test` in the package. The DOM part (the panel) is checked by the demo page and its screenshots.
  3. import { describe, expect, test } from 'bun:test';
  4. import { join } from 'node:path';
  5. const kit = require(join(import.meta.dir, '..', 'pages', 'design', 'explore-kit', 'explore.js'));
  6. const C = kit.core;
  7. const OPTS = {
  8. sections: [
  9. { title: 'Cards', controls: {
  10. preset: { kind: 'select', options: ['basic', 'cover', 'editorial'] },
  11. size: { kind: 'segment', options: [['s', 'S'], ['m', 'M'], ['l', 'L']], default: 'm' },
  12. round: { kind: 'bool' },
  13. } },
  14. { title: 'Edge', folded: true, controls: {
  15. line: { kind: 'range', min: 0, max: 20, step: 0.5, unit: '%', default: 7.5 },
  16. tint: { kind: 'swatch', options: ['chart-1', '--chart-2', '#e33', 'red'], colors: { red: '#d11' } },
  17. note: { kind: 'text', default: 'hi' },
  18. } },
  19. ],
  20. presets: { editorial: { preset: 'editorial', size: 'l' }, quiet: { round: true, line: 0 } },
  21. };
  22. describe('schema', () => {
  23. const m = C.normalize(OPTS);
  24. test('sections and a flat list keep the order', () => {
  25. expect(m.sections.map((s: any) => s.title)).toEqual(['Cards', 'Edge']);
  26. expect(m.controls.map((c: any) => c.name)).toEqual(['preset', 'size', 'round', 'line', 'tint', 'note']);
  27. expect(m.sections[1].folded).toBe(true);
  28. });
  29. test('defaults: given, else the first option, false, min, empty', () => {
  30. expect(C.defaults(m)).toEqual({ preset: 'basic', size: 'm', round: false, line: 7.5, tint: 'chart-1', note: 'hi' });
  31. });
  32. test('labels are humanized names unless given', () => {
  33. expect(m.controls[0].label).toBe('Preset');
  34. expect(C.humanize('chip-pos')).toBe('Chip pos');
  35. });
  36. test('options accept strings, pairs and objects', () => {
  37. expect(C.normOptions(['a', ['b', 'Bee'], { value: 3, label: 'Three' }])).toEqual([
  38. { value: 'a', label: 'a' }, { value: 'b', label: 'Bee' }, { value: 3, label: 'Three' }]);
  39. });
  40. test('controls with a section field join that section', () => {
  41. const m2 = C.normalize({ controls: { a: { kind: 'bool', section: 'X' }, b: { kind: 'text' } } });
  42. expect(m2.sections.map((s: any) => s.title)).toEqual(['X', '']);
  43. });
  44. test('an unknown kind becomes text; a bad default falls back', () => {
  45. const c = C.normControl('z', { kind: 'nope' });
  46. expect(c.kind).toBe('text');
  47. expect(C.normControl('s', { kind: 'select', options: ['a', 'b'], default: 'q' }).default).toBe('a');
  48. });
  49. });
  50. describe('coerce', () => {
  51. const m = C.normalize(OPTS);
  52. const by = (n: string) => m.controls.find((c: any) => c.name === n);
  53. test('select and swatch take only their options', () => {
  54. expect(C.coerce(by('preset'), 'cover')).toBe('cover');
  55. expect(C.coerce(by('preset'), 'nope')).toBeUndefined();
  56. expect(C.coerce(by('tint'), 'red')).toBe('red');
  57. });
  58. test('bool reads words and numbers; rejects others', () => {
  59. expect(C.coerce(by('round'), '1')).toBe(true);
  60. expect(C.coerce(by('round'), 'false')).toBe(false);
  61. expect(C.coerce(by('round'), 'maybe')).toBeUndefined();
  62. });
  63. test('range clamps and snaps to the step', () => {
  64. expect(C.coerce(by('line'), '99')).toBe(20);
  65. expect(C.coerce(by('line'), '3.3')).toBe(3.5);
  66. expect(C.coerce(by('line'), '-4')).toBe(0);
  67. expect(C.coerce(by('line'), 'x')).toBeUndefined();
  68. });
  69. });
  70. describe('URL', () => {
  71. const m = C.normalize(OPTS);
  72. test('reads the controls, ignores other parameters and bad values', () => {
  73. expect(C.fromQuery(m, '?size=l&view=deps&line=2&preset=bad&note=a%20b')).toEqual({ size: 'l', line: 2, note: 'a b' });
  74. });
  75. test('writes only the non-default values and keeps other parameters', () => {
  76. const v = { ...C.defaults(m), size: 'l', round: true };
  77. expect(C.toQuery(m, v, '?view=deps&size=s')).toBe('?view=deps&size=l&round=true');
  78. expect(C.toQuery(m, C.defaults(m), '?size=s')).toBe('');
  79. });
  80. test('round trip', () => {
  81. const v = { ...C.defaults(m), tint: '#e33', line: 12.5, note: 'x&y=z' };
  82. expect(C.merge(m, C.defaults(m), C.fromQuery(m, C.toQuery(m, v, '')))).toEqual(v);
  83. });
  84. });
  85. describe('storage and the start values', () => {
  86. const m = C.normalize(OPTS);
  87. const mem = () => { const d: Record<string, string> = {}; return { getItem: (k: string) => d[k] ?? null, setItem: (k: string, v: string) => { d[k] = v; }, d }; };
  88. test('save and load under explore:<key>', () => {
  89. const s = mem();
  90. C.save(s, 'pkg', { values: { size: 'l' }, collapsed: true, folded: { Edge: false } });
  91. expect(Object.keys(s.d)).toEqual(['explore:pkg']);
  92. expect(C.load(s, 'pkg')).toEqual({ values: { size: 'l' }, collapsed: true, folded: { Edge: false } });
  93. });
  94. test('broken or missing storage gives an empty state', () => {
  95. const s = mem(); s.setItem('explore:pkg', '{not json');
  96. expect(C.load(s, 'pkg')).toEqual({ values: {}, collapsed: null, folded: {} });
  97. expect(C.load(null, 'pkg').values).toEqual({});
  98. const throwing = { getItem: () => { throw new Error('denied'); }, setItem: () => { throw new Error('denied'); } };
  99. expect(C.load(throwing, 'pkg').values).toEqual({});
  100. expect(() => C.save(throwing, 'pkg', {})).not.toThrow();
  101. });
  102. test('defaults ← stored ← URL; invalid stored values are dropped', () => {
  103. const v = C.initial(m, { values: { size: 's', line: 3, preset: 'gone' } }, '?size=l');
  104. expect(v.size).toBe('l');
  105. expect(v.line).toBe(3);
  106. expect(v.preset).toBe('basic');
  107. });
  108. test('diff keeps only what differs from the defaults', () => {
  109. expect(C.diff(m, { ...C.defaults(m), round: true })).toEqual({ round: true });
  110. });
  111. });
  112. describe('presets', () => {
  113. const m = C.normalize(OPTS);
  114. test('a preset is the defaults plus its bundle', () => {
  115. const v = C.applyPreset(m, OPTS.presets, 'editorial', { ...C.defaults(m), round: true });
  116. expect(v).toEqual({ ...C.defaults(m), preset: 'editorial', size: 'l' });
  117. });
  118. test('an unknown preset changes nothing', () => {
  119. const cur = { ...C.defaults(m), size: 's' };
  120. expect(C.applyPreset(m, OPTS.presets, 'nope', cur)).toEqual(cur);
  121. });
  122. test('the matching preset is found; a tweak makes it custom', () => {
  123. const v = C.applyPreset(m, OPTS.presets, 'quiet');
  124. expect(C.matchPreset(m, OPTS.presets, v)).toBe('quiet');
  125. expect(C.matchPreset(m, OPTS.presets, { ...v, size: 's' })).toBe('');
  126. expect(C.matchPreset(m, null, v)).toBe('');
  127. });
  128. });
  129. describe('what lands on the target', () => {
  130. const m = C.normalize(OPTS);
  131. test('data attribute and CSS value per kind', () => {
  132. const a = C.attrs(m, { ...C.defaults(m), round: true });
  133. const by = (n: string) => a.find((x: any) => x.name === n);
  134. expect(by('round')).toEqual({ name: 'round', data: 'true', css: '1' });
  135. expect(by('line')).toEqual({ name: 'line', data: '7.5', css: '7.5%' });
  136. expect(by('tint').css).toBe('var(--chart-1)');
  137. });
  138. test('swatch colours: token names, custom properties, colours and the colors map', () => {
  139. const c = m.controls.find((x: any) => x.name === 'tint');
  140. expect(C.colorOf(c, '--chart-2')).toBe('var(--chart-2)');
  141. expect(C.colorOf(c, '#e33')).toBe('#e33');
  142. expect(C.colorOf(c, 'red')).toBe('#d11');
  143. expect(C.colorOf(c, 'primary')).toBe('var(--primary)');
  144. });
  145. });
  146. test('the kit exposes Explore.panel and a version', () => {
  147. expect(typeof kit.panel).toBe('function');
  148. expect(kit.version).toBe('0.1.2');
  149. expect(C.KINDS).toEqual(['select', 'segment', 'bool', 'range', 'swatch', 'text']);
  150. });