forked from kolodny/immutability-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
498 lines (457 loc) · 16.6 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
var update = require('./');
var expect = require('expect');
describe('update', function() {
describe('$push', function() {
it('pushes', function() {
expect(update([1], {$push: [7]})).toEqual([1, 7]);
});
it('does not mutate the original object', function() {
var obj = [1];
update(obj, {$push: [7]});
expect(obj).toEqual([1]);
});
it('only pushes an array', function() {
expect(update.bind(null, [], {$push: 7})).toThrow(
'update(): expected spec of $push to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only pushes unto an array', function() {
expect(update.bind(null, 1, {$push: 7})).toThrow(
'update(): expected target of $push to be an array; got 1.'
);
});
it('keeps reference equality when possible', function() {
var original = ['x'];
expect(update(original, {$push: []})).toBe(original)
});
});
describe('$unshift', function() {
it('unshifts', function() {
expect(update([1], {$unshift: [7]})).toEqual([7, 1]);
});
it('does not mutate the original object', function() {
var obj = [1];
update(obj, {$unshift: [7]});
expect(obj).toEqual([1]);
});
it('only unshifts an array', function() {
expect(update.bind(null, [], {$unshift: 7})).toThrow(
'update(): expected spec of $unshift to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only unshifts unto an array', function() {
expect(update.bind(null, 1, {$unshift: 7})).toThrow(
'update(): expected target of $unshift to be an array; got 1.'
);
});
it('keeps reference equality when possible', function() {
var original = ['x'];
expect(update(original, {$unshift: []})).toBe(original)
});
});
describe('$splice', function() {
it('splices', function() {
expect(update([1, 4, 3], {$splice: [[1, 1, 2]]})).toEqual([1, 2, 3]);
});
it('does not mutate the original object', function() {
var obj = [1, 4, 3];
update(obj, {$splice: [[1, 1, 2]]});
expect(obj).toEqual([1, 4, 3]);
});
it('only splices an array of arrays', function() {
expect(update.bind(null, [], {$splice: 1})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
expect(update.bind(null, [], {$splice: [1]})).toThrow(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
});
it('only splices unto an array', function() {
expect(update.bind(null, 1, {$splice: 7})).toThrow(
'Expected $splice target to be an array; got 1'
);
});
it('keeps reference equality when possible', function() {
var original = ['x'];
expect(update(original, {$splice: [[]]})).toBe(original)
});
});
describe('$merge', function() {
it('merges', function() {
expect(update({a: 'b'}, {$merge: {c: 'd'}})).toEqual({a: 'b', c: 'd'});
});
it('does not mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {$merge: {c: 'd'}});
expect(obj).toEqual({a: 'b'});
});
it('only merges with an object', function() {
expect(update.bind(null, {}, {$merge: 7})).toThrow(
'update(): $merge expects a spec of type \'object\'; got 7'
);
});
it('only merges with an object', function() {
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
'update(): $merge expects a target of type \'object\'; got 7'
);
});
it('keeps reference equality when possible', function() {
var original = {a: {b: {c: true}}};
expect(update(original, {a: {$merge: {}}})).toBe(original);
expect(update(original, {a: {$merge: { b: original.a.b }}})).toBe(original);
// Merging primatives of the same value should return the original.
expect(update(original, {a: {b: { $merge: {c: true} }}})).toBe(original);
// Two objects are different values even though they are deeply equal.
expect(update(original, {a: {$merge: { b: {c: true} }}})).toNotBe(original);
expect(update(original, {
a: {$merge: { b: original.a.b, c: false }}
})).toNotBe(original);
});
});
describe('$set', function() {
it('sets', function() {
expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
});
it('does not mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {$set: {c: 'd'}});
expect(obj).toEqual({a: 'b'});
});
it('keeps reference equality when possible', function() {
var original = {a: 1};
expect(update(original, {a: {$set: 1}})).toBe(original);
expect(update(original, {a: {$set: 2}})).toNotBe(original);
});
it('setting a property to undefined should add an enumerable key to final object with value undefined', function() {
var original = {a: 1};
var result = update(original, {b: {$set: undefined}});
expect(result).toNotBe(original);
expect(result).toEqual({a: 1, b: undefined});
expect(Object.keys(result).length).toEqual(2);
});
});
describe('$toggle', function() {
it('only takes an array as spec', function() {
expect(update.bind(null, {a: false}, {$toggle: 'a'})).toThrow(
'update(): expected spec of $toggle to be an array; got a. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('toggles false to true and true to false', function() {
expect(update({a: false, b: true}, {$toggle: ['a', 'b']})).toEqual({a: true, b: false});
});
it('does not mutate the original object', function() {
var obj = {a: false};
update(obj, {$toggle: ['a']});
expect(obj).toEqual({a: false});
});
it('keeps reference equality when possible', function() {
var original = {a: false};
expect(update(original, {$toggle: []})).toBe(original);
expect(update(original, {$toggle: ['a']})).toNotBe(original);
});
});
describe('$unset', function() {
it('unsets', function() {
expect(update({a: 'b'}, {$unset: ['a']}).a).toBe(undefined);
});
it('removes the key from the object', function() {
var removed = update({a: 'b'}, {$unset: ['a']});
expect('a' in removed).toBe(false);
});
it('removes multiple keys from the object', function() {
var original = {a: 'b', c: 'd', e: 'f'};
var removed = update(original, {$unset: ['a', 'e']});
expect('a' in removed).toBe(false);
expect('a' in original).toBe(true);
expect('e' in removed).toBe(false);
expect('e' in original).toBe(true);
});
it('does not remove keys from the inherited properties', function() {
function Parent() { this.foo = 'Parent'; }
function Child() {}
Child.prototype = new Parent()
var child = new Child();
expect(update(child, {$unset: ['foo']}).foo).toEqual('Parent');
});
it('keeps reference equality when possible', function() {
var original = {a: 1};
expect(update(original, {$unset: ['b']})).toBe(original);
expect(update(original, {$unset: ['a']})).toNotBe(original);
});
});
describe('$add', function() {
it('works on Map', function() {
var state = new Map([[1, 2], [3, 4]]);
var state2 = update(state, {$add: [[5, 6]]});
expect(state2.get(1)).toEqual(2);
expect(state2.get(5)).toEqual(6);
});
it('works on Set', function() {
var state = new Set([1, 2, 3, 4]);
var state2 = update(state, {$add: [5, 6]});
expect(state2.has(1)).toBe(true);
expect(state2.has(5)).toBe(true);
});
it('throws on a non Map or Set', function() {
expect(update.bind(null, 2, {$add: [1]})).toThrow(
'update(): $add expects a target of type Set or Map; got Number'
);
})
});
describe('$remove', function() {
it('works on Map', function() {
var state = new Map([[1, 2], [3, 4], [5, 6]]);
var state2 = update(state, {$remove: [1, 5]});
expect(state2.has(1)).toBe(false);
expect(state2.has(3)).toBe(true);
expect(state2.get(3)).toBe(4);
expect(state2.has(6)).toBe(false);
});
it('works on Set', function() {
var state = new Set([1, 2, 3, 4]);
var state2 = update(state, {$remove: [2, 3]});
expect(state2.has(1)).toBe(true);
expect(state2.has(2)).toBe(false);
});
it('throws on a non Map or Set', function() {
expect(update.bind(null, 2, {$remove: [1]})).toThrow(
'update(): $remove expects a target of type Set or Map; got Number'
);
})
});
describe('$apply', function() {
var applier = function(node) {
return {v: node.v * 2};
};
it('applies', function() {
expect(update({v: 2}, {$apply: applier})).toEqual({v: 4});
});
it('does not mutate the original object', function() {
var obj = {v: 2};
update(obj, {$apply: applier});
expect(obj).toEqual({v: 2});
});
it('only applies a function', function() {
expect(update.bind(null, 2, {$apply: 123})).toThrow(
'update(): expected spec of $apply to be a function; got 123.'
);
});
it('keeps reference equality when possible', function() {
var original = {a: {b: {}}};
function identity(val) {
return val;
}
expect(update(original, {a: {$apply: identity}})).toBe(original);
expect(update(original, {a: {$apply: applier}})).toNotBe(original);
});
});
describe('deep update', function() {
it('works', function() {
expect(update({
a: 'b',
c: {
d: 'e',
f: [1],
g: [2],
h: [3],
i: {j: 'k'},
l: 4,
},
}, {
c: {
d: {$set: 'm'},
f: {$push: [5]},
g: {$unshift: [6]},
h: {$splice: [[0, 1, 7]]},
i: {$merge: {n: 'o'}},
l: {$apply: function(x) { return x * 2 }},
},
})).toEqual({
a: 'b',
c: {
d: 'm',
f: [1, 5],
g: [6, 2],
h: [7],
i: {j: 'k', n: 'o'},
l: 8,
},
});
});
it('keeps reference equality when possible', function() {
var original = {a: {b: 1}, c: {d: {e: 1}}};
expect(update(original, {a: {b: {$set: 1}}})).toBe(original);
expect(update(original, {a: {b: {$set: 1}}}).a).toBe(original.a);
expect(update(original, {c: {d: {e: {$set: 1}}}})).toBe(original);
expect(update(original, {c: {d: {e: {$set: 1}}}}).c).toBe(original.c);
expect(update(original, {c: {d: {e: {$set: 1}}}}).c.d).toBe(original.c.d);
expect(update(original, {
a: {b: {$set: 1}},
c: {d: {e: {$set: 1}}},
})).toBe(original);
expect(update(original, {
a: {b: {$set: 1}},
c: {d: {e: {$set: 1}}},
}).a).toBe(original.a);
expect(update(original, {
a: {b: {$set: 1}},
c: {d: {e: {$set: 1}}},
}).c).toBe(original.c);
expect(update(original, {
a: {b: {$set: 1}},
c: {d: {e: {$set: 1}}},
}).c.d).toBe(original.c.d);
expect(update(original, {a: {b: {$set: 2}}})).toNotBe(original);
expect(update(original, {a: {b: {$set: 2}}}).a).toNotBe(original.a);
expect(update(original, {a: {b: {$set: 2}}}).a.b).toNotBe(original.a.b);
expect(update(original, {a: {b: {$set: 2}}}).c).toBe(original.c);
expect(update(original, {a: {b: {$set: 2}}}).c.d).toBe(original.c.d);
});
});
it('should accept array spec to modify arrays', function() {
var original = {value: [{a: 0}]};
var modified = update(original, {value: [{a: {$set: 1}}]});
expect(modified).toEqual({value: [{a: 1}]});
});
it('should accept object spec to modify arrays', function() {
var original = {value: [{a: 0}]};
var modified = update(original, {value: {'0': {a: {$set: 1}}}});
expect(modified).toEqual({value: [{a: 1}]});
});
it('should reject arrays except as values of specific commands', function() {
var specs = [
[],
{a: []},
{a: {$set: []}, b: [[]]},
];
specs.forEach(function(spec) {
expect(update.bind(null, {a: 'b'}, spec)).toThrow(
'update(): You provided an invalid spec to update(). The spec ' +
'may not contain an array except as the value of $set, $push, ' +
'$unshift, $splice or any custom command allowing an array value.'
);
});
});
it('should reject non arrays from $unset', function() {
expect(update.bind(null, {a: 'b'}, {$unset: 'a'})).toThrow(
'update(): expected spec of $unset to be an array; got a. ' +
'Did you forget to wrap your parameter in an array?'
);
});
it('should require a plain object spec containing command(s)', function() {
var specs = [
null,
false,
{a: 'c'},
{a: {b: 'c'}},
];
specs.forEach(function(spec) {
expect(update.bind(null, {a: 'b'}, spec)).toThrow(
'update(): You provided an invalid spec to update(). The spec ' +
'and every included key path must be plain objects containing one ' +
'of the following commands: $push, $unshift, $splice, $set, $toggle, $unset, ' +
'$add, $remove, $merge, $apply.'
);
});
});
it('should perform safe hasOwnProperty check', function() {
expect(update({}, {'hasOwnProperty': {$set: 'a'}})).toEqual({
'hasOwnProperty': 'a',
});
});
});
describe('update', function() {
var myUpdate;
beforeEach(function() {
myUpdate = update.newContext();
});
describe('can extend functionality', function() {
it('allows adding new directives', function() {
myUpdate.extend('$addtax', function(tax, original) {
return original + (tax * original);
});
expect(myUpdate(5, {$addtax: 0.10})).toEqual(5.5);
});
it('gets the original object (so be careful about mutations)', function() {
var obj = {};
var passedOriginal;
myUpdate.extend('$foobar', function(prop, original) {
passedOriginal = original;
});
myUpdate(obj, {$foobar: null});
expect(obj).toBe(passedOriginal);
});
it("doesn't touch the original update", function() {
myUpdate.extend('$addtax', function(tax, original) {
return original + (tax * original);
});
expect( update.bind(null, {$addtax: 0.10}, {$addtax: 0.10})).toThrow();
expect(myUpdate.bind(null, {$addtax: 0.10}, {$addtax: 0.10})).toNotThrow();
});
it('can handle nibling directives', function() {
var obj = {a: [1, 2, 3], b: "me"};
var spec = {
a: {$splice: [[0, 2]]},
$merge: {b: "you"},
};
expect(update(obj, spec)).toEqual({"a":[3],"b":"you"});
});
});
if (typeof Symbol === 'function' && Symbol('TEST').toString() === 'Symbol(TEST)') {
describe('works with symbols', function() {
it('in the source object', function() {
var obj = {a: 1};
obj[Symbol.for('b')] = 2;
expect(update(obj, {c: {$set: 3}})[Symbol.for('b')]).toEqual(2);
});
it('in the spec object', function() {
var obj = {a: 1};
obj[Symbol.for('b')] = 2;
var spec = {};
spec[Symbol.for('b')] = {$set: 2};
expect(update(obj, spec)[Symbol.for('b')]).toEqual(2);
});
it('in the $merge command', function() {
var obj = {a: 1};
obj[Symbol.for('b')] = {c: 3};
obj[Symbol.for('d')] = 4;
var spec = {};
spec[Symbol.for('b')] = { $merge: {} };
spec[Symbol.for('b')].$merge[Symbol.for('e')] = 5;
var updated = update(obj, spec);
expect(updated[Symbol.for('b')][Symbol.for('e')]).toEqual(5);
expect(updated[Symbol.for('d')]).toEqual(4);
});
});
}
it('supports objects without prototypes', function() {
var obj = Object.create(null);
expect(update.bind(null, obj, {$merge: {a: 'b'}})).toNotThrow()
});
it('supports an escape hatch for isEquals', function() {
myUpdate.isEquals = function(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
var a = {b: {c: {d: [4, 5]}}};
var b = myUpdate(a, {b: {c: {d: {$set: [4, 5]}}}});
var c = myUpdate(a, {b: {$set: {c: {d: [4, 5]}}}});
var d = myUpdate(a, {$set: {b: {c: {d: [4, 5]}}}});
expect(a).toBe(b)
expect(a).toBe(c)
expect(a).toBe(d)
});
it('does not lose non integer keys of an array', function() {
var state = a = { items: [
{ name: 'Superman', strength: 1000 },
{ name: 'Jim', strength: 2 },
] };
state.items.top = 0
var state2 = update(state, { items: { 1: { strength: { $set: 3 } } } });
expect(state2.items.top).toBe(0)
});
});