|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { _evaluateCssVariableExpression } from '.'; |
| 4 | + |
| 5 | +function evaluate(value: string, variables: Record<string, string> = {}) { |
| 6 | + const view = { style: { getCssVariable: (name: string) => (name in variables ? variables[name] : null) } }; |
| 7 | + return _evaluateCssVariableExpression(view as any, 'color', value); |
| 8 | +} |
| 9 | + |
| 10 | +describe('_evaluateCssVariableExpression', () => { |
| 11 | + it('substitutes defined variables and falls back for undefined ones', () => { |
| 12 | + expect(evaluate('var(--a)', { '--a': 'blue' })).toBe('blue'); |
| 13 | + expect(evaluate('var(--a, red)', { '--a': 'blue' })).toBe('blue'); |
| 14 | + expect(evaluate('var(--missing, red)')).toBe('red'); |
| 15 | + expect(evaluate('var(--missing)')).toBe('unset'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('resolves nested fallbacks', () => { |
| 19 | + expect(evaluate('var(--missing, var(--b))', { '--b': 'lime' })).toBe('lime'); |
| 20 | + expect(evaluate('var(--missing, var(--missing-too, yellow))')).toBe('yellow'); |
| 21 | + expect(evaluate('var(--missing, var(--missing-too))')).toBe('unset'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('keeps parentheses inside the fallback', () => { |
| 25 | + expect(evaluate('var(--missing, rgb(0, 0, 255))')).toBe('rgb(0, 0, 255)'); |
| 26 | + expect(evaluate('linear-gradient(var(--missing, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%))')).toBe('linear-gradient(rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('keeps every comma-separated part of the fallback', () => { |
| 30 | + expect(evaluate('var(--missing, red 0%, blue 100%)')).toBe('red 0%, blue 100%'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('uses the fallback for a variable set to initial', () => { |
| 34 | + expect(evaluate('var(--a, red)', { '--a': 'initial' })).toBe('red'); |
| 35 | + expect(evaluate('var(--a)', { '--a': 'initial' })).toBe('unset'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('resolves a gradient built from variables with fallbacks', () => { |
| 39 | + const variables = { |
| 40 | + '--via-stops': 'initial', |
| 41 | + '--position': 'to right in oklab', |
| 42 | + '--from': 'rgb(98, 96, 255)', |
| 43 | + '--from-position': '0%', |
| 44 | + '--to': 'rgb(246, 51, 154)', |
| 45 | + '--to-position': '100%', |
| 46 | + }; |
| 47 | + expect(evaluate('linear-gradient(var(--via-stops, var(--position), var(--from) var(--from-position), var(--to) var(--to-position)))', variables)).toBe( |
| 48 | + 'linear-gradient(to right in oklab, rgb(98, 96, 255) 0%, rgb(246, 51, 154) 100%)', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('leaves values without variables alone', () => { |
| 53 | + expect(evaluate('rgb(0, 0, 0)')).toBe('rgb(0, 0, 0)'); |
| 54 | + }); |
| 55 | +}); |
0 commit comments