Skip to content

Commit

Permalink
Use Iterable.isIterable for Immutable check
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelazaroff committed Dec 13, 2015
1 parent 6a9c9ed commit ef282de
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
12 changes: 6 additions & 6 deletions chai-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
return function () {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
var size = obj.size;
new Assertion(size).a('number');

Expand Down Expand Up @@ -96,7 +96,7 @@
return function (collection) {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
this.assert(
Immutable.is(obj, collection),
'expected #{act} to equal #{exp}',
Expand Down Expand Up @@ -141,7 +141,7 @@
return function (val) {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
this.assert(
obj.includes(val),
'expected #{act} to include #{exp}',
Expand Down Expand Up @@ -217,7 +217,7 @@

var obj = this._obj;

if (obj && obj instanceof KeyedCollection) {
if (obj instanceof KeyedCollection) {
switch (utils.type(keys)) {
case 'object':
if (keys instanceof IndexedCollection ||
Expand Down Expand Up @@ -483,7 +483,7 @@
// It seems like we shouldn't actually need this check, however,
// `assert.equal` actually behaves differently than its BDD counterpart!
// Namely, the BDD version is strict while the "assert" one isn't.
if (actual instanceof Collection) {
if (Immutable.Iterable.isIterable(actual)) {
return new Assertion(actual).equal(expected);
}
else return originalEqual(actual, expected);
Expand All @@ -509,7 +509,7 @@
*/

assert.notEqual = function (actual, expected) {
if (actual instanceof Collection) {
if (Immutable.Iterable.isIterable(actual)) {
return new Assertion(actual).not.equal(expected);
}
else return originalNotEqual(actual, expected);
Expand Down
65 changes: 65 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (!chai) {
var chai = require('chai');
var chaiImmutable = require('../chai-immutable');
var Immutable = require('immutable');
var otherImmutable = require('../node_modules/immutable/dist/immutable.min.js');

chai.use(chaiImmutable);
typeEnv = 'Node.js';
Expand Down Expand Up @@ -45,6 +46,8 @@ describe('chai-immutable (' + typeEnv + ')', function () {
list: List.of(42)
});

var otherImmutableList = otherImmutable.List.of(1, 2, 3);

describe('BDD interface', function () {
describe('empty property', function () {
it('should pass given an empty collection', function () {
Expand All @@ -68,6 +71,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given an empty collection', function () {
fail(function () { expect(new List()).to.not.be.empty; });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
expect(otherImmutable.List()).to.be.empty;
});
}
});

describe('equal method', function () {
Expand Down Expand Up @@ -162,6 +171,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { expect(deepMap).to.not.eqls(sameDeepMap); });
fail(function () { expect(deepMap).to.not.deep.equal(sameDeepMap); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
expect(otherImmutableList).to.equal(otherImmutable.List.of(1, 2, 3));
});
}
});

describe('include method', function () {
Expand Down Expand Up @@ -376,6 +391,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { expect(map).to.contain.key('z'); });
fail(function () { expect(obj).to.contain.key('z'); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
expect(otherImmutable.Map({ x: 1 })).to.have.key('x');
});
}
});

describe('size method', function () {
Expand All @@ -399,6 +420,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given the right size', function () {
fail(function () { expect(list3).to.not.have.size(3); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
expect(otherImmutableList).to.have.size(3);
});
}
});

describe('size property', function () {
Expand Down Expand Up @@ -530,6 +557,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('most should fail using `not` given a bad max size', function () {
fail(function () { expect(list3).to.not.have.size.of.at.most(42); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
expect(otherImmutableList).to.have.size.above(2);
});
}
});
});

Expand Down Expand Up @@ -568,6 +601,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given deeply different values', function () {
fail(function () { assert.equal(deepMap, differentDeepMap); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
assert.equal(otherImmutableList, otherImmutable.List.of(1, 2, 3));
});
}
});

describe('notEqual assertion', function () {
Expand Down Expand Up @@ -595,6 +634,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given deeply equal values', function () {
fail(function () { assert.notEqual(deepMap, sameDeepMap); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
assert.notEqual(otherImmutableList, otherImmutable.List.of());
});
}
});

describe('unoverridden strictEqual and deepEqual assertions', function () {
Expand All @@ -617,6 +662,13 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { assert.strictEqual(deepMap, differentDeepMap); });
fail(function () { assert.deepEqual(deepMap, differentDeepMap); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
assert.strictEqual(otherImmutableList, otherImmutable.List.of(1, 2, 3));
assert.deepEqual(otherImmutableList, otherImmutable.List.of(1, 2, 3));
});
}
});

describe('unoverridden notStrictEqual and notDeepEqual assertions', function () {
Expand All @@ -639,6 +691,13 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { assert.notStrictEqual(deepMap, sameDeepMap); });
fail(function () { assert.notDeepEqual(deepMap, sameDeepMap); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
assert.notStrictEqual(otherImmutableList, otherImmutable.List());
assert.notDeepEqual(otherImmutableList, otherImmutable.List());
});
}
});

describe('sizeOf assertion', function () {
Expand All @@ -653,6 +712,12 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given the wrong size', function () {
fail(function () { assert.sizeOf(list3, 42); });
});

if (typeEnv === 'Node.js') {
it('should work if using different copies of Immtuable', function () {
assert.sizeOf(otherImmutableList, 3);
});
}
});
});
});

0 comments on commit ef282de

Please sign in to comment.