File size: 8,789 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { type EachTestingContext } from '@/testing/types/EachTestingContext.type';
import { deepMerge } from '@/utils';

type DeepMergeTestCase<T extends object> = {
  source: Required<T>;
  target: Required<T>;
  expected: T;
};

describe('deepMerge', () => {
  describe('primitive values', () => {
    type PrimitiveValue = { value: string | number | boolean };

    const primitiveTestCases: EachTestingContext<
      DeepMergeTestCase<PrimitiveValue>
    >[] = [
      {
        title: 'should override string values',
        context: {
          source: { value: 'hello' },
          target: { value: 'world' },
          expected: { value: 'world' },
        },
      },
      {
        title: 'should override number values',
        context: {
          source: { value: 42 },
          target: { value: 24 },
          expected: { value: 24 },
        },
      },
      {
        title: 'should override boolean values',
        context: {
          source: { value: true },
          target: { value: false },
          expected: { value: false },
        },
      },
    ];

    it.each(primitiveTestCases)(
      '$title',
      ({ context: { source, target, expected } }) => {
        expect(deepMerge(source, target)).toEqual(expected);
      },
    );
  });

  describe('null and undefined handling', () => {
    type NullableValue = { value: string | null };

    const nullTestCases: EachTestingContext<
      DeepMergeTestCase<NullableValue>
    >[] = [
      {
        title: 'should preserve null values from target',
        context: {
          source: { value: 'hello' },
          target: { value: null },
          expected: { value: null },
        },
      },
      {
        title: 'should ignore undefined values from target',
        context: {
          source: { value: 'hello' },
          target: { value: undefined as unknown as null },
          expected: { value: 'hello' },
        },
      },
      {
        title: 'should override null values from source',
        context: {
          source: { value: null },
          target: { value: 'world' },
          expected: { value: 'world' },
        },
      },
    ];

    it.each(nullTestCases)(
      '$title',
      ({ context: { source, target, expected } }) => {
        expect(deepMerge(source, target)).toEqual(expected);
      },
    );

    type MixedNullValue = { a: number | null; b: number };

    const mixedNullTestCase: EachTestingContext<
      DeepMergeTestCase<MixedNullValue>
    > = {
      title: 'should handle mixed null and undefined values',
      context: {
        source: { a: 1, b: 2 },
        target: { a: null, b: 2 },
        expected: { a: null, b: 2 },
      },
    };

    it(mixedNullTestCase.title, () => {
      const { source, target, expected } = mixedNullTestCase.context;
      expect(deepMerge(source, target)).toEqual(expected);
    });
  });

  describe('array handling', () => {
    type ArrayValue = { arr: Array<string | number> | null };

    const arrayTestCases: EachTestingContext<DeepMergeTestCase<ArrayValue>>[] =
      [
        {
          title: 'should concatenate arrays',
          context: {
            source: { arr: [1, 2] },
            target: { arr: [3, 4] },
            expected: { arr: [1, 2, 3, 4] },
          },
        },
        {
          title: 'should handle empty target array',
          context: {
            source: { arr: [1, 2] },
            target: { arr: [] },
            expected: { arr: [1, 2] },
          },
        },
        {
          title: 'should handle empty source array',
          context: {
            source: { arr: [] },
            target: { arr: [1, 2] },
            expected: { arr: [1, 2] },
          },
        },
        {
          title: 'should handle null target array',
          context: {
            source: { arr: ['a', 'b'] },
            target: { arr: null },
            expected: { arr: null },
          },
        },
      ];

    it.each(arrayTestCases)(
      '$title',
      ({ context: { source, target, expected } }) => {
        expect(deepMerge(source, target)).toEqual(expected);
      },
    );
  });

  describe('nested objects', () => {
    type NestedValue = {
      nested: {
        a: number;
        b: number;
        deep?: {
          a: number;
          b: number;
        };
        arr: Array<number>;
        obj: {
          a: number;
          b: number;
        };
      } | null;
    };

    const nestedTestCases: EachTestingContext<
      DeepMergeTestCase<NestedValue>
    >[] = [
      {
        title: 'should merge nested objects',
        context: {
          source: { nested: { a: 1, b: 2, arr: [], obj: { a: 1, b: 2 } } },
          target: { nested: { a: 1, b: 3, arr: [], obj: { a: 1, b: 2 } } },
          expected: { nested: { a: 1, b: 3, arr: [], obj: { a: 1, b: 2 } } },
        },
      },
      {
        title: 'should merge deeply nested objects',
        context: {
          source: {
            nested: {
              a: 1,
              b: 2,
              deep: { a: 1, b: 2 },
              arr: [],
              obj: { a: 1, b: 2 },
            },
          },
          target: {
            nested: {
              a: 1,
              b: 2,
              deep: { a: 1, b: 3 },
              arr: [],
              obj: { a: 1, b: 2 },
            },
          },
          expected: {
            nested: {
              a: 1,
              b: 2,
              deep: { a: 1, b: 3 },
              arr: [],
              obj: { a: 1, b: 2 },
            },
          },
        },
      },
      {
        title: 'should handle null nested object in target',
        context: {
          source: { nested: { a: 1, b: 2, arr: [], obj: { a: 1, b: 2 } } },
          target: { nested: null },
          expected: { nested: null },
        },
      },
      {
        title: 'should handle complex nested structures',
        context: {
          source: {
            nested: {
              a: 1,
              b: 2,
              arr: [1, 2],
              obj: { a: 1, b: 2 },
            },
          },
          target: {
            nested: {
              a: 1,
              b: 2,
              arr: [3, 4],
              obj: { a: 1, b: 3 },
            },
          },
          expected: {
            nested: {
              a: 1,
              b: 2,
              arr: [1, 2, 3, 4],
              obj: { a: 1, b: 3 },
            },
          },
        },
      },
    ];

    it.each(nestedTestCases)(
      '$title',
      ({ context: { source, target, expected } }) => {
        expect(deepMerge(source, target)).toEqual(expected);
      },
    );
  });

  describe('edge cases', () => {
    type EdgeValue = {
      a: number | Date | RegExp;
      b: number | Date | RegExp;
    };

    const edgeTestCases: EachTestingContext<DeepMergeTestCase<EdgeValue>>[] = [
      {
        title: 'should handle Date objects by replacing them',
        context: {
          source: { a: new Date('2023-01-01'), b: new Date('2023-01-01') },
          target: { a: new Date('2023-12-31'), b: new Date('2023-12-31') },
          expected: { a: new Date('2023-12-31'), b: new Date('2023-12-31') },
        },
      },
      {
        title: 'should handle RegExp objects by replacing them',
        context: {
          source: { a: /test1/, b: /test1/ },
          target: { a: /test2/, b: /test2/ },
          expected: { a: /test2/, b: /test2/ },
        },
      },
      {
        title: 'should handle mixed Date and RegExp objects',
        context: {
          source: { a: new Date('2023-01-01'), b: /test1/ },
          target: { a: new Date('2023-12-31'), b: /test2/ },
          expected: { a: new Date('2023-12-31'), b: /test2/ },
        },
      },
    ];

    it.each(edgeTestCases)(
      '$title',
      ({ context: { source, target, expected } }) => {
        expect(deepMerge(source, target)).toEqual(expected);
      },
    );

    type NestedDateValue = {
      a: { value: Date };
      b: { value: Date };
    };

    const nestedDateTestCase: EachTestingContext<
      DeepMergeTestCase<NestedDateValue>
    > = {
      title: 'should handle Date objects in nested structures',
      context: {
        source: {
          a: { value: new Date('2023-01-01') },
          b: { value: new Date('2023-01-01') },
        },
        target: {
          a: { value: new Date('2023-12-31') },
          b: { value: new Date('2023-12-31') },
        },
        expected: {
          a: { value: new Date('2023-12-31') },
          b: { value: new Date('2023-12-31') },
        },
      },
    };

    it(nestedDateTestCase.title, () => {
      const { source, target, expected } = nestedDateTestCase.context;
      expect(deepMerge(source, target)).toEqual(expected);
    });
  });
});