|
| 1 | +import createVisibilityExpression from './visibility'; |
| 2 | +import {describe, test, expect, vi} from 'vitest'; |
| 3 | + |
| 4 | +describe('evaluate visibility expression', () => { |
| 5 | + test('literal values', () => { |
| 6 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 7 | + |
| 8 | + expect(createVisibilityExpression('none', {}).evaluate()).toBe('none'); |
| 9 | + expect(console.warn).not.toHaveBeenCalled(); |
| 10 | + |
| 11 | + expect(createVisibilityExpression('visible', {}).evaluate()).toBe('visible'); |
| 12 | + expect(console.warn).not.toHaveBeenCalled(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('global state property as visibility expression', () => { |
| 16 | + const globalState = {}; |
| 17 | + const value = createVisibilityExpression(['global-state', 'x'], globalState); |
| 18 | + |
| 19 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 20 | + |
| 21 | + globalState.x = 'none'; |
| 22 | + expect(value.evaluate()).toBe('none'); |
| 23 | + expect(console.warn).not.toHaveBeenCalled(); |
| 24 | + |
| 25 | + globalState.x = 'visible'; |
| 26 | + expect(value.evaluate()).toBe('visible'); |
| 27 | + expect(console.warn).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + test('global state flag as visibility expression', () => { |
| 31 | + const globalState = {}; |
| 32 | + const value = createVisibilityExpression(['case', ['global-state', 'x'], 'visible', 'none'], globalState); |
| 33 | + |
| 34 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 35 | + |
| 36 | + globalState.x = false; |
| 37 | + expect(value.evaluate()).toBe('none'); |
| 38 | + expect(console.warn).not.toHaveBeenCalled(); |
| 39 | + |
| 40 | + globalState.x = true; |
| 41 | + expect(value.evaluate()).toBe('visible'); |
| 42 | + expect(console.warn).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + test('warns and falls back to default for invalid expression', () => { |
| 46 | + const value = createVisibilityExpression(['get', 'x'], {}); |
| 47 | + |
| 48 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 49 | + |
| 50 | + expect(value.evaluate()).toBe('visible'); |
| 51 | + expect(console.warn).toHaveBeenCalledWith('Expected value to be of type string, but found null instead.'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('warns and falls back to default for missing global property', () => { |
| 55 | + const value = createVisibilityExpression(['global-state', 'x'], {}); |
| 56 | + |
| 57 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 58 | + |
| 59 | + expect(value.evaluate()).toBe('visible'); |
| 60 | + expect(console.warn).toHaveBeenCalledWith('Expected value to be of type string, but found null instead.'); |
| 61 | + }); |
| 62 | + |
| 63 | + test('warns and falls back to default for invalid global property', () => { |
| 64 | + const value = createVisibilityExpression(['global-state', 'x'], {x: 'invalid'}); |
| 65 | + |
| 66 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 67 | + |
| 68 | + expect(value.evaluate()).toBe('visible'); |
| 69 | + expect(console.warn).toHaveBeenCalledWith('Expected value to be one of "visible", "none", but found "invalid" instead.'); |
| 70 | + }); |
| 71 | +}); |
0 commit comments