Skip to content

Commit 801db8c

Browse files
committed
test(mergeProps): expand tests for className and style merging, including handling of undefined and null values
1 parent 5e8788b commit 801db8c

1 file changed

Lines changed: 78 additions & 14 deletions

File tree

tests/mergeProps.test.ts

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ describe('mergeProps', () => {
77
expect(mergeProps(a, b)).toEqual({ foo: 1, bar: 3, baz: 4 });
88
});
99

10-
it('merges className', () => {
11-
const a = { className: 'a' };
12-
const b = { className: 'b' };
13-
expect(mergeProps(a, b)).toEqual({ className: 'a b' });
14-
});
15-
16-
it('merges style', () => {
17-
const a = { style: { color: 'red' } };
18-
const b = { style: { backgroundColor: 'blue' } };
19-
expect(mergeProps(a, b)).toEqual({
20-
style: { color: 'red', backgroundColor: 'blue' },
21-
});
22-
});
23-
2410
it('excludes keys with undefined values', () => {
2511
const a = { foo: 1, bar: undefined };
2612
const b = { bar: 2 };
@@ -63,4 +49,82 @@ describe('mergeProps', () => {
6349
it('handles empty objects', () => {
6450
expect(mergeProps({}, { a: 1 }, {})).toEqual({ a: 1 });
6551
});
52+
53+
describe('className', () => {
54+
it('merges strings', () => {
55+
const a = { className: 'a' };
56+
const b = { className: 'b' };
57+
expect(mergeProps(a, b)).toEqual({ className: 'a b' });
58+
});
59+
60+
it('keeps previous when later is undefined', () => {
61+
expect(mergeProps({ className: 'a' }, { className: undefined })).toEqual({
62+
className: 'a',
63+
});
64+
});
65+
66+
it('omits key when only undefined', () => {
67+
expect(
68+
(mergeProps as (...items: any[]) => any)({ className: undefined }),
69+
).toEqual({});
70+
});
71+
72+
it('null merges like empty string', () => {
73+
expect(
74+
mergeProps({ className: 'a' }, { className: null as any }),
75+
).toEqual({ className: 'a' });
76+
expect(
77+
mergeProps({ className: null as any }, { className: 'b' }),
78+
).toEqual({ className: 'b' });
79+
expect(
80+
(mergeProps as (...items: any[]) => any)({ className: null as any }),
81+
).toEqual({ className: '' });
82+
});
83+
});
84+
85+
describe('style', () => {
86+
it('merges objects', () => {
87+
const a = { style: { color: 'red' } };
88+
const b = { style: { backgroundColor: 'blue' } };
89+
expect(mergeProps(a, b)).toEqual({
90+
style: { color: 'red', backgroundColor: 'blue' },
91+
});
92+
});
93+
94+
it('keeps previous when later is undefined', () => {
95+
expect(
96+
mergeProps({ style: { color: 'red' } }, { style: undefined }),
97+
).toEqual({ style: { color: 'red' } });
98+
});
99+
100+
it('null source does not wipe previous style', () => {
101+
expect(
102+
mergeProps({ style: { color: 'red' } }, { style: null as any }),
103+
).toEqual({ style: { color: 'red' } });
104+
});
105+
106+
it('applies when earlier style is undefined or null', () => {
107+
expect(
108+
mergeProps({ style: undefined }, { style: { color: 'red' } }),
109+
).toEqual({ style: { color: 'red' } });
110+
expect(
111+
mergeProps({ style: null as any }, { style: { color: 'red' } }),
112+
).toEqual({ style: { color: 'red' } });
113+
});
114+
115+
it('nested properties may be null or undefined', () => {
116+
expect(
117+
mergeProps(
118+
{ style: { color: 'red', margin: undefined as any } },
119+
{ style: { padding: null as any, color: 'blue' } },
120+
),
121+
).toEqual({
122+
style: {
123+
color: 'blue',
124+
margin: undefined,
125+
padding: null,
126+
},
127+
});
128+
});
129+
});
66130
});

0 commit comments

Comments
 (0)