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-text-token.js' describe('tailwind text token transform', () => { const createMockToken = (path: string[]): Token => ({ name: 'test-token', value: '400 1rem/1.5 Inter', type: 'typography', 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 text as first path element', () => { const token = createMockToken(['text', 'heading', 'h1']) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(true) }) it('should return false for tokens without text as first path element', () => { const testCases = [ ['color', 'primary', 'default'], ['spacing', '1', 'base'], ['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 text', () => { const token = createMockToken(['color']) const config = createMockConfig() const result = filter(token, config) expect(result).toBe(false) }) it('should handle case sensitivity', () => { const testCases = [ { path: ['Text', 'heading'], expected: false }, { path: ['TEXT', 'heading'], expected: false }, { path: ['text', 'heading'], 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 return text-{size} when second path element does not contain --', () => { const token = createMockToken(['text', 'heading']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text-heading') }) it('should return text-{size}--{suffix} when second path element contains --', () => { const token = createMockToken(['text', 'heading--large']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text-heading--large') }) it('should handle path with only text element', () => { const token = createMockToken(['text']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text-undefined') }) it('should handle path with text and empty second element', () => { const token = createMockToken(['text', '']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text-') }) it('should handle complex text paths without --', () => { const testCases = [ { path: ['text', 'heading'], expected: 'text-heading' }, { path: ['text', 'body'], expected: 'text-body' }, { path: ['text', 'caption'], expected: 'text-caption' }, { path: ['text', 'label'], expected: 'text-label' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle complex text paths with --', () => { const testCases = [ { path: ['text', 'heading--large'], expected: 'text-heading--large' }, { path: ['text', 'body--small'], expected: 'text-body--small' }, { path: ['text', 'caption--medium'], expected: 'text-caption--medium' }, { path: ['text', 'label--bold'], expected: 'text-label--bold' }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle text paths with numbers', () => { const testCases = [ { path: ['text', 'heading-1'], expected: 'text-heading-1' }, { path: ['text', 'body-2'], expected: 'text-body-2' }, { path: ['text', 'caption-3--large'], expected: 'text-caption-3--large', }, ] testCases.forEach(({ path, expected }) => { const token = createMockToken(path) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe(expected) }) }) it('should handle text paths with special characters', () => { const testCases = [ { path: ['text', 'heading-with-dash'], expected: 'text-heading-with-dash', }, { path: ['text', 'body_with_underscore'], expected: 'text-body_with_underscore', }, { path: ['text', 'caption.with.dot'], expected: 'text-caption.with.dot', }, { path: ['text', 'label--with-dash'], expected: 'text-label--with-dash', }, ] 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 with -- at different positions', () => { const testCases = [ { path: ['text', '--heading'], expected: 'text---heading' }, { path: ['text', 'heading--'], expected: 'text-heading--' }, { path: ['text', '--'], expected: 'text---' }, { path: ['text', '---'], expected: 'text----' }, ] 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 whitespace', () => { const testCases = [ { path: ['text', ' heading'], expected: 'text- heading' }, { path: ['text', 'heading '], expected: 'text-heading ' }, { path: ['text', ' heading '], expected: 'text- heading ' }, { path: ['text', 'heading-- large'], expected: 'text-heading-- large' }, ] 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 only -- in second element', () => { const token = createMockToken(['text', '--']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text---') }) it('should handle path with empty string after --', () => { const token = createMockToken(['text', 'heading--']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text-heading--') }) it('should handle path with empty string before --', () => { const token = createMockToken(['text', '--large']) const config = createMockPlatformConfig() const result = transform(token, config) expect(result).toBe('text---large') }) it('should handle path with mixed content and --', () => { const testCases = [ { path: ['text', 'heading-large--bold'], expected: 'text-heading-large--bold', }, { path: ['text', 'body-small--italic'], expected: 'text-body-small--italic', }, { path: ['text', 'caption-medium--underline'], expected: 'text-caption-medium--underline', }, ] 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(['text', 'heading']) const config = { ...createMockPlatformConfig(), prefix: 'uy' } const result = transform(token, config) expect(result).toBe('uy-text-heading') }) it('should prepend prefix with -- variant when config.prefix is set', () => { const token = createMockToken(['text', 'heading--large']) const config = { ...createMockPlatformConfig(), prefix: 'uy' } const result = transform(token, config) expect(result).toBe('uy-text-heading--large') }) }) })