|
| 1 | +import { isPlatformColorSentinel, safeMerge } from '../provider'; |
| 2 | + |
| 3 | +describe('isPlatformColorSentinel', () => { |
| 4 | + it('detects iOS PlatformColor (semantic)', () => { |
| 5 | + expect(isPlatformColorSentinel({ semantic: ['label'] })).toBe(true); |
| 6 | + }); |
| 7 | + |
| 8 | + it('detects iOS DynamicColorIOS (dynamic)', () => { |
| 9 | + expect( |
| 10 | + isPlatformColorSentinel({ dynamic: { light: '#fff', dark: '#000' } }) |
| 11 | + ).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it('detects Android PlatformColor (resource_paths)', () => { |
| 15 | + expect( |
| 16 | + isPlatformColorSentinel({ resource_paths: ['@android:color/black'] }) |
| 17 | + ).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('rejects plain objects, primitives, null, and arrays', () => { |
| 21 | + expect(isPlatformColorSentinel({ primary: '#fff' })).toBe(false); |
| 22 | + expect(isPlatformColorSentinel('#fff')).toBe(false); |
| 23 | + expect(isPlatformColorSentinel(null)).toBe(false); |
| 24 | + expect(isPlatformColorSentinel(undefined)).toBe(false); |
| 25 | + expect(isPlatformColorSentinel([1, 2, 3])).toBe(false); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('safeMerge', () => { |
| 30 | + it('deep-merges plain objects, overrides win at leaves', () => { |
| 31 | + const base = { a: 1, nested: { x: 1, y: 2 } }; |
| 32 | + const overrides = { nested: { y: 20, z: 30 } }; |
| 33 | + |
| 34 | + expect(safeMerge(base, overrides)).toEqual({ |
| 35 | + a: 1, |
| 36 | + nested: { x: 1, y: 20, z: 30 }, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns a new object reference (does not mutate base)', () => { |
| 41 | + const base = { nested: { x: 1 } }; |
| 42 | + const overrides = { nested: { y: 2 } }; |
| 43 | + const result = safeMerge(base, overrides); |
| 44 | + |
| 45 | + expect(result).not.toBe(base); |
| 46 | + expect(result.nested).not.toBe(base.nested); |
| 47 | + expect(base).toEqual({ nested: { x: 1 } }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('falls back to base when overrides is null/undefined', () => { |
| 51 | + const base = { a: 1 }; |
| 52 | + expect(safeMerge(base, null)).toEqual(base); |
| 53 | + expect(safeMerge(base, undefined)).toEqual(base); |
| 54 | + }); |
| 55 | + |
| 56 | + it('replaces arrays instead of merging', () => { |
| 57 | + const base = { list: [1, 2, 3] }; |
| 58 | + const overrides = { list: [9] }; |
| 59 | + expect(safeMerge(base, overrides)).toEqual({ list: [9] }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('treats iOS semantic sentinel as a leaf (no recursion into array)', () => { |
| 63 | + const sentinel = { semantic: ['label'] }; |
| 64 | + const base = { colors: { primary: '#000' } }; |
| 65 | + const overrides = { colors: { primary: sentinel } }; |
| 66 | + |
| 67 | + const result = safeMerge<typeof base & { colors: { primary: unknown } }>( |
| 68 | + base, |
| 69 | + overrides |
| 70 | + ); |
| 71 | + expect(result.colors.primary).toBe(sentinel); |
| 72 | + }); |
| 73 | + |
| 74 | + it('treats DynamicColorIOS sentinel as a leaf', () => { |
| 75 | + const sentinel = { dynamic: { light: '#fff', dark: '#000' } }; |
| 76 | + const base = { colors: { primary: sentinel } }; |
| 77 | + const overrides = { colors: { primary: '#abc' } }; |
| 78 | + |
| 79 | + const result = safeMerge<typeof base & { colors: { primary: unknown } }>( |
| 80 | + base, |
| 81 | + overrides |
| 82 | + ); |
| 83 | + expect(result.colors.primary).toBe('#abc'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('treats Android resource_paths sentinel as a leaf', () => { |
| 87 | + const sentinelBase = { resource_paths: ['@android:color/black'] }; |
| 88 | + const sentinelOverride = { resource_paths: ['@android:color/white'] }; |
| 89 | + const base = { colors: { primary: sentinelBase } }; |
| 90 | + const overrides = { colors: { primary: sentinelOverride } }; |
| 91 | + |
| 92 | + const result = safeMerge<typeof base & { colors: { primary: unknown } }>( |
| 93 | + base, |
| 94 | + overrides |
| 95 | + ); |
| 96 | + expect(result.colors.primary).toBe(sentinelOverride); |
| 97 | + }); |
| 98 | + |
| 99 | + it('preserves sentinel siblings when merging a colors map', () => { |
| 100 | + const sentinel = { semantic: ['label'] }; |
| 101 | + const base = { |
| 102 | + colors: { primary: sentinel, secondary: '#111', tertiary: '#222' }, |
| 103 | + }; |
| 104 | + const overrides = { colors: { secondary: '#999' } }; |
| 105 | + |
| 106 | + const result = safeMerge<typeof base & { colors: Record<string, unknown> }>( |
| 107 | + base, |
| 108 | + overrides |
| 109 | + ); |
| 110 | + expect(result.colors.primary).toBe(sentinel); |
| 111 | + expect(result.colors.secondary).toBe('#999'); |
| 112 | + expect(result.colors.tertiary).toBe('#222'); |
| 113 | + }); |
| 114 | +}); |
0 commit comments