-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest-object-hard-copy.test.js
287 lines (257 loc) · 6.62 KB
/
nest-object-hard-copy.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const deepCopy = require('./nest-object-hard-copy.min');
// const deepCopy = require('./nest-object-hard-copy');
// const nestedHardCopy = require('./nest-object-hard-copy');
// const deepCopy = nestedHardCopy;
describe('Plain Object', () => {
it('should create a copied object which is a hard copied version', () => {
const originalObj = {
a: undefined,
b: true,
c: false,
d: 'test',
e: 123,
f: null,
g: function() {
return 'I am a function from original obj';
},
h: [1, 2, 3]
};
const copiedObj = deepCopy(originalObj);
copiedObj.a = 1;
copiedObj.b = false;
copiedObj.c = true;
copiedObj.d = 'test changed';
copiedObj.e = 1234;
copiedObj.f = 'not null';
copiedObj.g = function() {
return 'I am a function from copied obj';
};
copiedObj.h = [1, 2, 3, 4];
expect(copiedObj).toEqual({
a: 1,
b: false,
c: true,
d: 'test changed',
e: 1234,
f: 'not null',
g: expect.any(Function),
h: [1, 2, 3, 4]
});
expect(copiedObj.g()).toBe('I am a function from copied obj');
expect(originalObj).toEqual({
a: undefined,
b: true,
c: false,
d: 'test',
e: 123,
f: null,
g: expect.any(Function),
h: [1, 2, 3]
});
expect(originalObj.g()).toBe('I am a function from original obj');
});
});
describe('Nested Object', () => {
it('should create a copied object which is a hard copied version', () => {
const user = {
id: 101,
email: '[email protected]',
personalInfo: {
name: 'Jack',
isEmployed: false,
nationality: undefined,
address: {
line1: 'westwish st',
line2: 'washmasher',
city: 'wallas',
state: 'WX'
}
},
father: {
id: 102,
email: '[email protected]',
personalInfo: {
name: 'Daniel',
isEmployed: true,
nationality: 'Australia',
address: {
line1: 'westwish loop',
line2: '',
city: 'Melbourne',
state: 'VIC'
}
},
speak: function() {
return 'I am Jack\'s father and my name is Daniel.';
}
},
speak: function() {
return 'My name is Jack.';
}
};
const copiedUser = deepCopy(user);
copiedUser.personalInfo.name = 'Trump';
copiedUser.personalInfo.address.line1 = 'White House';
copiedUser.speak = function() {
return 'My name is Trump but I am a copied version based on Jack';
};
copiedUser.father.personalInfo.name = 'Tom';
copiedUser.father.speak = function() {
return 'My name is Tom but I am a copied version based on Daniel';
};
// Test properties
expect(user.personalInfo.name).toBe('Jack');
expect(user.personalInfo.address.line1).toBe('westwish st');
// Test Functions
expect(copiedUser.speak).toEqual(expect.any(Function));
expect(copiedUser.speak()).toBe(
'My name is Trump but I am a copied version based on Jack'
);
expect(copiedUser.father.speak()).toBe(
'My name is Tom but I am a copied version based on Daniel'
);
expect(user.speak()).toBe('My name is Jack.');
});
it('should create a copied object which also keeps original object\'s prototype chain', () => {
function Foo(who) {
this.me = who;
}
Foo.prototype.identify = function() {
return 'I am ' + this.me;
}
const user = new Foo('Jack');
expect(user.__proto__).toEqual(Foo.prototype);
const copiedUser = deepCopy(user);
expect(copiedUser.__proto__).toEqual(Foo.prototype);
expect(copiedUser.identify()).toEqual('I am Jack');
});
});
describe('Array', () => {
it('should create a copied array which is a hard copied version', () => {
const originalArray = [
{
id: 101,
email: '[email protected]',
personalInfo: {
name: 'Jack',
isEmployed: false,
nationality: undefined,
address: {
line1: 'westwish st',
line2: 'washmasher',
city: 'wallas',
state: 'WX'
}
},
speak: function() {
return 'My name is Jack.';
}
},
{
id: 102,
email: '[email protected]',
personalInfo: {
name: 'Daniel',
isEmployed: true,
nationality: 'Australia',
address: {
line1: 'westwish loop',
line2: '',
city: 'Melbourne',
state: 'VIC'
}
},
speak: function() {
return "I am Jack's father and my name is Daniel.";
}
}
];
const copiedArray = deepCopy(originalArray);
// Change first element values in copied Array
copiedArray[0].personalInfo.name = 'Trump';
copiedArray[0].personalInfo.address.line1 = 'White House';
copiedArray[0].speak = function() {
return 'My name is Trump but I am a copied version based on Jack';
};
expect(originalArray[0].personalInfo.name).toBe('Jack');
expect(originalArray[0].personalInfo.address.line1).toBe('westwish st');
expect(originalArray[0].speak()).toBe('My name is Jack.');
});
});
describe('Array & Object Mix', () => {
it('should create a copied object from an object contains both array and object', () => {
const originalObject = [
{
"controlType": 1,
"customFieldId": "01d734b4-41dc-4f3c-6617-ecb4a03d28b8",
"instructions": null,
"isActive": true,
"label": [
{
"supportedLanguageCode": "en-US",
"value": "new label"
},
{
"supportedLanguageCode": "es-MX",
"value": "new labelaz"
},
{
"supportedLanguageCode": "ja-JP",
"value": "new labeljp"
}
],
"mandatory": true,
"selections": {
},
"sortOrder": 0,
"customText": null
},
{
"controlType": 0,
"customFieldId": "648f6668-f316-5d4f-005f-60666087299c",
"instructions": null,
"isActive": true,
"label": [
{
"supportedLanguageCode": "en-US",
"value": "aa"
},
{
"supportedLanguageCode": "es-MX",
"value": "bbbb"
},
{
"supportedLanguageCode": "ja-JP",
"value": "cc"
}
],
"mandatory": true,
"selections": {
},
"sortOrder": 0,
"customText": null
}
];
const copiedObj = deepCopy(originalObject);
copiedObj[0].customText = 'Changed value';
expect(originalObject[0].customText).toBe(null);
});
});
describe('Circular References', () => {
it('should create a copied object containing circular references as [Circular]', () => {
const originalObject = {
a: 'test',
f: 1
};
originalObject.a = originalObject;
originalObject.b = {};
originalObject.b.c = originalObject;
originalObject.g = originalObject.b;
const copiedObj = deepCopy(originalObject);
// const copiedObj = {...originalObject};
// const copiedObj = JSON.parse(JSON.stringify(originalObject));
expect(copiedObj.a).toBe('[Circular]');
expect(copiedObj.b.c).toBe('[Circular]');
expect(copiedObj.g).toBe('[Circular]');
});
});