|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateResponsiveStyles, |
| 6 | + STYLE_NODE_MISSING_ID, |
| 7 | + STYLE_CLASSNAME_TAILWIND, |
| 8 | + STYLE_RESPONSIVE_NO_BASE, |
| 9 | + STYLE_UNKNOWN_CSS_PROPERTY, |
| 10 | + STYLE_UNKNOWN_TOKEN, |
| 11 | +} from './validate-responsive-styles.js'; |
| 12 | + |
| 13 | +/** Wrap component nodes into a minimal stack with one page. */ |
| 14 | +const stackWith = (...components: any[]) => ({ |
| 15 | + pages: [{ name: 'pricing', regions: [{ name: 'main', components }] }], |
| 16 | +}); |
| 17 | + |
| 18 | +const rules = (findings: ReturnType<typeof validateResponsiveStyles>) => findings.map((f) => f.rule); |
| 19 | + |
| 20 | +describe('validateResponsiveStyles (ADR-0065)', () => { |
| 21 | + it('passes a clean page styled with responsiveStyles + tokens', () => { |
| 22 | + const findings = validateResponsiveStyles(stackWith({ |
| 23 | + id: 'card', type: 'flex', |
| 24 | + responsiveStyles: { |
| 25 | + large: { display: 'flex', flexDirection: 'column', gap: 'var(--space-4)', padding: 'var(--space-6)', backgroundColor: 'var(--surface)', border: '1px solid hsl(var(--primary))' }, |
| 26 | + small: { padding: 'var(--space-4)' }, |
| 27 | + }, |
| 28 | + properties: { |
| 29 | + children: [ |
| 30 | + { id: 'price', type: 'element:text', responsiveStyles: { large: { fontSize: '40px', color: 'var(--text-strong)' } }, properties: { content: '$29' } }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + })); |
| 34 | + expect(findings).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('errors when a styled node has no id (CSS cannot be scoped)', () => { |
| 38 | + const findings = validateResponsiveStyles(stackWith({ |
| 39 | + type: 'flex', responsiveStyles: { large: { padding: 'var(--space-4)' } }, |
| 40 | + })); |
| 41 | + expect(rules(findings)).toContain(STYLE_NODE_MISSING_ID); |
| 42 | + expect(findings[0].severity).toBe('error'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('warns when a smaller breakpoint has no large base', () => { |
| 46 | + const findings = validateResponsiveStyles(stackWith({ |
| 47 | + id: 'x', type: 'flex', responsiveStyles: { small: { padding: 'var(--space-2)' } }, |
| 48 | + })); |
| 49 | + expect(rules(findings)).toContain(STYLE_RESPONSIVE_NO_BASE); |
| 50 | + }); |
| 51 | + |
| 52 | + it('warns on Tailwind-looking className (silently dead in metadata)', () => { |
| 53 | + const findings = validateResponsiveStyles(stackWith({ |
| 54 | + id: 'x', type: 'flex', className: 'flex flex-col gap-4 md:grid-cols-2 bg-primary', |
| 55 | + })); |
| 56 | + expect(rules(findings)).toContain(STYLE_CLASSNAME_TAILWIND); |
| 57 | + }); |
| 58 | + |
| 59 | + it('warns on an unknown CSS property (typo)', () => { |
| 60 | + const findings = validateResponsiveStyles(stackWith({ |
| 61 | + id: 'x', type: 'flex', responsiveStyles: { large: { flexDirektion: 'column' } }, |
| 62 | + })); |
| 63 | + expect(rules(findings)).toContain(STYLE_UNKNOWN_CSS_PROPERTY); |
| 64 | + }); |
| 65 | + |
| 66 | + it('warns on an unknown design token (typo)', () => { |
| 67 | + const findings = validateResponsiveStyles(stackWith({ |
| 68 | + id: 'x', type: 'flex', responsiveStyles: { large: { padding: 'var(--spcae-6)' } }, |
| 69 | + })); |
| 70 | + expect(rules(findings)).toContain(STYLE_UNKNOWN_TOKEN); |
| 71 | + }); |
| 72 | + |
| 73 | + it('resolves known tokens (incl. hsl(var(--primary))) without complaint', () => { |
| 74 | + const findings = validateResponsiveStyles(stackWith({ |
| 75 | + id: 'x', type: 'flex', |
| 76 | + responsiveStyles: { large: { color: 'hsl(var(--primary))', boxShadow: '0 0 0 3px hsl(var(--primary) / 0.25), var(--shadow-lg)', borderRadius: 'var(--radius-xl)' } }, |
| 77 | + })); |
| 78 | + expect(findings).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('recurses into nested properties.children', () => { |
| 82 | + const findings = validateResponsiveStyles(stackWith({ |
| 83 | + id: 'root', type: 'flex', responsiveStyles: { large: { display: 'flex' } }, |
| 84 | + properties: { children: [ |
| 85 | + { type: 'flex', responsiveStyles: { large: { gap: 'var(--space-2)' } } }, // missing id, nested |
| 86 | + ] }, |
| 87 | + })); |
| 88 | + expect(rules(findings)).toContain(STYLE_NODE_MISSING_ID); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not flag a plain non-Tailwind className', () => { |
| 92 | + const findings = validateResponsiveStyles(stackWith({ |
| 93 | + id: 'x', type: 'flex', className: 'my-custom-scope', responsiveStyles: { large: { display: 'flex' } }, |
| 94 | + })); |
| 95 | + expect(rules(findings)).not.toContain(STYLE_CLASSNAME_TAILWIND); |
| 96 | + }); |
| 97 | +}); |
0 commit comments