archivo del paquete→ .shtukbase/packages/explore-kit/test/explore.test.ts
test/explore.test.ts
159 líneas, 7.4 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The pure core of the explore kit: the control schema, values, the URL, storage and presets.
- // Run: `bun test` in the package. The DOM part (the panel) is checked by the demo page and its screenshots.
- import { describe, expect, test } from 'bun:test';
- import { join } from 'node:path';
- const kit = require(join(import.meta.dir, '..', 'pages', 'design', 'explore-kit', 'explore.js'));
- const C = kit.core;
- const OPTS = {
- sections: [
- { title: 'Cards', controls: {
- preset: { kind: 'select', options: ['basic', 'cover', 'editorial'] },
- size: { kind: 'segment', options: [['s', 'S'], ['m', 'M'], ['l', 'L']], default: 'm' },
- round: { kind: 'bool' },
- } },
- { title: 'Edge', folded: true, controls: {
- line: { kind: 'range', min: 0, max: 20, step: 0.5, unit: '%', default: 7.5 },
- tint: { kind: 'swatch', options: ['chart-1', '--chart-2', '#e33', 'red'], colors: { red: '#d11' } },
- note: { kind: 'text', default: 'hi' },
- } },
- ],
- presets: { editorial: { preset: 'editorial', size: 'l' }, quiet: { round: true, line: 0 } },
- };
- describe('schema', () => {
- const m = C.normalize(OPTS);
- test('sections and a flat list keep the order', () => {
- expect(m.sections.map((s: any) => s.title)).toEqual(['Cards', 'Edge']);
- expect(m.controls.map((c: any) => c.name)).toEqual(['preset', 'size', 'round', 'line', 'tint', 'note']);
- expect(m.sections[1].folded).toBe(true);
- });
- test('defaults: given, else the first option, false, min, empty', () => {
- expect(C.defaults(m)).toEqual({ preset: 'basic', size: 'm', round: false, line: 7.5, tint: 'chart-1', note: 'hi' });
- });
- test('labels are humanized names unless given', () => {
- expect(m.controls[0].label).toBe('Preset');
- expect(C.humanize('chip-pos')).toBe('Chip pos');
- });
- test('options accept strings, pairs and objects', () => {
- expect(C.normOptions(['a', ['b', 'Bee'], { value: 3, label: 'Three' }])).toEqual([
- { value: 'a', label: 'a' }, { value: 'b', label: 'Bee' }, { value: 3, label: 'Three' }]);
- });
- test('controls with a section field join that section', () => {
- const m2 = C.normalize({ controls: { a: { kind: 'bool', section: 'X' }, b: { kind: 'text' } } });
- expect(m2.sections.map((s: any) => s.title)).toEqual(['X', '']);
- });
- test('an unknown kind becomes text; a bad default falls back', () => {
- const c = C.normControl('z', { kind: 'nope' });
- expect(c.kind).toBe('text');
- expect(C.normControl('s', { kind: 'select', options: ['a', 'b'], default: 'q' }).default).toBe('a');
- });
- });
- describe('coerce', () => {
- const m = C.normalize(OPTS);
- const by = (n: string) => m.controls.find((c: any) => c.name === n);
- test('select and swatch take only their options', () => {
- expect(C.coerce(by('preset'), 'cover')).toBe('cover');
- expect(C.coerce(by('preset'), 'nope')).toBeUndefined();
- expect(C.coerce(by('tint'), 'red')).toBe('red');
- });
- test('bool reads words and numbers; rejects others', () => {
- expect(C.coerce(by('round'), '1')).toBe(true);
- expect(C.coerce(by('round'), 'false')).toBe(false);
- expect(C.coerce(by('round'), 'maybe')).toBeUndefined();
- });
- test('range clamps and snaps to the step', () => {
- expect(C.coerce(by('line'), '99')).toBe(20);
- expect(C.coerce(by('line'), '3.3')).toBe(3.5);
- expect(C.coerce(by('line'), '-4')).toBe(0);
- expect(C.coerce(by('line'), 'x')).toBeUndefined();
- });
- });
- describe('URL', () => {
- const m = C.normalize(OPTS);
- test('reads the controls, ignores other parameters and bad values', () => {
- expect(C.fromQuery(m, '?size=l&view=deps&line=2&preset=bad¬e=a%20b')).toEqual({ size: 'l', line: 2, note: 'a b' });
- });
- test('writes only the non-default values and keeps other parameters', () => {
- const v = { ...C.defaults(m), size: 'l', round: true };
- expect(C.toQuery(m, v, '?view=deps&size=s')).toBe('?view=deps&size=l&round=true');
- expect(C.toQuery(m, C.defaults(m), '?size=s')).toBe('');
- });
- test('round trip', () => {
- const v = { ...C.defaults(m), tint: '#e33', line: 12.5, note: 'x&y=z' };
- expect(C.merge(m, C.defaults(m), C.fromQuery(m, C.toQuery(m, v, '')))).toEqual(v);
- });
- });
- describe('storage and the start values', () => {
- const m = C.normalize(OPTS);
- const mem = () => { const d: Record<string, string> = {}; return { getItem: (k: string) => d[k] ?? null, setItem: (k: string, v: string) => { d[k] = v; }, d }; };
- test('save and load under explore:<key>', () => {
- const s = mem();
- C.save(s, 'pkg', { values: { size: 'l' }, collapsed: true, folded: { Edge: false } });
- expect(Object.keys(s.d)).toEqual(['explore:pkg']);
- expect(C.load(s, 'pkg')).toEqual({ values: { size: 'l' }, collapsed: true, folded: { Edge: false } });
- });
- test('broken or missing storage gives an empty state', () => {
- const s = mem(); s.setItem('explore:pkg', '{not json');
- expect(C.load(s, 'pkg')).toEqual({ values: {}, collapsed: null, folded: {} });
- expect(C.load(null, 'pkg').values).toEqual({});
- const throwing = { getItem: () => { throw new Error('denied'); }, setItem: () => { throw new Error('denied'); } };
- expect(C.load(throwing, 'pkg').values).toEqual({});
- expect(() => C.save(throwing, 'pkg', {})).not.toThrow();
- });
- test('defaults ← stored ← URL; invalid stored values are dropped', () => {
- const v = C.initial(m, { values: { size: 's', line: 3, preset: 'gone' } }, '?size=l');
- expect(v.size).toBe('l');
- expect(v.line).toBe(3);
- expect(v.preset).toBe('basic');
- });
- test('diff keeps only what differs from the defaults', () => {
- expect(C.diff(m, { ...C.defaults(m), round: true })).toEqual({ round: true });
- });
- });
- describe('presets', () => {
- const m = C.normalize(OPTS);
- test('a preset is the defaults plus its bundle', () => {
- const v = C.applyPreset(m, OPTS.presets, 'editorial', { ...C.defaults(m), round: true });
- expect(v).toEqual({ ...C.defaults(m), preset: 'editorial', size: 'l' });
- });
- test('an unknown preset changes nothing', () => {
- const cur = { ...C.defaults(m), size: 's' };
- expect(C.applyPreset(m, OPTS.presets, 'nope', cur)).toEqual(cur);
- });
- test('the matching preset is found; a tweak makes it custom', () => {
- const v = C.applyPreset(m, OPTS.presets, 'quiet');
- expect(C.matchPreset(m, OPTS.presets, v)).toBe('quiet');
- expect(C.matchPreset(m, OPTS.presets, { ...v, size: 's' })).toBe('');
- expect(C.matchPreset(m, null, v)).toBe('');
- });
- });
- describe('what lands on the target', () => {
- const m = C.normalize(OPTS);
- test('data attribute and CSS value per kind', () => {
- const a = C.attrs(m, { ...C.defaults(m), round: true });
- const by = (n: string) => a.find((x: any) => x.name === n);
- expect(by('round')).toEqual({ name: 'round', data: 'true', css: '1' });
- expect(by('line')).toEqual({ name: 'line', data: '7.5', css: '7.5%' });
- expect(by('tint').css).toBe('var(--chart-1)');
- });
- test('swatch colours: token names, custom properties, colours and the colors map', () => {
- const c = m.controls.find((x: any) => x.name === 'tint');
- expect(C.colorOf(c, '--chart-2')).toBe('var(--chart-2)');
- expect(C.colorOf(c, '#e33')).toBe('#e33');
- expect(C.colorOf(c, 'red')).toBe('#d11');
- expect(C.colorOf(c, 'primary')).toBe('var(--primary)');
- });
- });
- test('the kit exposes Explore.panel and a version', () => {
- expect(typeof kit.panel).toBe('function');
- expect(kit.version).toBe('0.1.2');
- expect(C.KINDS).toEqual(['select', 'segment', 'bool', 'range', 'swatch', 'text']);
- });