-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.js
431 lines (371 loc) · 11.7 KB
/
tests.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
var immutable = require("./index");
var lens = immutable.lens;
var assert = require("chai").assert;
describe("factory", function() {
var obj;
beforeEach(function() {
obj = immutable({ a: 1, b: 2, c: 3, d: 4 });
});
it("allows read access to properties", function() {
assert.equal(obj.a, 1);
assert.equal(obj.b, 2);
});
it("freezes object", function() {
assert(Object.isFrozen(obj));
assert(Object.isFrozen(immutable()));
assert(Object.isFrozen(immutable([])));
});
it("throws if trying to write property, in strict mode", function() {
"use strict";
assert.throw(function() { obj.a = 2; }, TypeError);
});
it("can be called with new operator", function() {
var obj = new immutable({ a: 1 });
assert.equal(obj.a, 1);
});
it("returns empty object when no props", function() {
assert.equal(Object.keys(immutable()).length, 0);
});
it("returns same empty object when no props", function() {
assert.strictEqual(immutable(), immutable());
});
it("can be called with various primitive types`", function() {
var primitives = [false, "hello", 5, null, undefined];
primitives.forEach(function(val) {
assert.strictEqual(immutable(val), val);
var obj = { val: val };
assert.strictEqual(immutable({ val: val }).val, val);
});
assert.deepEqual(immutable(primitives), primitives);
});
});
describe("immutable array", function() {
var source = [{foo:1},{foo:2},{foo:3}];
var array = immutable(source);
it("is still an Array", function() {
assert(array instanceof Array);
});
it("copies the source array", function() {
assert(source !== array);
});
it("freezes the array", function() {
assert(Object.isFrozen(array));
});
it("it makes each item immutable", function() {
assert(array[0].__isImmutableObject__);
assert(array[1].__isImmutableObject__);
assert(array[2].__isImmutableObject__);
});
});
describe("set method", function() {
var obj;
beforeEach(function() {
obj = immutable({});
});
it("returns same object if no props are undefined", function() {
assert.strictEqual(obj.set(), obj);
});
it("returns same object if props are empty", function() {
var obj = immutable();
assert.strictEqual(obj.set({}), obj);
});
it("returns a new object", function() {
var newObject = obj.set({a:1});
assert.notStrictEqual(newObject, obj);
});
it("allows key-value arguments", function() {
var newObject = obj.set("a", 1);
assert.equal(newObject.a, 1);
});
it("deeply converts props to immutable", function() {
var newObject = obj.set({ a: { b: { c: 1} } });
assert(newObject.a instanceof immutable.Object);
assert(newObject.a.b instanceof immutable.Object);
});
it("doesn't modify previous object", function() {
var first = immutable({ a: 1 });
var second = first.set({ a: 2, b: 3 });
assert(!first.hasOwnProperty("b"));
assert.equal(first.a, 1);
assert.equal(second.a, 2);
});
it("inherits from original object", function() {
var newObject = obj.set({a:1});
assert(Object.getPrototypeOf(newObject) === obj);
});
it("avoids un-necessary prototype chaining", function() {
var o1 = obj.set({a:1});
var o2 = o1.set({a:1});
assert(Object.getPrototypeOf(o2) === obj);
});
it("gets ALL properties from another immutable object", function() {
var props = immutable({ a: 1 }).set({ b: 2 });
var result = obj.set(props);
assert.equal(result.a, 1);
assert.equal(result.b, 2);
});
});
describe("unset", function() {
var obj;
beforeEach(function() {
obj = immutable({ a: 1 });
});
it("return object without the property", function() {
var newObj = obj.unset("a");
assert.deepEqual(immutable.keys(newObj), []);
});
it("re-uses prototype if key is only in the top-level object", function() {
var base = immutable({a:1});
var current = base.set({b:1});
var result = current.unset("b");
assert.equal(immutable.keys(result).length, 1);
assert.equal(immutable.keys(result).indexOf("b"), -1);
assert.equal(immutable.keys(result).indexOf("a"), 0);
assert.strictEqual(result, base);
});
it("creates new object if key is in prototype", function() {
var base = immutable({a:1});
var current = base.set({b:1});
var result = current.unset("a");
assert.equal(immutable.keys(result).length, 1);
assert.equal(immutable.keys(result).indexOf("a"), -1);
assert.equal(immutable.keys(result).indexOf("b"), 0);
assert.notStrictEqual(result, base);
});
it("returns same object if property doesn't exist", function() {
assert.strictEqual(obj.unset("b"), obj);
});
it("returns empty when object is already empty", function() {
assert.strictEqual(immutable().unset("a"), immutable());
});
});
describe("toJSON", function() {
it("works with JSON.stringify", function() {
var obj = immutable({ a: 1 });
assert.equal(JSON.stringify(obj), "{\"a\":1}");
});
it("includes properties from prototype chain", function() {
var obj = immutable({ a: 1 }).set({ b: 2 });
assert.deepEqual(obj.toJSON(), { a: 1, b: 2 });
});
it("JSONifies nested immutables", function() {
var obj = immutable({ a: 1, b: { c: 2 } });
assert.deepEqual(obj.toJSON(), { a: 1, b: { c: 2 } });
});
it("calls toJSON of property values if exists", function() {
var obj = immutable({ a: { toJSON: function() { return 1; } } });
assert.deepEqual(obj.toJSON(), { a: 1 });
});
});
describe("immutable.keys", function() {
it("includes props from initialization", function() {
var obj = immutable({a:1, b:2});
assert.deepEqual(immutable.keys(obj), ["a", "b"]);
});
it("includes props from prototype", function() {
var obj = immutable({a:1, b:2}).set({c:3});
assert.deepEqual(immutable.keys(obj).sort(), ["a", "b", "c"]);
});
it("doesn't duplicate properties from prototype", function() {
var obj = immutable({a:1, b:2}).set({b:3});
assert.deepEqual(immutable.keys(obj).sort(), ["a", "b" ]);
});
});
describe("using immutable as base class", function() {
it("constructs immutable objects", function() {
var Type = immutable.createClass({
});
var obj = new Type();
assert(Object.isFrozen(obj));
});
it("constructs immutable objects when using init method", function() {
var Type = immutable.createClass({
init: function() { return this; }
});
var obj = new Type();
assert(Object.isFrozen(obj));
});
it("throws if init doesn't return an immutable object", function() {
var Type = immutable.createClass({
init: function() {}
});
assert.throw(function() { new Type(); }, Error);
});
it("can be created without new operator and doesn't break instanceof", function() {
var Type = immutable.createClass({ x: 1 });
var obj = Type();
assert(obj instanceof Type);
});
it("calls init method if defined", function() {
var called = false;
var Type = immutable.createClass({
init: function() { called = true; return this; }
});
var obj = Type();
assert(called);
});
it("passes constructor arguments to init method", function() {
var called;
var Type = immutable.createClass({
init: function(arg) { called = arg; return this; }
});
var obj = Type("arg");
assert.equal(called, "arg");
});
});
describe("lens.prop", function() {
var target = immutable({ foo: 1 });
var foo = lens.prop("foo");
it("can get property value", function() {
assert.equal(foo.get(target), 1);
});
it("can set property value", function() {
var newObj = foo.set(target, 2);
assert.equal(newObj.foo, 2);
});
});
describe("lens.arrayItem", function() {
var original = ["a", "b", "c"];
var item1 = lens.arrayItem(1);
it("can get item", function() {
assert.equal(item1.get(original), "b");
});
it("can set item", function() {
var newArray = item1.set(original, "z");
assert.deepEqual(newArray, ["a", "z", "c"]);
});
});
describe("composed lenses with lens.compose", function() {
var foo = lens.prop("foo");
var bar = lens.prop("bar");
var target = immutable({ foo: { bar: 1 } });
var l = lens.compose(foo, bar);
it("gets inner property", function() {
assert.equal(l.get(target), 1);
});
it("sets inner property", function() {
var result = l.set(target, 2);
assert.equal(result.foo.bar, 2);
});
});
describe("lens.build", function() {
var l = lens.build({
foo: true
});
var target = immutable({
foo: "a"
});
it("creates lens property per object property", function() {
assert("get" in l.foo);
assert("set" in l.foo);
assert.equal(l.foo.get(target), "a");
});
describe("given nested objects", function() {
var nested = lens.build({
foo: { bar: true }
});
var target = immutable({ foo: { bar: "a" } });
it("creates lens for top-level property", function() {
assert("foo" in nested);
assert.equal(nested.foo.get(target), target.foo);
});
it("creates nested lens property", function() {
assert("bar" in nested.foo);
assert.equal(nested.foo.bar.get(target), "a");
});
});
describe("given array", function() {
var arrayItem = lens.build([]);
var target = ["a","b"];
it("creates array item lens factory", function() {
assert.equal(arrayItem(0).get(target), "a");
assert.equal(arrayItem(1).get(target), "b");
});
});
describe("given array with child object", function() {
var arrayItem = lens.build([ { foo: true } ]);
var target = [
{ foo: "a" },
{ foo: "b" }
];
it("creates item object property lenses", function() {
assert.equal(arrayItem(0).foo.get(target), "a");
assert.equal(arrayItem(1).foo.get(target), "b");
});
});
describe("given array with child array", function() {
var arrayItem = lens.build([ { foo: [] } ]);
var target = [
{ foo: ["a"] }
];
it("creates nested array lenses", function() {
assert.equal(arrayItem(0).foo(0).get(target), "a");
});
});
describe("given object with array property", function() {
var l = lens.build({
items: [ { foo: true } ]
});
var target = immutable({ items: [ { foo: "a" } ] });
it("can access array item property", function() {
var itemProp = l.items(0).foo;
assert.equal(itemProp.get(target), "a");
});
it("can update array item property", function() {
var itemProp = l.items(0).foo;
var newObj = itemProp.set(target, "b");
assert.equal(newObj.items[0].foo, "b");
});
});
describe("object -> array -> object -> array", function() {
var l = lens.build({
a: [
{ b: [] }
]
});
var target = immutable({
a: [
{ b: [1,2,3] }
]
});
it("can get nested array", function() {
var prop = l.a(0).b;
assert.equal(prop.get(target), target.a[0].b);
});
it("can set nested array", function() {
var prop = l.a(0).b;
var newObject = prop.set(target, [4,5,6]);
assert.deepEqual(newObject.a[0].b, [4,5,6]);
});
});
it("supports complex structures", function() {
var l = lens.build({
customers: [
{
name: true,
invoices: [
{ items: [ { name: true, qty: true, price: true } ] }
]
}
]
});
var data = immutable({
customers: [
{
name: "Foo Corp",
invoices: [
{
items: [
{ name: "Nuts", qty: 100, price: 2 },
{ name: "Bolts", qty: 100, price: 3 }
]
}
]
}
]
});
var priceLens = l.customers(0).invoices(0).items(1).price;
var newInvoice = priceLens.set(data, 4);
assert.equal(newInvoice.customers[0].invoices[0].items[1].price, 4);
});
});