|
| 1 | +import { normalizeEmbeddedViewConfig } from './normalizeEmbeddedViewConfig'; |
| 2 | + |
| 3 | +describe('normalizeEmbeddedViewConfig', () => { |
| 4 | + let warnSpy: jest.SpyInstance; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + warnSpy.mockRestore(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns null or undefined unchanged', () => { |
| 15 | + expect(normalizeEmbeddedViewConfig(null)).toBeNull(); |
| 16 | + expect(normalizeEmbeddedViewConfig(undefined)).toBeUndefined(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('parses numeric strings for borderWidth and borderCornerRadius', () => { |
| 20 | + const input = { |
| 21 | + borderWidth: '45', |
| 22 | + borderCornerRadius: '12.5', |
| 23 | + backgroundColor: '#fff', |
| 24 | + }; |
| 25 | + |
| 26 | + // Runtime JSON / native payloads may use strings for numeric fields. |
| 27 | + const result = normalizeEmbeddedViewConfig(input as never); |
| 28 | + |
| 29 | + expect(result).toEqual({ |
| 30 | + borderWidth: 45, |
| 31 | + borderCornerRadius: 12.5, |
| 32 | + backgroundColor: '#fff', |
| 33 | + }); |
| 34 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('trims whitespace before parsing numeric strings', () => { |
| 38 | + const result = normalizeEmbeddedViewConfig({ |
| 39 | + borderWidth: ' 8 ', |
| 40 | + } as never); |
| 41 | + |
| 42 | + expect(result?.borderWidth).toBe(8); |
| 43 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('leaves valid numbers unchanged', () => { |
| 47 | + const result = normalizeEmbeddedViewConfig({ |
| 48 | + borderWidth: 3, |
| 49 | + borderCornerRadius: 0, |
| 50 | + }); |
| 51 | + |
| 52 | + expect(result?.borderWidth).toBe(3); |
| 53 | + expect(result?.borderCornerRadius).toBe(0); |
| 54 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('drops non-parsable strings and warns', () => { |
| 58 | + const result = normalizeEmbeddedViewConfig({ |
| 59 | + borderWidth: 'nope', |
| 60 | + borderCornerRadius: '10', |
| 61 | + } as never); |
| 62 | + |
| 63 | + expect(result?.borderWidth).toBeUndefined(); |
| 64 | + expect(result?.borderCornerRadius).toBe(10); |
| 65 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 66 | + expect(warnSpy.mock.calls[0][0]).toContain('borderWidth'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('drops empty strings and warns', () => { |
| 70 | + const result = normalizeEmbeddedViewConfig({ |
| 71 | + borderWidth: ' ', |
| 72 | + } as never); |
| 73 | + |
| 74 | + expect(result?.borderWidth).toBeUndefined(); |
| 75 | + expect(warnSpy).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('drops NaN and Infinity numbers and warns', () => { |
| 79 | + const result = normalizeEmbeddedViewConfig({ |
| 80 | + borderWidth: Number.NaN, |
| 81 | + borderCornerRadius: Number.POSITIVE_INFINITY, |
| 82 | + } as never); |
| 83 | + |
| 84 | + expect(result?.borderWidth).toBeUndefined(); |
| 85 | + expect(result?.borderCornerRadius).toBeUndefined(); |
| 86 | + expect(warnSpy).toHaveBeenCalledTimes(2); |
| 87 | + }); |
| 88 | + |
| 89 | + it('drops invalid types and warns', () => { |
| 90 | + const result = normalizeEmbeddedViewConfig({ |
| 91 | + borderWidth: true, |
| 92 | + } as never); |
| 93 | + |
| 94 | + expect(result?.borderWidth).toBeUndefined(); |
| 95 | + expect(warnSpy).toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does not mutate the original config object', () => { |
| 99 | + const original = { borderWidth: '7' as const }; |
| 100 | + const snapshot = { ...original }; |
| 101 | + |
| 102 | + normalizeEmbeddedViewConfig(original as never); |
| 103 | + |
| 104 | + expect(original).toEqual(snapshot); |
| 105 | + }); |
| 106 | +}); |
0 commit comments