ShtukbaseStandard
包文件→ .shtukbase/packages/design-lenses/test/style-data.test.ts

test/style-data.test.ts

78 行, 5.1 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The pure core of the specimen's data: DTCG values to CSS, references, the dark tree, groups, tokens.css.
  2. import { describe, expect, test } from 'bun:test';
  3. import { cssName, cssValue, groupOf, leaves, parseTokensCss, sortTokens, styleDataJs, styleTokens } from '../lib/style-data.ts';
  4. const none = () => null;
  5. describe('cssValue', () => {
  6. test('colours: hex, components, alpha', () => {
  7. expect(cssValue('#fff', 'color', none)).toBe('#fff');
  8. expect(cssValue({ colorSpace: 'srgb', components: [1, 0, 0], hex: '#ff0000' }, 'color', none)).toBe('#ff0000');
  9. expect(cssValue({ colorSpace: 'oklch', components: [0.6, 0.2, 10] }, 'color', none)).toBe('oklch(0.6 0.2 10)');
  10. expect(cssValue({ colorSpace: 'srgb', components: [1, 0, 0], alpha: 0.5 }, 'color', none)).toBe('color(srgb 1 0 0 / 0.5)');
  11. });
  12. test('dimensions, durations, fonts, easings', () => {
  13. expect(cssValue({ value: 0.5, unit: 'rem' }, 'dimension', none)).toBe('0.5rem');
  14. expect(cssValue({ value: 180, unit: 'ms' }, 'duration', none)).toBe('180ms');
  15. expect(cssValue(['Geist Mono', 'monospace'], 'fontFamily', none)).toBe('"Geist Mono", monospace');
  16. expect(cssValue([0.2, 0, 0, 1], 'cubicBezier', none)).toBe('cubic-bezier(0.2, 0, 0, 1)');
  17. });
  18. test('shadows: one, a list, inset', () => {
  19. const s = { color: '#0000001a', offsetX: '0px', offsetY: '1px', blur: '2px', spread: '0px' };
  20. expect(cssValue(s, 'shadow', none)).toBe('0px 1px 2px 0px #0000001a');
  21. expect(cssValue([s, { ...s, inset: true }], 'shadow', none)).toBe('0px 1px 2px 0px #0000001a, inset 0px 1px 2px 0px #0000001a');
  22. });
  23. test('a reference resolves through the callback, an unknown one stays as written', () => {
  24. expect(cssValue('{a.b}', 'color', (p) => (p === 'a.b' ? '#123' : null))).toBe('#123');
  25. expect(cssValue('{x}', 'color', none)).toBe('{x}');
  26. });
  27. });
  28. describe('tokens', () => {
  29. test('paths map to names; group types are inherited', () => {
  30. const l = leaves({ sb: { hue: { $type: 'color', 1: { $value: '#e04a72' } } }, 'card-foreground': { $type: 'color', $value: '#111' } });
  31. expect(l.map((x) => [cssName(x.path), x.type])).toEqual([['--sb-hue-1', 'color'], ['--card-foreground', 'color']]);
  32. });
  33. test('the dark tree overrides; references resolve against the merged tree', () => {
  34. const light = { background: { $type: 'color', $value: '#fff' }, card: { $type: 'color', $value: '{background}' }, radius: { $type: 'dimension', $value: { value: 6, unit: 'px' } } };
  35. const dark = { background: { $type: 'color', $value: '#191919' } };
  36. const t = styleTokens(light, dark);
  37. const card = t.find((x) => x.name === '--card');
  38. expect(card).toMatchObject({ light: '#fff', dark: '#191919', group: 'Colors' });
  39. expect(t.find((x) => x.name === '--radius')).toMatchObject({ light: '6px', dark: '6px', group: 'Radii' });
  40. });
  41. test('shadow layers are joined by commas, also in an override without $type', () => {
  42. 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' });
  43. expect(cssValue([layer(1), layer(8)], '', none)).toBe(cssValue([layer(1), layer(8)], 'shadow', none));
  44. expect(cssValue([layer(1), layer(8)], '', none)).toContain(', ');
  45. });
  46. test('a reference that leads nowhere or loops is a problem, not a crash', () => {
  47. const problems: string[] = [];
  48. styleTokens({ a: { $type: 'color', $value: '{b}' }, b: { $type: 'color', $value: '{a}' }, c: { $type: 'color', $value: '{zzz}' } }, null, problems);
  49. expect(problems.some((p) => p.includes('circular'))).toBe(true);
  50. expect(problems.some((p) => p.includes('{zzz}'))).toBe(true);
  51. });
  52. test('groups by name and type, sorted in the specimen order', () => {
  53. expect(groupOf('--chart-1', 'color')).toBe('Charts');
  54. expect(groupOf('--sidebar-ring', 'color')).toBe('Sidebar');
  55. expect(groupOf('--sb-hue-3-soft', 'color')).toBe('Hues');
  56. expect(groupOf('--sb-glow', 'shadow')).toBe('Glows and edges');
  57. expect(groupOf('--shadow-sm', 'shadow')).toBe('Shadows');
  58. expect(groupOf('--font-sans', 'fontFamily')).toBe('Type');
  59. expect(groupOf('--sb-ease', 'cubicBezier')).toBe('Motion');
  60. const s = sortTokens([{ name: '--sb-dur', group: 'Motion' }, { name: '--background', group: 'Colors' }] as never);
  61. expect(s.map((x) => x.name)).toEqual(['--background', '--sb-dur']);
  62. });
  63. test('tokens.css: the light and the dark block', () => {
  64. const t = parseTokensCss(':root { --background: #fff; --radius: .5rem; }\n.dark, [data-theme="dark"] { --background: #191919; }');
  65. expect(t).toEqual([
  66. { name: '--background', group: 'Colors', type: 'color', light: '#fff', dark: '#191919', description: '' },
  67. { name: '--radius', group: 'Radii', type: 'dimension', light: '.5rem', dark: '.5rem', description: '' },
  68. ]);
  69. });
  70. test('the data file is stable text', () => {
  71. const d = { version: 1 as const, id: 's', title: 'S', tokens: [], sections: [], css: [], assets: [], styles: [], problems: [] };
  72. expect(styleDataJs(d)).toBe(styleDataJs(d));
  73. expect(styleDataJs(d)).toContain('window.SB_STYLE = {');
  74. });
  75. });