Sanakey
2024-08-29 45b43f4ff4bea965638166ff619db1ef5afcad70
提交 | 用户 | age
c516d3 1 // 暂时未安装依赖,无法测试
KL 2 // @ts-ignore
3 import { describe, expect, test } from 'vitest';
4 import { deepMerge } from '@/utils';
5
6 describe('deepMerge function', () => {
7535db 7   test('should correctly merge basic data types', () => {
KL 8     const source = { a: 1, b: 2, c: null };
9     const target = {
10       a: 2,
11       b: undefined,
12       c: 3,
13     };
14     const expected = {
15       a: 2,
16       b: 2,
17       c: 3,
18     };
19     expect(deepMerge(source, target)).toStrictEqual(expected);
20   });
21
22   test('should return the same date if only 1 is passed', () => {
23     const foo = new Date();
24     const merged = deepMerge(foo, null);
25     const merged2 = deepMerge(undefined, foo);
26     expect(merged).toStrictEqual(foo);
27     expect(merged2).toStrictEqual(foo);
28     expect(merged).toStrictEqual(merged2);
29   });
30
c516d3 31   test('should merge two objects recursively', () => {
KL 32     const source = {
33       a: { b: { c: 1 }, d: [1, 2] },
34       e: [1, 2],
35       foo: { bar: 3 },
36       array: [
37         {
38           does: 'work',
39           too: [1, 2, 3],
40         },
41       ],
7535db 42       r: { a: 1 },
c516d3 43     };
KL 44     const target = {
45       a: { b: { d: [3] } },
46       e: [3],
47       foo: { baz: 4 },
48       qu: 5,
49       array: [
50         {
51           does: 'work',
52           too: [4, 5, 6],
53         },
54         {
55           really: 'yes',
56         },
57       ],
7535db 58       r: { a: 2 },
c516d3 59     };
KL 60     const expected = {
61       a: { b: { c: 1, d: [3] }, d: [1, 2] },
62       e: [3],
63       foo: {
64         bar: 3,
65         baz: 4,
66       },
67       array: [
68         {
69           does: 'work',
70           too: [4, 5, 6],
71         },
72         {
73           really: 'yes',
74         },
75       ],
76       qu: 5,
7535db 77       r: { a: 2 },
c516d3 78     };
7535db 79     expect(deepMerge(source, target)).toStrictEqual(expected);
c516d3 80   });
KL 81
82   test('should replace arrays by default', () => {
83     const source = {
84       a: { b: { d: [1, 2] } },
85       e: [1, 2],
86     };
87     const target = {
88       a: { b: { d: [3] } },
89       e: [3],
90     };
91     const expected = {
92       a: { b: { d: [3] } },
93       e: [3],
94     };
7535db 95     expect(deepMerge(source, target)).toStrictEqual(expected);
c516d3 96   });
KL 97
98   test("should union arrays using mergeArrays = 'union'", () => {
99     const source = {
100       a: { b: { d: [1, 2] } },
101       e: [1, 2],
102     };
103     const target = {
104       a: { b: { d: [2, 3] } },
7535db 105       e: [1, 3],
c516d3 106     };
KL 107     const expected = {
108       a: { b: { d: [1, 2, 3] } },
109       e: [1, 2, 3],
110     };
7535db 111     expect(deepMerge(source, target, 'union')).toStrictEqual(expected);
c516d3 112   });
KL 113
114   test("should intersect arrays using mergeArrays = 'intersection'", () => {
115     const source = {
116       a: { b: { d: [1, 2] } },
117       e: [1, 2],
118     };
119     const target = {
120       a: { b: { d: [2, 3] } },
121       e: [3],
122     };
123     const expected = {
124       a: { b: { d: [2] } },
125       e: [],
126     };
7535db 127     expect(deepMerge(source, target, 'intersection')).toStrictEqual(expected);
c516d3 128   });
KL 129
130   test("should concatenate arrays using mergeArrays = 'concat'", () => {
131     const source = {
132       a: { b: { d: [1, 2] } },
133       e: [1, 2],
134     };
135     const target = {
136       a: { b: { d: [2, 3] } },
137       e: [3],
138     };
139     const expected = {
140       a: { b: { d: [1, 2, 2, 3] } },
141       e: [1, 2, 3],
142     };
7535db 143     expect(deepMerge(source, target, 'concat')).toStrictEqual(expected);
c516d3 144   });
KL 145 });