|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import { test, expect } from 'vitest'; |
| 4 | +import { buildThemedComponentsInternal } from '../internal'; |
| 5 | +import { Theme, resolveTheme, resolveContext } from '../../shared/theme'; |
| 6 | +import { isReferenceToken, generateReferenceTokenDefaults, generateReferenceTokenName } from '../../shared/theme/utils'; |
| 7 | +import { createBuildDeclarations } from '../../shared/declaration'; |
| 8 | +import { mkdtemp, rm } from 'fs/promises'; |
| 9 | +import { tmpdir } from 'os'; |
| 10 | +import { join } from 'path'; |
| 11 | + |
| 12 | +const testTheme: Theme = { |
| 13 | + id: 'test', |
| 14 | + selector: ':root', |
| 15 | + tokens: { |
| 16 | + colorPrimary: '#0073bb', |
| 17 | + colorBackground: '{colorPrimary}', |
| 18 | + colorPrimary500: '#direct-primary-500', // Different from reference token to test precedence |
| 19 | + colorSecondary: '{colorNeutral900}', // References a token that exists in both places |
| 20 | + colorNeutral900: '#direct-neutral', // Different from reference token |
| 21 | + }, |
| 22 | + modes: {}, |
| 23 | + contexts: {}, |
| 24 | + tokenModeMap: {}, |
| 25 | + referenceTokens: { |
| 26 | + color: { |
| 27 | + primary: { 500: '#reference-primary-value' }, // Also defined in tokens - tests precedence |
| 28 | + neutral: { 900: '#reference-neutral-value' }, // Also defined in tokens - tests precedence |
| 29 | + success: { 200: '#reference-success-value' }, // Only in reference tokens - tests auto-generation |
| 30 | + }, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +const propertiesMap = { |
| 35 | + colorPrimary: '--color-primary', |
| 36 | + colorBackground: '--color-background', |
| 37 | + colorPrimary500: '--color-primary-500', |
| 38 | + colorSecondary: '--color-secondary', |
| 39 | + colorNeutral900: '--color-neutral-900', |
| 40 | + colorSuccess200: '--color-success-200', // For the reference-only token |
| 41 | + colorError100: '--color-error-100', // For secondary theme reference token |
| 42 | +}; |
| 43 | + |
| 44 | +test('CSS variable optimization works without errors', async () => { |
| 45 | + const tempDir = await mkdtemp(join(tmpdir(), 'css-vars-test-')); |
| 46 | + |
| 47 | + try { |
| 48 | + await buildThemedComponentsInternal({ |
| 49 | + primary: testTheme, |
| 50 | + exposed: ['colorPrimary', 'colorBackground'], |
| 51 | + themeable: ['colorPrimary', 'colorBackground'], |
| 52 | + variablesMap: { |
| 53 | + colorPrimary: 'color-primary', |
| 54 | + colorBackground: 'color-background', |
| 55 | + }, |
| 56 | + componentsOutputDir: tempDir, |
| 57 | + scssDir: tempDir, |
| 58 | + useCssVars: true, |
| 59 | + skip: ['preset', 'design-tokens'], |
| 60 | + }); |
| 61 | + |
| 62 | + expect(true).toBe(true); |
| 63 | + } finally { |
| 64 | + await rm(tempDir, { recursive: true, force: true }); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +test('isReferenceToken identifies reference tokens correctly', () => { |
| 69 | + expect(isReferenceToken(testTheme, 'colorPrimary500')).toBe(true); |
| 70 | + expect(isReferenceToken(testTheme, 'colorNeutral900')).toBe(true); |
| 71 | + expect(isReferenceToken(testTheme, 'colorPrimary')).toBe(false); |
| 72 | + expect(isReferenceToken(testTheme, 'colorSuccess500')).toBe(false); // Not in referenceTokens |
| 73 | +}); |
| 74 | + |
| 75 | +test('generateReferenceTokenName creates correct token names', () => { |
| 76 | + expect(generateReferenceTokenName('primary', '500')).toBe('colorPrimary500'); |
| 77 | + expect(generateReferenceTokenName('neutral', '900')).toBe('colorNeutral900'); |
| 78 | +}); |
| 79 | + |
| 80 | +test('generateReferenceTokenDefaults creates CSS variable declarations', () => { |
| 81 | + const defaults = generateReferenceTokenDefaults(testTheme, propertiesMap); |
| 82 | + |
| 83 | + expect(defaults).toEqual({ |
| 84 | + colorPrimary500: '#reference-primary-value', |
| 85 | + colorNeutral900: '#reference-neutral-value', |
| 86 | + colorSuccess200: '#reference-success-value', |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +test('resolveTheme with CSS variables returns CSS var() for reference tokens', () => { |
| 91 | + const resolved = resolveTheme(testTheme, undefined, { |
| 92 | + useCssVars: true, |
| 93 | + propertiesMap, |
| 94 | + }); |
| 95 | + |
| 96 | + // Direct token takes precedence over reference token |
| 97 | + expect(resolved.colorPrimary500).toBe('#direct-primary-500'); |
| 98 | + // Token referencing a reference token should get CSS var |
| 99 | + expect(resolved.colorSecondary).toBe('var(--color-neutral-900)'); |
| 100 | + expect(resolved.colorPrimary).toBe('#0073bb'); // Not a reference token |
| 101 | +}); |
| 102 | + |
| 103 | +test('token precedence: direct tokens override reference tokens', () => { |
| 104 | + const resolved = resolveTheme(testTheme, undefined); |
| 105 | + |
| 106 | + // Direct token value should be used, not reference token value |
| 107 | + expect(resolved.colorPrimary500).toBe('#direct-primary-500'); |
| 108 | + expect(resolved.colorNeutral900).toBe('#direct-neutral'); |
| 109 | +}); |
| 110 | + |
| 111 | +test('resolveContext with CSS variables preserves var() references', () => { |
| 112 | + const context = { |
| 113 | + id: 'test-context', |
| 114 | + selector: '.test', |
| 115 | + tokens: { |
| 116 | + colorSecondary: '{colorNeutral900}', // References a token that should become CSS var |
| 117 | + }, |
| 118 | + }; |
| 119 | + |
| 120 | + const resolved = resolveContext(testTheme, context, undefined, undefined, { |
| 121 | + useCssVars: true, |
| 122 | + propertiesMap, |
| 123 | + }); |
| 124 | + |
| 125 | + // Should resolve to CSS var since colorNeutral900 is a reference token |
| 126 | + expect(resolved.colorSecondary).toBe('var(--color-neutral-900)'); |
| 127 | +}); |
| 128 | + |
| 129 | +test('createBuildDeclarations includes reference tokens when useCssVars enabled', () => { |
| 130 | + const css = createBuildDeclarations(testTheme, [], propertiesMap, (selector) => selector, ['colorPrimary'], { |
| 131 | + useCssVars: true, |
| 132 | + propertiesMap, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(css).toContain('--color-primary-500'); |
| 136 | + expect(css).toContain('--color-neutral-900'); |
| 137 | +}); |
| 138 | + |
| 139 | +test('createBuildDeclarations with secondary theme generates reference token CSS variables', () => { |
| 140 | + const secondaryTheme: Theme = { |
| 141 | + id: 'dark', |
| 142 | + selector: '[data-theme="dark"]', |
| 143 | + tokens: { |
| 144 | + colorPrimary: '#1a73e8', |
| 145 | + colorPrimary500: '#secondary-primary-500', |
| 146 | + }, |
| 147 | + modes: {}, |
| 148 | + contexts: {}, |
| 149 | + tokenModeMap: {}, |
| 150 | + referenceTokens: { |
| 151 | + color: { |
| 152 | + primary: { 500: '#secondary-reference-value' }, |
| 153 | + error: { 100: '#secondary-error-value' }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }; |
| 157 | + |
| 158 | + const css = createBuildDeclarations( |
| 159 | + testTheme, |
| 160 | + [secondaryTheme], |
| 161 | + propertiesMap, |
| 162 | + (selector) => selector, |
| 163 | + ['colorPrimary', 'colorError100'], |
| 164 | + { |
| 165 | + useCssVars: true, |
| 166 | + propertiesMap, |
| 167 | + } |
| 168 | + ); |
| 169 | + |
| 170 | + // Should contain reference tokens from both primary and secondary themes |
| 171 | + expect(css).toContain('--color-primary-500'); |
| 172 | + expect(css).toContain('[data-theme="dark"]'); |
| 173 | + expect(css).toContain('--color-error-100'); // Now working - reference token from secondary theme |
| 174 | +}); |
0 commit comments