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