diff --git a/chai-immutable.js b/chai-immutable.js index 1adb128..230e457 100644 --- a/chai-immutable.js +++ b/chai-immutable.js @@ -24,6 +24,13 @@ var Assertion = chai.Assertion; + function assertIsIterable(obj) { + new Assertion(obj).assert( + Immutable.Iterable.isIterable(obj), + 'expected #{this} to be an Iterable' + ); + } + /** * ## BDD API Reference */ @@ -46,7 +53,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'); @@ -96,7 +103,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}', @@ -141,7 +148,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}', @@ -217,14 +224,13 @@ var obj = this._obj; - if (obj && obj instanceof KeyedCollection) { + if (Immutable.Iterable.isKeyed(obj)) { switch (utils.type(keys)) { case 'object': - if (keys instanceof IndexedCollection || - keys instanceof SetCollection) { + if (Immutable.Iterable.isIndexed(keys)) keys = keys.toJS(); - } - else if (keys instanceof KeyedCollection) keys = keys.keySeq().toJS(); + else if (Immutable.Iterable.isIterable(keys)) + keys = keys.keySeq().toJS(); else keys = Object.keys(keys); case 'array': if (arguments.length > 1) throw new Error( @@ -309,7 +315,7 @@ */ function assertCollectionSize(n) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -335,7 +341,7 @@ function assertCollectionSizeLeast(_super) { return function (n) { if (utils.flag(this, 'immutable.collection.size')) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -355,7 +361,7 @@ function assertCollectionSizeMost(_super) { return function (n) { if (utils.flag(this, 'immutable.collection.size')) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -375,7 +381,7 @@ function assertCollectionSizeAbove(_super) { return function (n) { if (utils.flag(this, 'immutable.collection.size')) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -395,7 +401,7 @@ function assertCollectionSizeBelow(_super) { return function (n) { if (utils.flag(this, 'immutable.collection.size')) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -429,7 +435,7 @@ Assertion.overwriteMethod('within', function (_super) { return function (min, max) { if (utils.flag(this, 'immutable.collection.size')) { - new Assertion(this._obj).instanceof(Collection); + assertIsIterable(this._obj); var size = this._obj.size; new Assertion(size).a('number'); @@ -483,7 +489,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); @@ -509,7 +515,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); diff --git a/test/test.js b/test/test.js index c982f3f..b3e80f4 100644 --- a/test/test.js +++ b/test/test.js @@ -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'; @@ -45,6 +46,10 @@ describe('chai-immutable (' + typeEnv + ')', function () { list: List.of(42) }); + if (typeEnv === 'Node.js') { + var otherImmutableList = otherImmutable.List.of(1, 2, 3); + } + describe('BDD interface', function () { describe('empty property', function () { it('should pass given an empty collection', function () { @@ -68,6 +73,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 () { @@ -162,6 +173,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 () { @@ -376,6 +393,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 () { @@ -399,6 +422,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 () { @@ -530,6 +559,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); + }); + } }); }); @@ -568,6 +603,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 () { @@ -595,6 +636,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 () { @@ -617,6 +664,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 () { @@ -639,6 +693,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 () { @@ -653,6 +714,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); + }); + } }); }); });