forked from rjsf-team/react-jsonschema-form
/
mergeObjects.test.ts
109 lines (93 loc) · 2.76 KB
/
mergeObjects.test.ts
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
import { mergeObjects } from "../src";
describe("mergeObjects()", () => {
it("shouldn`t mutate the provided objects", () => {
const obj1 = { a: 1 };
mergeObjects(obj1, { b: 2 });
expect(obj1).toEqual({ a: 1 });
});
it("should merge two one-level deep objects", () => {
expect(mergeObjects({ a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 });
});
it("should override the first object with the values from the second", () => {
expect(mergeObjects({ a: 1 }, { a: 2 })).toEqual({ a: 2 });
});
it("should override non-existing values of the first object with the values from the second", () => {
expect(
mergeObjects({ a: { b: undefined } }, { a: { b: { c: 1 } } })
).toEqual({ a: { b: { c: 1 } } });
});
it("should recursively merge deeply nested objects", () => {
const obj1 = {
a: 1,
b: {
c: 3,
d: [1, 2, 3],
e: { f: { g: 1 } },
},
c: 2,
};
const obj2 = {
a: 1,
b: {
d: [3, 2, 1],
e: { f: { h: 2 } },
g: 1,
},
c: 3,
};
const expected = {
a: 1,
b: {
c: 3,
d: [3, 2, 1],
e: { f: { g: 1, h: 2 } },
g: 1,
},
c: 3,
};
expect(mergeObjects(obj1, obj2)).toEqual(expected);
});
it("should recursively merge File objects", () => {
const file = new File(["test"], "test.txt");
const obj1 = {
a: {},
};
const obj2 = {
a: file,
};
expect(mergeObjects(obj1, obj2).a).toBeInstanceOf(File);
});
describe("concatArrays option", () => {
it("should not concat arrays by default", () => {
const obj1 = { a: [1] };
const obj2 = { a: [2] };
expect(mergeObjects(obj1, obj2)).toEqual({ a: [2] });
});
it("should concat arrays when concatArrays is true", () => {
const obj1 = { a: [1] };
const obj2 = { a: [2] };
expect(mergeObjects(obj1, obj2, true)).toEqual({ a: [1, 2] });
});
it("should concat nested arrays when concatArrays is true", () => {
const obj1 = { a: { b: [1] } };
const obj2 = { a: { b: [2] } };
expect(mergeObjects(obj1, obj2, true)).toEqual({
a: { b: [1, 2] },
});
});
it("should not concat duplicate values in arrays when concatArrays is 'preventDuplicates'", () => {
const obj1 = { a: [1] };
const obj2 = { a: [1, 2] };
expect(mergeObjects(obj1, obj2, "preventDuplicates")).toEqual({
a: [1, 2],
});
});
it("should not concat duplicate values in nested arrays when concatArrays is 'preventDuplicates'", () => {
const obj1 = { a: { b: [1] } };
const obj2 = { a: { b: [1, 2] } };
expect(mergeObjects(obj1, obj2, "preventDuplicates")).toEqual({
a: { b: [1, 2] },
});
});
});
});