import type { Token } from 'style-dictionary' import type { Config, PlatformConfig } from 'style-dictionary/types' import { describe, expect, it } from 'vitest' import { filter, transform } from './tailwind-spacing-token.js' describe('tailwind spacing token transform', () => { const createMockToken = (path: string[]): Token => ({ name: 'test-token', value: '1rem', type: 'spacing', path, original: {}, filePath: '', isSource: false, }) as Token const createMockConfig = (): Config => ({ platforms: {}, source: [], include: [], }) const createMockPlatformConfig = (): PlatformConfig => ({ transforms: [], buildPath: '', files: [], }) describe('filter', () => { it('should return true for tokens with spacing as first path element', () => { const token = createMockToken(['spacing', '1', 'base']) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(true) }) it('should return false for tokens without spacing as first path element', () => { const testCases = [ ['color', 'primary', 'default'], ['typography', 'heading', 'h1'], ['border', 'width', 'thin'], ['shadow', 'small'], ['breakpoint', 'mobile'], ] testCases.forEach(path => { const token = createMockToken(path) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(false) }) }) it('should return false for empty path', () => { const token = createMockToken([]) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(false) }) it('should return false for single element path that is not spacing', () => { const token = createMockToken(['color']) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(false) }) it('should handle case sensitivity', () => { const testCases = [ { path: ['Spacing', '1'], expected: false }, { path: ['SPACING', '1'], expected: false }, { path: ['spacing', '1'], expected: true }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(expected) }) }) }) describe('transform', () => { it('should join path elements with hyphens, excluding base', () => { const token = createMockToken(['spacing', '1', 'base']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing-1') }) it('should handle path without base', () => { const token = createMockToken(['spacing', '1', 'mobile']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing-1-mobile') }) it('should handle path with multiple base occurrences', () => { const token = createMockToken(['spacing', 'base', '1', 'base']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing-1') }) it('should handle path with only spacing and base', () => { const token = createMockToken(['spacing', 'base']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing') }) it('should handle path with only spacing', () => { const token = createMockToken(['spacing']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing') }) it('should handle complex spacing paths', () => { const testCases = [ { path: ['spacing', '1', 'base'], expected: 'spacing-1' }, { path: ['spacing', '2', 'mobile'], expected: 'spacing-2-mobile' }, { path: ['spacing', '3', 'base', 'desktop'], expected: 'spacing-3-desktop', }, { path: ['spacing', '4', 'base', 'base', 'tablet'], expected: 'spacing-4-tablet', }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle path with special characters', () => { const testCases = [ { path: ['spacing', '1', 'with-dash'], expected: 'spacing-1-with-dash', }, { path: ['spacing', '1', 'with_underscore'], expected: 'spacing-1-with_underscore', }, { path: ['spacing', '1', 'with.dot'], expected: 'spacing-1-with.dot' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle path with numbers', () => { const testCases = [ { path: ['spacing', '1', 'base'], expected: 'spacing-1' }, { path: ['spacing', '2', 'mobile'], expected: 'spacing-2-mobile' }, { path: ['spacing', '10', 'desktop'], expected: 'spacing-10-desktop' }, { path: ['spacing', '0.5', 'tablet'], expected: 'spacing-0.5-tablet' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle path with mixed content', () => { const testCases = [ { path: ['spacing', '1', 'base', 'mobile', 'desktop'], expected: 'spacing-1-mobile-desktop', }, { path: ['spacing', '2', 'base', 'tablet', 'base'], expected: 'spacing-2-tablet', }, { path: ['spacing', '3', 'base', 'base', 'base'], expected: 'spacing-3', }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle edge cases', () => { const testCases = [ { path: ['spacing', 'base', 'base', 'base'], expected: 'spacing' }, { path: ['spacing', '1', 'base', 'base'], expected: 'spacing-1' }, { path: ['spacing', 'base', '1', 'base'], expected: 'spacing-1' }, { path: ['spacing', 'base', 'base', '1'], expected: 'spacing-1' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle empty path elements', () => { const token = createMockToken(['spacing', '', '1', 'base']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing--1') }) it('should handle path with whitespace', () => { const token = createMockToken(['spacing', '1', ' ', 'base']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('spacing-1- ') }) it('should handle path with base in different positions', () => { const testCases = [ { path: ['spacing', 'base', '1'], expected: 'spacing-1' }, { path: ['spacing', '1', 'base'], expected: 'spacing-1' }, { path: ['spacing', 'base', 'base', '1'], expected: 'spacing-1' }, { path: ['spacing', '1', 'base', 'base'], expected: 'spacing-1' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle path with base-like strings that are not base', () => { const testCases = [ { path: ['spacing', '1', 'baseball'], expected: 'spacing-1' }, { path: ['spacing', '1'], expected: 'spacing-1' }, { path: ['spacing', 'base'], expected: 'spacing' }, { path: ['spacing', '1', 'base'], expected: 'spacing-1' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should prepend prefix when config.prefix is set', () => { const token = createMockToken(['spacing', '1', 'base']) const config = { ...createMockPlatformConfig(), prefix: 'uy' } const result = transform(token, config) expect(result).toBe('uy-spacing-1') }) }) })