|
| 1 | +/** |
| 2 | + * Tests for useExpression and useCondition hooks |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from 'vitest'; |
| 6 | +import { renderHook } from '@testing-library/react'; |
| 7 | +import { useExpression, useCondition } from '../useExpression'; |
| 8 | + |
| 9 | +describe('useExpression', () => { |
| 10 | + it('returns string value directly for non-expression strings', () => { |
| 11 | + const { result } = renderHook(() => useExpression('hello')); |
| 12 | + |
| 13 | + expect(result.current).toBe('hello'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns number value directly', () => { |
| 17 | + const { result } = renderHook(() => useExpression(42)); |
| 18 | + |
| 19 | + expect(result.current).toBe(42); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns boolean value directly', () => { |
| 23 | + const { result } = renderHook(() => useExpression(true)); |
| 24 | + |
| 25 | + expect(result.current).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns null for null expressions', () => { |
| 29 | + const { result } = renderHook(() => useExpression(null)); |
| 30 | + |
| 31 | + expect(result.current).toBeNull(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns undefined for undefined expressions', () => { |
| 35 | + const { result } = renderHook(() => useExpression(undefined)); |
| 36 | + |
| 37 | + expect(result.current).toBeUndefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('evaluates expressions with ${...} syntax', () => { |
| 41 | + const context = { data: { name: 'John' } }; |
| 42 | + const { result } = renderHook(() => |
| 43 | + useExpression('${data.name}', context), |
| 44 | + ); |
| 45 | + |
| 46 | + expect(result.current).toBe('John'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('evaluates expressions with context data', () => { |
| 50 | + const context = { data: { age: 25 } }; |
| 51 | + const { result } = renderHook(() => |
| 52 | + useExpression('${data.age > 18}', context), |
| 53 | + ); |
| 54 | + |
| 55 | + expect(result.current).toBe(true); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('useCondition', () => { |
| 60 | + it('returns true for boolean true', () => { |
| 61 | + const { result } = renderHook(() => useCondition(true)); |
| 62 | + |
| 63 | + expect(result.current).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns false for boolean false', () => { |
| 67 | + const { result } = renderHook(() => useCondition(false)); |
| 68 | + |
| 69 | + expect(result.current).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns true for undefined', () => { |
| 73 | + const { result } = renderHook(() => useCondition(undefined)); |
| 74 | + |
| 75 | + expect(result.current).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('evaluates string conditions with context', () => { |
| 79 | + const context = { data: { status: 'active' } }; |
| 80 | + const { result } = renderHook(() => |
| 81 | + useCondition('${data.status === "active"}', context), |
| 82 | + ); |
| 83 | + |
| 84 | + expect(result.current).toBe(true); |
| 85 | + }); |
| 86 | +}); |
0 commit comments