import type { Config, LocalOptions } from 'style-dictionary/types' import { describe, expect, it } from 'vitest' import { getPrefixes, processCssVariables } from './utils.js' describe('getPrefixes', () => { const createMockOptions = (prefix?: string): Config & LocalOptions => ({ prefix, platforms: {}, source: [], include: [], exclude: [], files: [], transform: {}, transformGroup: {}, format: {}, action: {}, fileHeader: '', log: {}, }) it('should return empty prefixes when no prefix is provided', () => { const options = createMockOptions() const result = getPrefixes(options) expect(result).toEqual({ classPrefix: '', variablePrefix: '', }) }) it('should return prefixes with backslash escape for class prefix', () => { const options = createMockOptions('uy') const result = getPrefixes(options) expect(result).toEqual({ classPrefix: 'uy\\:', variablePrefix: 'uy-', }) }) it('should handle different prefix values', () => { const testCases = [ { prefix: 'unity', expected: { classPrefix: 'unity\\:', variablePrefix: 'unity-' }, }, { prefix: 'custom', expected: { classPrefix: 'custom\\:', variablePrefix: 'custom-' }, }, { prefix: 'test-prefix', expected: { classPrefix: 'test-prefix\\:', variablePrefix: 'test-prefix-', }, }, ] testCases.forEach(({ prefix, expected }) => { const options = createMockOptions(prefix) const result = getPrefixes(options) expect(result).toEqual(expected) }) }) it('should handle empty string prefix', () => { const options = createMockOptions('') const result = getPrefixes(options) expect(result).toEqual({ classPrefix: '', variablePrefix: '', }) }) }) describe('processCssVariables', () => { it('should add prefix to var() references', () => { const rawCssVars = ` --color-primary: #000000; --color-secondary: var(--color-gray-500); --spacing-1: 0.25rem; --spacing-2: var(--spacing-1); ` const variablePrefix = 'uy-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: #000000; --color-secondary: var(--uy-color-gray-500); --spacing-1: 0.25rem; --spacing-2: var(--uy-spacing-1); " `) }) it('should handle empty variable prefix', () => { const rawCssVars = ` --color-primary: var(--color-gray-500); --spacing-1: var(--spacing-base); ` const variablePrefix = '' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: var(--color-gray-500); --spacing-1: var(--spacing-base); " `) }) it('should handle complex var() references', () => { const rawCssVars = ` --color-primary: var(--color-gray-500); --color-secondary: var(--color-primary); --color-tertiary: var(--color-secondary); --spacing-1: var(--spacing-base); --spacing-2: var(--spacing-1); --spacing-3: var(--spacing-2); ` const variablePrefix = 'custom-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: var(--custom-color-gray-500); --color-secondary: var(--custom-color-primary); --color-tertiary: var(--custom-color-secondary); --spacing-1: var(--custom-spacing-base); --spacing-2: var(--custom-spacing-1); --spacing-3: var(--custom-spacing-2); " `) }) it('should handle nested var() references', () => { const rawCssVars = ` --color-primary: var(--color-gray-500); --color-secondary: var(--color-primary); --color-tertiary: var(--color-secondary); ` const variablePrefix = 'nested-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: var(--nested-color-gray-500); --color-secondary: var(--nested-color-primary); --color-tertiary: var(--nested-color-secondary); " `) }) it('should handle CSS without var() references', () => { const rawCssVars = ` --color-primary: #000000; --color-secondary: #ffffff; --spacing-1: 0.25rem; --spacing-2: 0.5rem; ` const variablePrefix = 'uy-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: #000000; --color-secondary: #ffffff; --spacing-1: 0.25rem; --spacing-2: 0.5rem; " `) }) it('should handle empty CSS string', () => { const rawCssVars = '' const variablePrefix = 'uy-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toBe('') }) it('should handle CSS with mixed var() and non-var() values', () => { const rawCssVars = ` --color-primary: #000000; --color-secondary: var(--color-gray-500); --color-tertiary: #ffffff; --spacing-1: 0.25rem; --spacing-2: var(--spacing-1); --spacing-3: 1rem; ` const variablePrefix = 'mixed-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: #000000; --color-secondary: var(--mixed-color-gray-500); --color-tertiary: #ffffff; --spacing-1: 0.25rem; --spacing-2: var(--mixed-spacing-1); --spacing-3: 1rem; " `) }) it('should handle CSS with special characters in variable names', () => { const rawCssVars = ` --color-primary: var(--color-gray-500); --color-secondary: var(--color-primary); --spacing-1: var(--spacing-base); --border-radius: var(--radius-small); ` const variablePrefix = 'special-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: var(--special-color-gray-500); --color-secondary: var(--special-color-primary); --spacing-1: var(--special-spacing-base); --border-radius: var(--special-radius-small); " `) }) it('should handle CSS with multiple var() references on the same line', () => { const rawCssVars = ` --color-primary: var(--color-gray-500); --color-secondary: var(--color-primary); --color-tertiary: var(--color-secondary); --spacing-1: var(--spacing-base); --spacing-2: var(--spacing-1); --spacing-3: var(--spacing-2); ` const variablePrefix = 'multi-' const result = processCssVariables(rawCssVars, variablePrefix) expect(result).toMatchInlineSnapshot(` " --color-primary: var(--multi-color-gray-500); --color-secondary: var(--multi-color-primary); --color-tertiary: var(--multi-color-secondary); --spacing-1: var(--multi-spacing-base); --spacing-2: var(--multi-spacing-1); --spacing-3: var(--multi-spacing-2); " `) }) })