-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathsetObjectProperty.test.ts
More file actions
76 lines (74 loc) · 2.22 KB
/
Copy pathsetObjectProperty.test.ts
File metadata and controls
76 lines (74 loc) · 2.22 KB
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
import { setObjectProperty } from "../../../src/core/utils/setObjectProperty";
interface TestCase {
description: string;
giveObject: object;
givePath: string;
giveValue: any;
wantObject: object;
}
describe("Test setObjectProperty", () => {
const testCases: TestCase[] = [
{
description: "empty",
giveObject: {},
givePath: "",
giveValue: 0,
wantObject: { "": 0 },
},
{
description: "top-level primitive",
giveObject: {},
givePath: "age",
giveValue: 42,
wantObject: { age: 42 },
},
{
description: "top-level object",
giveObject: {},
givePath: "name",
giveValue: { first: "John", last: "Doe" },
wantObject: { name: { first: "John", last: "Doe" } },
},
{
description: "top-level array",
giveObject: {},
givePath: "values",
giveValue: [1, 2, 3],
wantObject: { values: [1, 2, 3] },
},
{
description: "nested object property",
giveObject: {
name: {
first: "John",
},
},
givePath: "name.last",
giveValue: "Doe",
wantObject: { name: { first: "John", last: "Doe" } },
},
{
description: "deeply nested object property",
giveObject: {
info: {
address: {
street: "123 Main St.",
},
age: 42,
name: {
last: "Doe",
},
},
},
givePath: "info.name.first",
giveValue: "John",
wantObject: {
info: { age: 42, address: { street: "123 Main St." }, name: { first: "John", last: "Doe" } },
},
},
];
test.each(testCases)("$description", ({ giveObject, givePath, giveValue, wantObject }) => {
const result = setObjectProperty(giveObject, givePath, giveValue);
expect(result).toEqual(wantObject);
});
});