-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverwrite_object_field_value.test.js
127 lines (122 loc) · 3.14 KB
/
overwrite_object_field_value.test.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const overwriteObjectFieldValue = require("../src/overwrite_object_field_value");
describe(`::overwriteObjectFieldValue`, () => {
const dataProvider = [
/** simple assigments */
{
path: 'f',
v: {},
obj: {},
expected: { f: {} },
},
{
path: 'f',
v: 'v',
obj: {},
expected: { f: 'v' },
},
{
path: 'f',
v: 1,
obj: {},
expected: { f: 1 },
},
{
path: 'f',
v: true,
obj: {},
expected: { f: true },
},
{
path: 'f',
v: {},
obj: { f: ['test'] },
expected: { f: {} },
},
/** 2-level assignments */
{
path: 'f0.f1',
v: 'v',
obj: {},
expected: { f0: { f1: 'v' } },
},
/** 3-level assignments */
{
path: 'f0.f1.f2',
v: 'v',
obj: {},
expected: { f0: { f1: { f2: 'v' } } },
},
/** replacements */
{
path: 'f',
v: 1,
obj: { f: true },
expected: { f: 1 },
},
{
path: 'f',
v: 'v',
obj: { f: true },
expected: { f: 'v' },
},
{
path: 'f',
v: [],
obj: { f: true },
expected: { f: [] },
},
{
path: 'f',
v: {},
obj: { f: true },
expected: { f: {} },
},
/** 2-level replacements */
{
path: 'f0.f1',
v: 1,
obj: { f0: { f1: {} } },
expected: { f0: { f1: 1 } },
},
/** 3-level replacements */
{
path: 'f0.f1.f2',
v: 1,
obj: { f0: { f1: {} } },
expected: { f0: { f1: { f2: 1 } } },
},
{
path: 'f0.f1.f20',
v: 1,
obj: { f0: { f1: { f2: {} } } },
expected: { f0: { f1: { f2: {}, f20: 1 } } },
},
/** should be not overwritten */
{
path: 'f0.f1.f2',
v: 1,
obj: { f0: [] },
expected: { f0: [] },
},
];
const execute = ({ path, v, obj, delimiter, expected }) => {
it(`injecting "${typeof v === 'object' ? JSON.stringify(v) : v}" by "${path}" path, into "${JSON.stringify(obj)}" expected: "${JSON.stringify(expected)}"`, () => {
const object = overwriteObjectFieldValue(path, v, obj, delimiter);
expect(JSON.stringify(object)).toEqual(JSON.stringify(expected));
});
};
describe('with undefined delimeter', () => {
dataProvider
.forEach(execute);
});
describe('with defined [as "/"] delimeter', () => {
const delimiter = '/';
dataProvider
.map((v) => ({
...v,
delimiter,
path: v.path.replace(/\./g, delimiter),
}))
.forEach(execute);
});
});