forked from jonschlinkert/assign-deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
388 lines (325 loc) · 10.9 KB
/
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
'use strict';
require('mocha');
const assert = require('assert');
const assign = require('./');
describe('assign', () => {
it('should deeply assign properties of additional objects to the first object', () => {
let one = { b: { c: { d: 'e' } } };
let two = { b: { c: { f: 'g', j: 'i' } } };
assert.deepEqual(assign(one, two), { b: { c: { d: 'e', f: 'g', j: 'i' } } });
});
it('should not assign values in arrays (same as Object.assign)', () => {
let abc = ['a', 'b'];
let xyz = ['c', 'd'];
assert(assign(abc, xyz), xyz);
let obj = { b: [{ a: { a: 0 } }] };
let one = assign({}, obj);
let two = assign({}, obj);
assert(one !== obj);
assert(two !== obj);
assert(one !== two);
assert(one.b === two.b);
one.b[0].a = { b: 0 };
two.b[0].a = { c: 0 };
assert.deepEqual(one.b[0].a, { c: 0 });
});
it('should extend properties onto a function', () => {
function target() {}
let one = { b: { c: { d: 'e' } } };
let two = { b: { c: { f: 'g', j: 'i' } } };
assign(target, one, two);
assert.deepEqual(target.b, { c: { d: 'e', f: 'g', j: 'i' } });
});
it('should extend ignore primitives', () => {
function target() {}
let one = { b: { c: { d: 'e' } } };
let two = { b: { c: { f: 'g', j: 'i' } } };
assign('foo', target, one, two);
assert.deepEqual(target.b, { c: { d: 'e', f: 'g', j: 'i' } });
});
it('should extend deeply nested functions', () => {
let fn = function() {};
let target = {};
let one = { b: { c: { d: fn } } };
let two = { b: { c: { f: 'g', j: 'i' } } };
assign(target, one, two);
assert.deepEqual(target.b, { c: { d: fn, f: 'g', j: 'i' } });
});
it('should extend deeply nested functions with properties', () => {
let fn = function() {};
fn.foo = 'foo';
fn.bar = 'bar';
let target = {};
let one = { b: { c: { d: fn } } };
let two = { b: { c: { f: 'g', j: 'i' } } };
assign(target, one, two);
assert.deepEqual(target.b, { c: { d: fn, f: 'g', j: 'i' } });
});
it('should invoke getters and setters', () => {
let name, username;
let config = {
set name(val) {
name = val;
},
get name() {
return name;
},
set username(val) {
username = val;
},
get username() {
return username || 'jonschlinkert';
},
get other() {
return 'other';
}
};
let locals = {
get name() {
return 'Brian';
},
get username() {
return 'doowb';
}
}
let result = assign(config, locals);
assert(result === config);
assert.equal(result.name, 'Brian');
assert.equal(result.username, 'doowb');
assert.equal(result.other, 'other');
});
it('should extend functions with nested properties', () => {
let aaa = () => {};
aaa.foo = { y: 'y' };
aaa.bar = { z: 'z' };
let bbb = () => {};
bbb.foo = { w: 'w' };
bbb.bar = { x: 'x' };
let result = assign(aaa, bbb);
assert(aaa === result);
assert.deepEqual(result.foo, { y: 'y', w: 'w' });
assert.deepEqual(result.bar, { z: 'z', x: 'x' });
});
it('should extend properties from functions to functions', () => {
function target() {}
function one() {}
function two() {}
one.b = { c: { d: 'e' } };
two.b = { c: { f: 'g', j: 'i' } };
assign(target, one, two);
assert.deepEqual(target.b, { c: { d: 'e', f: 'g', j: 'i' } });
});
it('should update a value when a duplicate is assigned', () => {
let one = { b: { c: { d: 'e' } } };
let two = { b: { c: { d: 'f' } } };
assert.deepEqual(assign(one, two), { b: { c: { d: 'f' } } });
});
it('should not loop over arrays', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = { b: { c: { d: 'f', g: ['a'] } } };
assert.deepEqual(assign(one, two), { b: { c: { d: 'f', g: ['a'] } } });
});
it('should deeply assign values from multiple objects', () => {
let foo = {};
let bar = { a: 'b' };
let baz = { c: 'd', g: { h: 'i' } };
let quux = { e: 'f', g: { j: 'k' } };
assign(foo, bar, baz, quux);
assert.deepEqual(foo, { a: 'b', c: 'd', e: 'f', g: { h: 'i', j: 'k' } });
});
it('should not assign primitive arguments', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = 5;
assert.deepEqual(assign(one, two), one);
});
it('should assign primitive values', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = { b: 5 };
assert.deepEqual(assign(one, two), { b: 5 });
});
it('should assign over primitive values', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = { b: 5 };
let three = { b: function() {} };
three.b.foo = 'bar';
assert.deepEqual(assign(one, two, three), three);
});
it('should assign null values', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = { b: null, c: null };
assert.deepEqual(assign(one, two), { b: null, c: null });
});
it('should assign undefined values', () => {
let one = { b: { c: { d: 'e', g: ['b'] } } };
let two = { b: undefined };
assert.deepEqual(assign(one, two), { b: undefined });
});
it('should assign properties to a function', () => {
function one() {}
one.a = 'b';
one.c = 'd';
let two = { e: 'f', g: ['h'] };
assign(one, two);
assert.deepEqual(one.g, ['h']);
assert.equal(one.g, two.g);
assert.equal(typeof one, 'function');
});
it('should assign properties from a function', () => {
function one() {}
one.a = 'b';
one.c = 'd';
let two = { e: 'f', g: ['h'] };
assign(two, one);
assert.deepEqual(two.g, ['h']);
assert.equal(two.g, two.g);
assert.equal(typeof two, 'object');
});
it('should deeply mix the properties of object into the first object.', () => {
let a = assign({ a: { aa: 'aa' } }, { a: { bb: 'bb' } }, { a: { cc: 'cc' } });
assert.deepEqual(a, { a: { aa: 'aa', bb: 'bb', cc: 'cc' } });
let b = assign(
{ a: { aa: 'aa', dd: { ee: 'ff' } } },
{ a: { bb: 'bb', dd: { gg: 'hh' } } },
{ a: { cc: 'cc', dd: { ii: 'jj' } } }
);
assert.deepEqual(b, { a: { aa: 'aa', dd: { ee: 'ff', gg: 'hh', ii: 'jj' }, bb: 'bb', cc: 'cc' } });
});
it('should merge object properties without affecting any object', () => {
let obj1 = { a: 0, b: 1 };
let obj2 = { c: 2, d: 3 };
let obj3 = { a: 4, d: 5 };
let actual = { a: 4, b: 1, c: 2, d: 5 };
assert.deepEqual(assign({}, obj1, obj2, obj3), actual);
assert.notDeepEqual(actual, obj1);
assert.notDeepEqual(actual, obj2);
assert.notDeepEqual(actual, obj3);
});
it('should do a deep merge', () => {
let obj1 = { a: { b: 1, c: 1, d: { e: 1, f: 1 } } };
let obj2 = { a: { b: 2, d: { f: 'f' } } };
assert.deepEqual(assign(obj1, obj2), { a: { b: 2, c: 1, d: { e: 1, f: 'f' } } });
});
it('should use the last value defined', () => {
let obj1 = { a: 'b' };
let obj2 = { a: 'c' };
assert.deepEqual(assign(obj1, obj2), { a: 'c' });
});
it('should use the last value defined on nested object', () => {
let obj1 = { a: 'b', c: { d: 'e' } };
let obj2 = { a: 'c', c: { d: 'f' } };
assert.deepEqual(assign(obj1, obj2), { a: 'c', c: { d: 'f' } });
});
it('should shallow clone when an empty object is passed', () => {
let obj1 = { a: 'b', c: { d: 'e' } };
let obj2 = { a: 'c', c: { d: 'f' } };
let res = assign({}, obj1, obj2);
assert.deepEqual(res, { a: 'c', c: { d: 'f' } });
});
it('should merge additional objects into the first', () => {
let obj1 = { a: { b: 1, c: 1, d: { e: 1, f: 1 } } };
let obj2 = { a: { b: 2, d: { f: 'f' } } };
assign(obj1, obj2);
assert.deepEqual(obj1, { a: { b: 2, c: 1, d: { e: 1, f: 'f' } } });
});
it('should clone objects during merge', () => {
let obj1 = { a: { b: 1 } };
let obj2 = { a: { c: 2 } };
let target = assign({}, obj1, obj2);
assert.deepEqual(target, { a: { b: 1, c: 2 } });
assert.deepEqual(target.a, { b: 1, c: 2 });
});
it('should deep clone arrays during merge', () => {
let obj1 = { a: [1, 2, [3, 4]] };
let obj2 = { b: [5, 6] };
let actual = assign(obj1, obj2);
assert.deepEqual(actual.a, [1, 2, [3, 4]]);
assert.deepEqual(actual.a[2], [3, 4]);
assert.deepEqual(actual.b, obj2.b);
});
it('should copy source properties', () => {
assert(assign({ test: true }).test, true);
});
it('should not deep clone arrays', () => {
assert.deepEqual(assign([1, 2, 3]), [1, 2, 3]);
assert.deepEqual(assign([1, 2, 3], {}), [1, 2, 3]);
});
it('should iterate over sparse arguments', () => {
let actual = assign({}, undefined, { a: 'b' }, undefined, { c: 'd' });
assert.deepEqual(actual, { a: 'b', c: 'd' });
});
it('should clone RegExps', () => {
let fixture = /test/g;
let actual = assign(fixture);
assert.deepEqual(actual, fixture);
});
it('should clone Dates', () => {
let fixture = new Date();
let actual = assign(fixture);
assert.deepEqual(actual, fixture);
});
it('should not clone objects created with custom constructor', () => {
function TestType() {}
let fixture = new TestType();
let actual = assign(fixture);
assert.deepEqual(actual, fixture);
});
});
describe('symbols', () => {
it('should return the first object when one argument is passed', () => {
assert.deepEqual(assign({ a: 'b' }), { a: 'b' });
});
if (typeof Symbol !== 'undefined') {
it('should assign symbol properties from an object to the receiver', () => {
let a = {};
let b = {};
let key = Symbol('abc');
b[key] = 'xyz';
assign(a, b);
assert.equal(a[key], 'xyz');
});
it('should deeply assign symbol properties', () => {
let a = { c: { e: { f: {} } } };
let b = { c: { e: { g: {} } } };
let foo = Symbol('foo');
let bar = Symbol('bar');
a.c.e.f[foo] = 'xyz';
b.c.e.g[bar] = 'xyz';
assign(a, b);
assert.equal(a.c.e.f[foo], 'xyz');
assert.equal(a.c.e.g[bar], 'xyz');
});
it('should assign symbol properties from each object to the receiver', () => {
let target = {};
let a = {};
let aa = Symbol('aa');
a[aa] = 'aa';
let b = {};
let bb = Symbol('bb');
b[bb] = 'bb';
let c = {};
let cc = Symbol('cc');
c[cc] = 'cc';
assign(target, a, b, c);
assert.equal(target[aa], 'aa');
assert.equal(target[bb], 'bb');
assert.equal(target[cc], 'cc');
});
it('should not assign non-enumerable symbols', () => {
let a = {};
let key = Symbol('abc');
function App() {}
App.prototype[key] = 'xyz';
let app = new App();
assign(a, app);
assert.equal(typeof a[key], 'undefined');
});
it('should return the receiver object', () => {
let a = {};
let b = {};
let key = Symbol('abc');
b[key] = 'xyz';
let res = assign(a, b);
assert.equal(res[key], 'xyz');
});
}
});