forked from kolodny/immutability-helper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
374 lines (312 loc) · 10.5 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
var update = require('./');
var expect = require('expect');
describe('update', function() {
describe('$push', function() {
it('pushes an array', function() {
var originalData = [1];
update(originalData, {$push: [7]});
expect(originalData).toEqual([1, 7]);
});
it('mutates the original object', function() {
var obj = [1];
update(obj, {$push: [7]});
expect(obj).toEqual([1, 7]);
});
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', function() {
var original = ['x'];
expect(update(original, {$push: []})).toBe(original)
});
});
describe('$unshift', function() {
it('unshifts', function() {
var originalData = [1];
update(originalData, {$unshift: [7]});
expect(originalData).toEqual([7, 1]);
});
it('does mutate the original object', function() {
var obj = [1];
update(obj, {$unshift: [7]});
expect(obj).toEqual([7, 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 mutate the original object', function() {
var obj = [1, 4, 3];
update(obj, {$splice: [[1, 1, 2]]});
expect(obj).toEqual([1, 2, 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() {
var obj = {a: 'b'};
update(obj, {$merge: {c: 'd'}});
expect(obj).toEqual({a: 'b', c: 'd'});
});
it('does mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {$merge: {c: 'd'}});
expect(obj).toEqual({a: 'b', c: 'd'});
});
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);
});
});
describe('$set', function() {
it('throws if path not found', function() {
var obj = {a: 3};
expect(update.bind(null, obj, {a: {b: {$set: {c: 'd'}}}})).toThrow(
'$set path not found'
);
});
it('sets', function() {
var obj = {a: {b: 3}};
update(obj, {a: {b: {$set: {c: 'd'}}}})
expect(obj).toEqual({a: {b: {c: 'd'}}});
});
it('should be nested under an accessor', function() {
expect(update.bind(null, {a: {b: 2}}, {$set: 2})).toThrow(
'$set should be nested in an accessor'
);
});
it('does mutate the original object', function() {
var obj = {a: 'b'};
update(obj, {a: {$set: {c: 'd'}}});
expect(obj).toEqual({a: {c: 'd'}});
});
it('keeps reference equality when possible', function() {
var original = {a: 1};
expect(update(original, {a: {$set: 1}})).toBe(original);
});
});
describe('$unset', function() {
it('unsets', function() {
var obj = {a: 'b'};
update(obj, {$unset: ['a']})
expect(obj.a).toBe(undefined);
});
it('removes multiple keys from the object', function() {
var obj = {a: 'b', c: 'd', e: 'f'};
update(obj, {$unset: ['a', 'e']});
expect('a' in obj).toBe(false);
expect('e' in obj).toBe(false);
expect('c' in obj).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);
});
});
describe('$apply', function() {
var applier = function(value) {
return value * 2;
};
function identity(val) {
return val;
}
it('applies', function() {
var obj = {v: 2};
update(obj, {v: {$apply: applier}});
expect(obj).toEqual({v: 4});
});
it('throws when not in path', function() {
expect(update.bind(null, {v: 2}, {$apply: identity})).toThrow(
'$apply should be nested in an accessor'
);
});
it('does mutate the original object', function() {
var obj = {v: 2};
update(obj, {v: {$apply: applier}});
expect(obj).toEqual({v: 4});
});
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: {}}};
expect(update(original, {a: {$apply: identity}})).toBe(original);
});
});
describe('deep update', function() {
it('works', function() {
var obj = {
a: 'b',
c: {
d: 'e',
f: [1],
g: [2],
h: [3],
i: {j: 'k'},
l: 4,
},
};
update(obj, {
c: {
f: {$push: [5]},
g: {$unshift: [6]},
h: {$splice: [[0, 1, 7]]},
i: {$merge: {n: 'o'}},
l: {$apply: function(x) { return x * 2 }},
},
})
expect(obj).toEqual({
a: 'b',
c: {
d: 'e',
f: [1, 5],
g: [6, 2],
h: [7],
i: {j: 'k', n: 'o'},
l: 8,
},
});
});
});
it('should accept array spec to modify arrays', function() {
var original = {value: [{a: 0}]};
update(original, {value: [{a: {$set: 5}}]});
expect(original).toEqual({value: [{a: 5}]});
});
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 the key(s) 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, $unset, ' +
'$merge, $apply.'
);
});
});
});
describe('update', function() {
// TODO: make it work with symbols
return;
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);
});
});
}
});