UNPKG

22 kBJavaScriptView Raw
1'use strict';
2
3var typeEnv;
4if (!chai) {
5 var chai = require('chai');
6 var chaiImmutable = require('../chai-immutable');
7 var Immutable = require('immutable');
8
9 chai.use(chaiImmutable);
10 typeEnv = 'Node.js';
11}
12else typeEnv = 'PhantomJS';
13
14var assert = chai.assert;
15var expect = chai.expect;
16var List = Immutable.List;
17var Map = Immutable.Map;
18var Set = Immutable.Set;
19var Stack = Immutable.Stack;
20
21/*!
22 * Test helper to check that a given function (wrapping the assertion) will
23 * fail.
24 */
25function fail(fn) {
26 expect(fn).to.throw(chai.AssertionError);
27}
28
29describe('chai-immutable (' + typeEnv + ')', function () {
30 var list3 = List.of(1, 2, 3);
31
32 var deepMap = new Map({
33 foo: 'bar',
34 list: List.of(1, 2, 3)
35 });
36
37 var sameDeepMap = new Map({
38 foo: 'bar',
39 list: List.of(1, 2, 3)
40 });
41
42 var differentDeepMap = new Map({
43 foo: 'bar',
44 list: List.of(42)
45 });
46
47 describe('BDD interface', function () {
48 describe('empty property', function () {
49 it('should pass given an empty collection', function () {
50 expect(new List()).to.be.empty;
51 });
52
53 it('should pass using `not` given a non-empty collection', function () {
54 expect(list3).to.not.be.empty;
55 });
56
57 it('should not affect the original assertions', function () {
58 expect([]).to.be.empty;
59 expect('').to.be.empty;
60 expect({}).to.be.empty;
61 });
62
63 it('should fail given a non-empty collection', function () {
64 fail(function () { expect(list3).to.be.empty; });
65 });
66
67 it('should fail using `not` given an empty collection', function () {
68 fail(function () { expect(new List()).to.not.be.empty; });
69 });
70 });
71
72 describe('equal method', function () {
73 it('should pass given equal values', function () {
74 expect(list3).to.equal(List.of(1, 2, 3));
75 });
76
77 it('should pass using `not` given different values', function () {
78 expect(list3).to.not.equal(new List());
79 });
80
81 it('aliases of equal should also work', function () {
82 expect(list3).to.equal(List.of(1, 2, 3));
83 expect(list3).to.not.equal(new List());
84 expect(list3).to.equals(List.of(1, 2, 3));
85 expect(list3).to.not.equals(new List());
86 expect(list3).to.eq(List.of(1, 2, 3));
87 expect(list3).to.not.eq(new List());
88 expect(list3).to.eql(List.of(1, 2, 3));
89 expect(list3).to.not.eql(new List());
90 expect(list3).to.deep.equal(List.of(1, 2, 3));
91 expect(list3).to.not.deep.equal(new List());
92 });
93
94 it('should not affect the original assertions', function () {
95 expect('hello').to.equal('hello');
96 expect(42).to.equal(42);
97 expect(1).to.not.equal(true);
98 expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });
99 expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });
100 });
101
102 it('should pass given deeply equal values', function () {
103 expect(deepMap).to.equal(sameDeepMap);
104 expect(deepMap).to.equals(sameDeepMap);
105 expect(deepMap).to.eq(sameDeepMap);
106 expect(deepMap).to.eql(sameDeepMap);
107 expect(deepMap).to.deep.equal(sameDeepMap);
108 });
109
110 it('should pass using `not` given deeply different values', function () {
111 expect(deepMap).to.not.equal(differentDeepMap);
112 expect(deepMap).to.not.equals(differentDeepMap);
113 expect(deepMap).to.not.eq(differentDeepMap);
114 expect(deepMap).to.not.eql(differentDeepMap);
115 expect(deepMap).to.not.deep.equal(differentDeepMap);
116 });
117
118 it('should pass using `not` given a non-Immutable value', function () {
119 expect([]).to.not.equal(List());
120 });
121
122 it('should fail given a non-Immutable value', function () {
123 fail(function () { expect([]).to.equal(List()); });
124 });
125
126 it('should fail given different values', function () {
127 fail(function () { expect(list3).to.equal(new List()); });
128 });
129
130 it('should fail using `not` given equal values', function () {
131 fail(function () { expect(list3).to.not.equal(List.of(1, 2, 3)); });
132 });
133
134 it('should fail given deeply different values', function () {
135 fail(function () { expect(deepMap).to.equal(differentDeepMap); });
136 fail(function () { expect(deepMap).to.equals(differentDeepMap); });
137 fail(function () { expect(deepMap).to.eq(differentDeepMap); });
138 fail(function () { expect(deepMap).to.eql(differentDeepMap); });
139 fail(function () { expect(deepMap).to.deep.equal(differentDeepMap); });
140 });
141
142 it('should fail using `not` given deeply equal values', function () {
143 fail(function () { expect(deepMap).to.not.equal(sameDeepMap); });
144 fail(function () { expect(deepMap).to.not.equals(sameDeepMap); });
145 fail(function () { expect(deepMap).to.not.eq(sameDeepMap); });
146 fail(function () { expect(deepMap).to.not.eql(sameDeepMap); });
147 fail(function () { expect(deepMap).to.not.deep.equal(sameDeepMap); });
148 });
149 });
150
151 describe('include method', function () {
152 it('should pass given an existing value', function () {
153 expect(new List([1, 2, 3])).to.include(2);
154 });
155
156 it('should pass using `not` given an inexisting value', function () {
157 expect(new List([1, 2, 3])).to.not.include(42);
158 });
159
160 it('should chain and pass given an existing key', function () {
161 expect(new Map({
162 foo: 'bar',
163 hello: 'universe'
164 })).to.include.keys('foo');
165 });
166
167 it('should chain and pass using `not` given an inexisting key', function () {
168 expect(new Map({
169 foo: 'bar',
170 hello: 'universe'
171 })).to.not.include.keys('not-foo');
172 });
173
174 it('aliases of include should also work', function () {
175 expect(new List([1, 2, 3])).contain(2);
176 expect(new List([1, 2, 3])).not.contain(42);
177 expect(new List([1, 2, 3])).contains(2);
178 expect(new List([1, 2, 3])).not.contains(42);
179 expect(new List([1, 2, 3])).includes(2);
180 expect(new List([1, 2, 3])).not.includes(42);
181 });
182
183 it('should not affect the original assertions', function () {
184 expect([1, 2, 3]).to.include(2);
185 expect('foobar').to.contain('foo');
186 expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');
187 });
188
189 it('should fail given an inexisting value', function () {
190 fail(function () { expect(new List([1, 2, 3])).to.include(42); });
191 });
192
193 it('should fail using `not` given an existing value', function () {
194 fail(function () { expect(new List([1, 2, 3])).not.to.include(2); });
195 });
196
197 it('should chain and fail given an inexisting key', function () {
198 fail(function () {
199 expect(new Map({
200 foo: 'bar',
201 hello: 'universe'
202 })).to.include.keys('not-foo');
203 });
204 });
205
206 it('should chain and fail using `not` given an existing key', function () {
207 fail(function () {
208 expect(new Map({
209 foo: 'bar',
210 hello: 'universe'
211 })).to.not.include.keys('foo');
212 });
213 });
214 });
215
216 describe('keys method', function () {
217 var map = new Map({ x: 1, y: 2 });
218 var obj = { x: 1, y: 2 };
219
220 it('should pass given an existing key', function () {
221 expect(new Map({ x: 1 })).to.have.key('x');
222 expect({ x: 1 }).to.have.key('x');
223 });
224
225 it('should pass using `not` given an inexisting key', function () {
226 expect(map).to.not.have.key('z');
227 expect(obj).to.not.have.key('z');
228 });
229
230 it('should pass given multiple existing keys', function () {
231 expect(map).to.have.keys('x', 'y');
232 expect(obj).to.have.keys('x', 'y');
233 });
234
235 it('should pass using `not` given multiple inexisting keys', function () {
236 expect(map).to.not.have.keys('z1', 'z2');
237 expect(obj).to.not.have.keys('z1', 'z2');
238 });
239
240 it('should accept an Array of keys to check against', function () {
241 expect(map).to.have.keys(['x', 'y']);
242 expect(obj).to.have.keys(['x', 'y']);
243 });
244
245 it('should accept a List of keys to check against', function () {
246 expect(map).to.have.keys(new List(['x', 'y']));
247 });
248
249 it('should accept a Set of keys to check against', function () {
250 expect(map).to.have.keys(new Set(['x', 'y']));
251 });
252
253 it('should accept a Stack of keys to check against', function () {
254 expect(map).to.have.keys(new Stack(['x', 'y']));
255 });
256
257 it('should accept an Object to check against', function () {
258 expect(map).to.have.keys({ 'x': 6, 'y': 7 });
259 expect(obj).to.have.keys({ 'x': 6, 'y': 7 });
260 });
261
262 it('should accept a Map to check against', function () {
263 expect(map).to.have.keys(new Map({ 'x': 6, 'y': 7 }));
264 });
265
266 it('should pass using `any` given an existing key', function () {
267 expect(map).to.have.any.keys('x', 'z');
268 expect(obj).to.have.any.keys('x', 'z');
269 });
270
271 it('should pass using `not` and `any` given inexisting keys', function () {
272 expect(map).to.not.have.any.keys('z1', 'z2');
273 expect(obj).to.not.have.any.keys('z1', 'z2');
274 });
275
276 it('should pass using `all` given existing keys', function () {
277 expect(map).to.have.all.keys('x', 'y');
278 expect(obj).to.have.all.keys('x', 'y');
279 });
280
281 it('should pass using `not` and `all` given inexisting keys', function () {
282 expect(map).to.not.have.all.keys('z1', 'y');
283 expect(obj).to.not.have.all.keys('z1', 'y');
284 });
285
286 it('should pass using `contain` given an existing key', function () {
287 expect(map).to.contain.key('x');
288 expect(obj).to.contain.key('x');
289 });
290
291 it('should not affect the original assertions', function () {
292 expect({ x: 1, y: 2 }).to.have.any.keys('x', 'z');
293 expect({ x: 1, y: 2 }).to.have.any.keys('x');
294 expect({ x: 1, y: 2 }).to.contain.any.keys('y', 'z');
295 expect({ x: 1, y: 2 }).to.contain.any.keys(['x']);
296 expect({ x: 1, y: 2 }).to.contain.any.keys({ 'x': 6 });
297 expect({ x: 1, y: 2 }).to.have.all.keys(['x', 'y']);
298 expect({ x: 1, y: 2 }).to.have.all.keys({ 'x': 6, 'y': 7 });
299 expect({ x: 1, y: 2, z: 3 }).to.contain.all.keys(['x', 'y']);
300 expect({ x: 1, y: 2, z: 3 }).to.contain.all.keys({ 'x': 6 });
301 });
302
303 it('should fail given an inexisting key', function () {
304 fail(function () { expect(new Map({ x: 1 })).to.have.key('z'); });
305 fail(function () { expect({ x: 1 }).to.have.key('z'); });
306 });
307
308 it('should fail using `not` given an inexisting key', function () {
309 fail(function () { expect(map).to.not.have.key('x'); });
310 fail(function () { expect(obj).to.not.have.key('x'); });
311 });
312
313 it('should fail given multiple inexisting keys', function () {
314 fail(function () { expect(map).to.have.keys('z1', 'z2'); });
315 fail(function () { expect(obj).to.have.keys('z1', 'z2'); });
316 });
317
318 it('should fail using `not` given multiple existing keys', function () {
319 fail(function () { expect(map).to.not.have.keys('x', 'y'); });
320 fail(function () { expect(obj).to.not.have.keys('x', 'y'); });
321 });
322
323 it('should fail using `any` given inexisting keys', function () {
324 fail(function () { expect(map).to.have.any.keys('z1', 'z2'); });
325 fail(function () { expect(obj).to.have.any.keys('z1', 'z2'); });
326 });
327
328 it('should fail using `not` and `any` given an existing key', function () {
329 fail(function () { expect(map).to.not.have.any.keys('x', 'z'); });
330 fail(function () { expect(obj).to.not.have.any.keys('x', 'z'); });
331 });
332
333 it('should fail using `all` given an inexisting key', function () {
334 fail(function () { expect(map).to.have.all.keys('z1', 'y'); });
335 fail(function () { expect(obj).to.have.all.keys('z1', 'y'); });
336 });
337
338 it('should fail using `not` and `all` given existing keys', function () {
339 fail(function () { expect(map).to.not.have.all.keys('x', 'y'); });
340 fail(function () { expect(obj).to.not.have.all.keys('x', 'y'); });
341 });
342
343 it('should fail using `contain` given an inexisting key', function () {
344 fail(function () { expect(map).to.contain.key('z'); });
345 fail(function () { expect(obj).to.contain.key('z'); });
346 });
347 });
348
349 describe('size method', function () {
350 it('should pass given the right size', function () {
351 expect(list3).to.have.size(3);
352 });
353
354 it('should pass using `not` given the wrong size', function () {
355 expect(list3).to.not.have.size(42);
356 });
357
358 it('should also work with alias sizeOf', function () {
359 expect(list3).to.have.sizeOf(3);
360 expect(list3).to.not.have.sizeOf(42);
361 });
362
363 it('should fail given the wrong size', function () {
364 fail(function () { expect(list3).to.have.size(42); });
365 });
366
367 it('should fail using `not` given the right size', function () {
368 fail(function () { expect(list3).to.not.have.size(3); });
369 });
370 });
371
372 describe('size property', function () {
373 it('above should pass given a good min size', function () {
374 expect(list3).to.have.size.above(2);
375 });
376
377 it('above should pass using `not` given a bad min size', function () {
378 expect(list3).to.not.have.size.above(42);
379 });
380
381 it('aliases of above should also work', function () {
382 expect(list3).to.have.size.gt(2);
383 expect(list3).to.have.size.greaterThan(2);
384 expect(list3).to.not.have.size.gt(42);
385 expect(list3).to.not.have.size.greaterThan(42);
386 });
387
388 it('should not affect the original assertions of above', function () {
389 expect('foo').to.have.length.above(2);
390 expect([1, 2, 3]).to.have.length.above(2);
391 });
392
393 it('above should fail given a bad min size', function () {
394 fail(function () { expect(list3).to.have.size.above(42); });
395 });
396
397 it('above should fail using `not` given a good min size', function () {
398 fail(function () { expect(list3).to.not.have.size.above(2); });
399 });
400
401 it('below should pass given a good max size', function () {
402 expect(list3).to.have.size.below(42);
403 });
404
405 it('below should pass using `not` given a bad max size', function () {
406 expect(list3).to.not.have.size.below(1);
407 });
408
409 it('aliases of below should also work', function () {
410 expect(list3).to.have.size.lt(4);
411 expect(list3).to.have.size.lessThan(4);
412 expect(list3).to.not.have.size.lt(1);
413 expect(list3).to.not.have.size.lessThan(1);
414 });
415
416 it('should not affect the original assertions of below', function () {
417 expect('foo').to.have.length.below(4);
418 expect([1, 2, 3]).to.have.length.below(4);
419 });
420
421 it('below should fail given a bad max size', function () {
422 fail(function () { expect(list3).to.have.size.below(1); });
423 });
424
425 it('below should fail using `not` given a good max size', function () {
426 fail(function () { expect(list3).to.not.have.size.below(42); });
427 });
428
429 it('within should pass given a good range', function () {
430 expect(list3).to.have.size.within(2, 42);
431 });
432
433 it('within should pass using `not` given a bad range', function () {
434 expect(list3).to.not.have.size.within(10, 20);
435 });
436
437 it('should not affect the original assertions of within', function () {
438 expect('foo').to.have.length.within(2, 4);
439 expect([1, 2, 3]).to.have.length.within(2, 4);
440 });
441
442 it('within should fail given a bad range', function () {
443 fail(function () { expect(list3).to.have.size.within(10, 20); });
444 });
445
446 it('within should fail using `not` given a good range', function () {
447 fail(function () { expect(list3).to.not.have.size.within(2, 42); });
448 });
449
450 it('least should pass given a good min size', function () {
451 expect(list3).to.have.size.of.at.least(2);
452 });
453
454 it('least should pass using `not` given a bad min size', function () {
455 expect(list3).to.not.have.size.of.at.least(42);
456 });
457
458 it('aliases of least should also work', function () {
459 expect(list3).to.have.size.gte(2);
460 expect(list3).to.not.have.size.gte(42);
461 });
462
463 it('should not affect the original assertions of least', function () {
464 expect('foo').to.have.length.of.at.least(2);
465 expect([1, 2, 3]).to.have.length.of.at.least(3);
466 });
467
468 it('least should fail given a bad min size', function () {
469 fail(function () { expect(list3).to.have.size.of.at.least(42); });
470 });
471
472 it('least should fail using `not` given a good min size', function () {
473 fail(function () { expect(list3).to.not.have.size.of.at.least(2); });
474 });
475
476 it('most should pass given a good max size', function () {
477 expect(list3).to.have.size.of.at.most(42);
478 });
479
480 it('most should pass using `not` given a bad max size', function () {
481 expect(list3).to.not.have.size.of.at.most(2);
482 });
483
484 it('aliases of most should also work', function () {
485 expect(list3).to.have.size.lte(42);
486 expect(list3).to.not.have.size.lte(2);
487 });
488
489 it('should not affect the original assertions of most', function () {
490 expect('foo').to.have.length.of.at.most(4);
491 expect([1, 2, 3]).to.have.length.of.at.most(3);
492 });
493
494 it('most should fail given a good max size', function () {
495 fail(function () { expect(list3).to.have.size.of.at.most(2); });
496 });
497
498 it('most should fail using `not` given a bad max size', function () {
499 fail(function () { expect(list3).to.not.have.size.of.at.most(42); });
500 });
501 });
502 });
503
504 describe('TDD interface', function () {
505 describe('equal assertion', function () {
506 it('should pass given equal values', function () {
507 assert.equal(list3, List.of(1, 2, 3));
508 });
509
510 it('should pass given deeply equal values', function () {
511 assert.equal(deepMap, sameDeepMap);
512 });
513
514 it('should not affect the original assertion', function () {
515 assert.equal(42, 42);
516 assert.equal(3, '3');
517 });
518
519 it('should fail given a non-Immutable value', function () {
520 fail(function () { assert.equal([], List()); });
521 });
522
523 it('should fail given different values', function () {
524 fail(function () { assert.equal(list3, new List()); });
525 });
526
527 it('should fail given deeply different values', function () {
528 fail(function () { assert.equal(deepMap, differentDeepMap); });
529 });
530 });
531
532 describe('notEqual assertion', function () {
533 it('should pass given different values', function () {
534 assert.notEqual(list3, new List());
535 });
536
537 it('should pass given deeply different values', function () {
538 assert.notEqual(deepMap, differentDeepMap);
539 });
540
541 it('should not affect the original assertion', function () {
542 assert.notEqual('oui', 'non');
543 assert.notEqual({ foo: 'bar' }, { foo: 'bar' });
544 });
545
546 it('should pass given a non-Immutable value', function () {
547 assert.notEqual([], List());
548 });
549
550 it('should fail given equal values', function () {
551 fail(function () { assert.notEqual(list3, List.of(1, 2, 3)); });
552 });
553
554 it('should fail given deeply equal values', function () {
555 fail(function () { assert.notEqual(deepMap, sameDeepMap); });
556 });
557 });
558
559 describe('unoverridden strictEqual and deepEqual assertions', function () {
560 it('should pass given equal values', function () {
561 assert.strictEqual(list3, List.of(1, 2, 3));
562 assert.deepEqual(list3, List.of(1, 2, 3));
563 });
564
565 it('should pass given deeply equal values', function () {
566 assert.strictEqual(deepMap, sameDeepMap);
567 assert.deepEqual(deepMap, sameDeepMap);
568 });
569
570 it('should fail given different values', function () {
571 fail(function () { assert.strictEqual(list3, new List()); });
572 fail(function () { assert.deepEqual(list3, new List()); });
573 });
574
575 it('should fail given deeply different values', function () {
576 fail(function () { assert.strictEqual(deepMap, differentDeepMap); });
577 fail(function () { assert.deepEqual(deepMap, differentDeepMap); });
578 });
579 });
580
581 describe('unoverridden notStrictEqual and notDeepEqual assertions', function () {
582 it('should pass given different values', function () {
583 assert.notStrictEqual(list3, new List());
584 assert.notDeepEqual(list3, new List());
585 });
586
587 it('should pass given deeply different values', function () {
588 assert.notStrictEqual(deepMap, differentDeepMap);
589 assert.notDeepEqual(deepMap, differentDeepMap);
590 });
591
592 it('should fail given equal values', function () {
593 fail(function () { assert.notStrictEqual(list3, List.of(1, 2, 3)); });
594 fail(function () { assert.notDeepEqual(list3, List.of(1, 2, 3)); });
595 });
596
597 it('should fail given deeply equal values', function () {
598 fail(function () { assert.notStrictEqual(deepMap, sameDeepMap); });
599 fail(function () { assert.notDeepEqual(deepMap, sameDeepMap); });
600 });
601 });
602
603 describe('sizeOf assertion', function () {
604 it('should pass given the right size', function () {
605 assert.sizeOf(list3, 3);
606 });
607
608 it('should work with empty collections', function () {
609 assert.sizeOf(new List(), 0);
610 });
611
612 it('should fail given the wrong size', function () {
613 fail(function () { assert.sizeOf(list3, 42); });
614 });
615 });
616 });
617});