файл пакета→ .shtukbase/packages/design-lenses/test/style-data.test.ts
test/style-data.test.ts
78 строк, 5.1 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The pure core of the specimen's data: DTCG values to CSS, references, the dark tree, groups, tokens.css.
- import { describe, expect, test } from 'bun:test';
- import { cssName, cssValue, groupOf, leaves, parseTokensCss, sortTokens, styleDataJs, styleTokens } from '../lib/style-data.ts';
- const none = () => null;
- describe('cssValue', () => {
- test('colours: hex, components, alpha', () => {
- expect(cssValue('#fff', 'color', none)).toBe('#fff');
- expect(cssValue({ colorSpace: 'srgb', components: [1, 0, 0], hex: '#ff0000' }, 'color', none)).toBe('#ff0000');
- expect(cssValue({ colorSpace: 'oklch', components: [0.6, 0.2, 10] }, 'color', none)).toBe('oklch(0.6 0.2 10)');
- expect(cssValue({ colorSpace: 'srgb', components: [1, 0, 0], alpha: 0.5 }, 'color', none)).toBe('color(srgb 1 0 0 / 0.5)');
- });
- test('dimensions, durations, fonts, easings', () => {
- expect(cssValue({ value: 0.5, unit: 'rem' }, 'dimension', none)).toBe('0.5rem');
- expect(cssValue({ value: 180, unit: 'ms' }, 'duration', none)).toBe('180ms');
- expect(cssValue(['Geist Mono', 'monospace'], 'fontFamily', none)).toBe('"Geist Mono", monospace');
- expect(cssValue([0.2, 0, 0, 1], 'cubicBezier', none)).toBe('cubic-bezier(0.2, 0, 0, 1)');
- });
- test('shadows: one, a list, inset', () => {
- const s = { color: '#0000001a', offsetX: '0px', offsetY: '1px', blur: '2px', spread: '0px' };
- expect(cssValue(s, 'shadow', none)).toBe('0px 1px 2px 0px #0000001a');
- expect(cssValue([s, { ...s, inset: true }], 'shadow', none)).toBe('0px 1px 2px 0px #0000001a, inset 0px 1px 2px 0px #0000001a');
- });
- test('a reference resolves through the callback, an unknown one stays as written', () => {
- expect(cssValue('{a.b}', 'color', (p) => (p === 'a.b' ? '#123' : null))).toBe('#123');
- expect(cssValue('{x}', 'color', none)).toBe('{x}');
- });
- });
- describe('tokens', () => {
- test('paths map to names; group types are inherited', () => {
- const l = leaves({ sb: { hue: { $type: 'color', 1: { $value: '#e04a72' } } }, 'card-foreground': { $type: 'color', $value: '#111' } });
- expect(l.map((x) => [cssName(x.path), x.type])).toEqual([['--sb-hue-1', 'color'], ['--card-foreground', 'color']]);
- });
- test('the dark tree overrides; references resolve against the merged tree', () => {
- const light = { background: { $type: 'color', $value: '#fff' }, card: { $type: 'color', $value: '{background}' }, radius: { $type: 'dimension', $value: { value: 6, unit: 'px' } } };
- const dark = { background: { $type: 'color', $value: '#191919' } };
- const t = styleTokens(light, dark);
- const card = t.find((x) => x.name === '--card');
- expect(card).toMatchObject({ light: '#fff', dark: '#191919', group: 'Colors' });
- expect(t.find((x) => x.name === '--radius')).toMatchObject({ light: '6px', dark: '6px', group: 'Radii' });
- });
- test('shadow layers are joined by commas, also in an override without $type', () => {
- const layer = (x: number) => ({ offsetX: { value: 0, unit: 'px' }, offsetY: { value: 0, unit: 'px' }, blur: { value: x, unit: 'px' }, spread: { value: 0, unit: 'px' }, color: '#000' });
- expect(cssValue([layer(1), layer(8)], '', none)).toBe(cssValue([layer(1), layer(8)], 'shadow', none));
- expect(cssValue([layer(1), layer(8)], '', none)).toContain(', ');
- });
- test('a reference that leads nowhere or loops is a problem, not a crash', () => {
- const problems: string[] = [];
- styleTokens({ a: { $type: 'color', $value: '{b}' }, b: { $type: 'color', $value: '{a}' }, c: { $type: 'color', $value: '{zzz}' } }, null, problems);
- expect(problems.some((p) => p.includes('circular'))).toBe(true);
- expect(problems.some((p) => p.includes('{zzz}'))).toBe(true);
- });
- test('groups by name and type, sorted in the specimen order', () => {
- expect(groupOf('--chart-1', 'color')).toBe('Charts');
- expect(groupOf('--sidebar-ring', 'color')).toBe('Sidebar');
- expect(groupOf('--sb-hue-3-soft', 'color')).toBe('Hues');
- expect(groupOf('--sb-glow', 'shadow')).toBe('Glows and edges');
- expect(groupOf('--shadow-sm', 'shadow')).toBe('Shadows');
- expect(groupOf('--font-sans', 'fontFamily')).toBe('Type');
- expect(groupOf('--sb-ease', 'cubicBezier')).toBe('Motion');
- const s = sortTokens([{ name: '--sb-dur', group: 'Motion' }, { name: '--background', group: 'Colors' }] as never);
- expect(s.map((x) => x.name)).toEqual(['--background', '--sb-dur']);
- });
- test('tokens.css: the light and the dark block', () => {
- const t = parseTokensCss(':root { --background: #fff; --radius: .5rem; }\n.dark, [data-theme="dark"] { --background: #191919; }');
- expect(t).toEqual([
- { name: '--background', group: 'Colors', type: 'color', light: '#fff', dark: '#191919', description: '' },
- { name: '--radius', group: 'Radii', type: 'dimension', light: '.5rem', dark: '.5rem', description: '' },
- ]);
- });
- test('the data file is stable text', () => {
- const d = { version: 1 as const, id: 's', title: 'S', tokens: [], sections: [], css: [], assets: [], styles: [], problems: [] };
- expect(styleDataJs(d)).toBe(styleDataJs(d));
- expect(styleDataJs(d)).toContain('window.SB_STYLE = {');
- });
- });