|
| 1 | +// deepMerge.test.ts |
1 | 2 | import deepMerge from '@utils/deepMerge' |
2 | 3 |
|
3 | 4 | describe('deepMerge', () => { |
4 | | - test('merge objects', () => { |
5 | | - expect(deepMerge( |
6 | | - { |
7 | | - id: 'not visible', |
8 | | - type: 'unique', |
9 | | - nested: { |
10 | | - stay: 'still here', |
11 | | - override: 'not here', |
12 | | - merged: [2] |
13 | | - } |
14 | | - }, |
15 | | - { |
16 | | - id: 'visible', |
17 | | - nested: { |
18 | | - override: 'new value', |
19 | | - merged: ['test'] |
20 | | - }, |
21 | | - description: 'from second' |
22 | | - } |
23 | | - )).toStrictEqual( |
24 | | - { |
25 | | - id: 'visible', |
26 | | - type: 'unique', |
27 | | - nested: { |
28 | | - stay: 'still here', |
29 | | - override: 'new value', |
30 | | - merged: [2, 'test'] |
31 | | - }, |
32 | | - description: 'from second' |
33 | | - } |
34 | | - ) |
35 | | - }) |
36 | | - |
37 | | - test('argument 1 is string, return source', () => { |
38 | | - expect(deepMerge('test', { value: 1 })).toStrictEqual({ value: 1 }) |
39 | | - }) |
40 | | - |
41 | | - test('argument 2 is string, return source', () => { |
42 | | - expect(deepMerge({ value: 1 }, 'test')).toStrictEqual('test') |
| 5 | + it('should merge two objects', () => { |
| 6 | + const target = { a: 1, b: 2 } |
| 7 | + const source = { b: 3, c: 4 } |
| 8 | + const result = deepMerge(target, source) |
| 9 | + expect(result).toEqual({ a: 1, b: 3, c: 4 }) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should merge nested objects', () => { |
| 13 | + const target = { a: { b: 1 } } |
| 14 | + const source = { a: { c: 2 } } |
| 15 | + const result = deepMerge(target, source) |
| 16 | + expect(result).toEqual({ a: { b: 1, c: 2 } }) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should merge arrays without duplicates', () => { |
| 20 | + const target = { a: [1, 2] } |
| 21 | + const source = { a: [2, 3] } |
| 22 | + const result = deepMerge(target, source) |
| 23 | + expect(result).toEqual({ a: [1, 2, 3] }) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should overwrite non-object values', () => { |
| 27 | + const target = { a: 1 } |
| 28 | + const source = { a: 2 } |
| 29 | + const result = deepMerge(target, source) |
| 30 | + expect(result).toEqual({ a: 2 }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should return source if target is not an object', () => { |
| 34 | + const target = null |
| 35 | + const source = { a: 1 } |
| 36 | + const result = deepMerge(target, source) |
| 37 | + expect(result).toEqual({ a: 1 }) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should return source if source is not an object', () => { |
| 41 | + const target = { a: 1 } |
| 42 | + const source = null |
| 43 | + const result = deepMerge(target, source) |
| 44 | + expect(result).toEqual(null) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should handle empty objects', () => { |
| 48 | + const target = {} |
| 49 | + const source = {} |
| 50 | + const result = deepMerge(target, source) |
| 51 | + expect(result).toEqual({}) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should handle empty arrays', () => { |
| 55 | + const target = { a: [] } |
| 56 | + const source = { a: [] } |
| 57 | + const result = deepMerge(target, source) |
| 58 | + expect(result).toEqual({ a: [] }) |
43 | 59 | }) |
44 | 60 | }) |
0 commit comments