|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { buildDesignSystem } from '../../design-system' |
| 3 | +import { Theme } from '../../theme' |
| 4 | +import { createCompatConfig } from './create-compat-config' |
| 5 | +import { resolveConfig } from './resolve-config' |
| 6 | + |
| 7 | +function buildCompatConfig(cssValues: Record<string, string>) { |
| 8 | + let theme = new Theme() |
| 9 | + for (let [key, value] of Object.entries(cssValues)) { |
| 10 | + theme.add(key, value) |
| 11 | + } |
| 12 | + let design = buildDesignSystem(theme) |
| 13 | + |
| 14 | + let { resolvedConfig } = resolveConfig(design, [ |
| 15 | + { config: createCompatConfig(design.theme), base: '/root', reference: true, src: undefined }, |
| 16 | + ]) |
| 17 | + |
| 18 | + return resolvedConfig |
| 19 | +} |
| 20 | + |
| 21 | +describe('theme namespace lookups', () => { |
| 22 | + test('a namespace that only defines a bare value resolves to `{ DEFAULT: … }`, not a string', () => { |
| 23 | + // When the CSS theme only defines `--text` (a single bare value), |
| 24 | + // `theme('text', {})` used to return the string `'1rem'`. |
| 25 | + // |
| 26 | + // Spreading that string, like the compat config does, produced char-indexed |
| 27 | + // garbage (`{ '0': '1', '1': 'r', … }`) instead of `{ DEFAULT: '1rem' }`. |
| 28 | + let config = buildCompatConfig({ '--text': '1rem' }) |
| 29 | + |
| 30 | + expect(config.theme?.fontSize?.DEFAULT).toBe('1rem') |
| 31 | + expect(config.theme?.fontSize).not.toHaveProperty('0') |
| 32 | + }) |
| 33 | + |
| 34 | + test('a bare value is kept alongside suffixed values in the same namespace', () => { |
| 35 | + let config = buildCompatConfig({ |
| 36 | + '--text': '1rem', |
| 37 | + '--text-sm': '0.875rem', |
| 38 | + }) |
| 39 | + |
| 40 | + expect(config.theme?.fontSize?.DEFAULT).toBe('1rem') |
| 41 | + expect(config.theme?.fontSize?.sm).toBe('0.875rem') |
| 42 | + }) |
| 43 | + |
| 44 | + test('a naive spread of a theme namespace in a user config is safe', () => { |
| 45 | + let theme = new Theme() |
| 46 | + theme.add('--text', '1rem') |
| 47 | + let design = buildDesignSystem(theme) |
| 48 | + |
| 49 | + let { resolvedConfig } = resolveConfig(design, [ |
| 50 | + { |
| 51 | + config: { |
| 52 | + theme: { |
| 53 | + extend: { |
| 54 | + fontSize: ({ theme }: any) => ({ ...theme('text', {}) }), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + base: '/root', |
| 59 | + reference: true, |
| 60 | + src: undefined, |
| 61 | + }, |
| 62 | + ]) |
| 63 | + |
| 64 | + expect(resolvedConfig.theme?.fontSize?.DEFAULT).toBe('1rem') |
| 65 | + expect(resolvedConfig.theme?.fontSize).not.toHaveProperty('0') |
| 66 | + }) |
| 67 | + |
| 68 | + test('namespace root lookups resolve to an object, key lookups resolve to the value', () => { |
| 69 | + let theme = new Theme() |
| 70 | + theme.add('--text', '1rem') |
| 71 | + theme.add('--animate-spin', 'spin 1s linear infinite') |
| 72 | + let design = buildDesignSystem(theme) |
| 73 | + |
| 74 | + let root: unknown |
| 75 | + let rootWithDefault: unknown |
| 76 | + let leaf: unknown |
| 77 | + |
| 78 | + resolveConfig(design, [ |
| 79 | + { |
| 80 | + config: { |
| 81 | + theme: { |
| 82 | + extend: { |
| 83 | + fontSize: ({ theme }: any) => { |
| 84 | + root = theme('text') |
| 85 | + rootWithDefault = theme('text', {}) |
| 86 | + leaf = theme('animate.spin') |
| 87 | + return {} |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + base: '/root', |
| 93 | + reference: true, |
| 94 | + src: undefined, |
| 95 | + }, |
| 96 | + ]) |
| 97 | + |
| 98 | + // A namespace root lookup always resolves to an object, like in v3, even |
| 99 | + // when the namespace only contains a bare value |
| 100 | + expect(root).toMatchObject({ DEFAULT: '1rem' }) |
| 101 | + expect(rootWithDefault).toMatchObject({ DEFAULT: '1rem' }) |
| 102 | + |
| 103 | + // Lookups of a specific key resolve to the value itself |
| 104 | + expect(leaf).toBe('spin 1s linear infinite') |
| 105 | + }) |
| 106 | + |
| 107 | + test('lookups of a specific key still resolve to the value itself', () => { |
| 108 | + let theme = new Theme() |
| 109 | + theme.add('--animate-spin', 'spin 1s linear infinite') |
| 110 | + let design = buildDesignSystem(theme) |
| 111 | + |
| 112 | + let { resolvedConfig } = resolveConfig(design, [ |
| 113 | + { |
| 114 | + config: { |
| 115 | + theme: { |
| 116 | + extend: { |
| 117 | + animation: ({ theme }: any) => ({ spin: theme('animate.spin') }), |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + base: '/root', |
| 122 | + reference: true, |
| 123 | + src: undefined, |
| 124 | + }, |
| 125 | + ]) |
| 126 | + |
| 127 | + expect(resolvedConfig.theme?.animation?.spin).toBe('spin 1s linear infinite') |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +describe('createCompatConfig namespace mappings', () => { |
| 132 | + test('fontSize maps from CSS text namespace', () => { |
| 133 | + let config = buildCompatConfig({ '--text-base': '1rem' }) |
| 134 | + expect(config.theme?.fontSize?.base).toBe('1rem') |
| 135 | + }) |
| 136 | + |
| 137 | + test('boxShadow maps from CSS shadow namespace', () => { |
| 138 | + let config = buildCompatConfig({ '--shadow-md': '0 4px 6px #000' }) |
| 139 | + expect(config.theme?.boxShadow?.md).toBe('0 4px 6px #000') |
| 140 | + }) |
| 141 | + |
| 142 | + test('animation maps from CSS animate namespace', () => { |
| 143 | + let config = buildCompatConfig({ '--animate-spin': 'spin 1s linear infinite' }) |
| 144 | + expect(config.theme?.animation?.spin).toBe('spin 1s linear infinite') |
| 145 | + }) |
| 146 | + |
| 147 | + test('aspectRatio maps from CSS aspect namespace', () => { |
| 148 | + let config = buildCompatConfig({ '--aspect-square': '1 / 1' }) |
| 149 | + expect(config.theme?.aspectRatio?.square).toBe('1 / 1') |
| 150 | + }) |
| 151 | + |
| 152 | + test('borderRadius maps from CSS radius namespace', () => { |
| 153 | + let config = buildCompatConfig({ '--radius-lg': '0.5rem' }) |
| 154 | + expect(config.theme?.borderRadius?.lg).toBe('0.5rem') |
| 155 | + }) |
| 156 | + |
| 157 | + test('screens maps from CSS breakpoint namespace', () => { |
| 158 | + let config = buildCompatConfig({ '--breakpoint-sm': '640px' }) |
| 159 | + expect(config.theme?.screens?.sm).toBe('640px') |
| 160 | + }) |
| 161 | + |
| 162 | + test('letterSpacing maps from CSS tracking namespace', () => { |
| 163 | + let config = buildCompatConfig({ '--tracking-wide': '0.025em' }) |
| 164 | + expect(config.theme?.letterSpacing?.wide).toBe('0.025em') |
| 165 | + }) |
| 166 | + |
| 167 | + test('lineHeight maps from CSS leading namespace', () => { |
| 168 | + let config = buildCompatConfig({ '--leading-relaxed': '1.75' }) |
| 169 | + expect(config.theme?.lineHeight?.relaxed).toBe('1.75') |
| 170 | + }) |
| 171 | + |
| 172 | + test('maxWidth maps from the container namespace in the default theme', () => { |
| 173 | + let config = buildCompatConfig({}) |
| 174 | + // The default theme has container.sm = '24rem' |
| 175 | + expect(config.theme?.maxWidth?.sm).toBe('24rem') |
| 176 | + }) |
| 177 | + |
| 178 | + test('transitionDuration gets DEFAULT from --default-transition-duration', () => { |
| 179 | + let config = buildCompatConfig({ '--default-transition-duration': '150ms' }) |
| 180 | + expect(config.theme?.transitionDuration?.DEFAULT).toBe('150ms') |
| 181 | + }) |
| 182 | + |
| 183 | + test('transitionTimingFunction gets DEFAULT from --default-transition-timing-function', () => { |
| 184 | + let config = buildCompatConfig({ |
| 185 | + '--default-transition-timing-function': 'cubic-bezier(0.4, 0, 0.2, 1)', |
| 186 | + }) |
| 187 | + expect(config.theme?.transitionTimingFunction?.DEFAULT).toBe('cubic-bezier(0.4, 0, 0.2, 1)') |
| 188 | + }) |
| 189 | +}) |
0 commit comments