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