From d8e2005ff421b3030d07ca2fcd321190c43d7a8f Mon Sep 17 00:00:00 2001 From: Jake Lazaroff Date: Sun, 21 Feb 2016 22:05:07 -0500 Subject: [PATCH] Deep clone Immutable to create other copy --- test/test.js | 101 +++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 56 deletions(-) diff --git a/test/test.js b/test/test.js index b3e80f4..5367c8a 100644 --- a/test/test.js +++ b/test/test.js @@ -1,17 +1,28 @@ 'use strict'; +// From http://stackoverflow.com/a/728694 +function clone(obj) { + if (null === obj || 'object' !== typeof obj) return obj; + var copy = obj.constructor(); + for (var attr in obj) { + if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; + } + return copy; +} + var typeEnv; 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'; } else typeEnv = 'PhantomJS'; +var clonedImmutable = clone(Immutable); + var assert = chai.assert; var expect = chai.expect; var List = Immutable.List; @@ -46,9 +57,7 @@ describe('chai-immutable (' + typeEnv + ')', function () { list: List.of(42) }); - if (typeEnv === 'Node.js') { - var otherImmutableList = otherImmutable.List.of(1, 2, 3); - } + var clonedImmutableList = clonedImmutable.List.of(1, 2, 3); describe('BDD interface', function () { describe('empty property', function () { @@ -74,11 +83,9 @@ describe('chai-immutable (' + typeEnv + ')', 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; - }); - } + it('should work if using different copies of Immtuable', function () { + expect(clonedImmutable.List()).to.be.empty; + }); }); describe('equal method', function () { @@ -174,11 +181,9 @@ describe('chai-immutable (' + typeEnv + ')', function () { 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)); - }); - } + it('should work if using different copies of Immtuable', function () { + expect(clonedImmutableList).to.equal(clonedImmutable.List.of(1, 2, 3)); + }); }); describe('include method', function () { @@ -394,11 +399,9 @@ describe('chai-immutable (' + typeEnv + ')', function () { 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'); - }); - } + it('should work if using different copies of Immtuable', function () { + expect(clonedImmutable.Map({ x: 1 })).to.have.key('x'); + }); }); describe('size method', function () { @@ -423,11 +426,9 @@ describe('chai-immutable (' + typeEnv + ')', 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); - }); - } + it('should work if using different copies of Immtuable', function () { + expect(clonedImmutableList).to.have.size(3); + }); }); describe('size property', function () { @@ -560,11 +561,9 @@ describe('chai-immutable (' + typeEnv + ')', 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); - }); - } + it('should work if using different copies of Immtuable', function () { + expect(clonedImmutableList).to.have.size.above(2); + }); }); }); @@ -604,11 +603,9 @@ describe('chai-immutable (' + typeEnv + ')', 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)); - }); - } + it('should work if using different copies of Immtuable', function () { + assert.equal(clonedImmutableList, clonedImmutable.List.of(1, 2, 3)); + }); }); describe('notEqual assertion', function () { @@ -637,11 +634,9 @@ describe('chai-immutable (' + typeEnv + ')', 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()); - }); - } + it('should work if using different copies of Immtuable', function () { + assert.notEqual(clonedImmutableList, clonedImmutable.List.of()); + }); }); describe('unoverridden strictEqual and deepEqual assertions', function () { @@ -665,12 +660,10 @@ describe('chai-immutable (' + typeEnv + ')', function () { 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)); - }); - } + it('should work if using different copies of Immtuable', function () { + assert.strictEqual(clonedImmutableList, clonedImmutable.List.of(1, 2, 3)); + assert.deepEqual(clonedImmutableList, clonedImmutable.List.of(1, 2, 3)); + }); }); describe('unoverridden notStrictEqual and notDeepEqual assertions', function () { @@ -694,12 +687,10 @@ describe('chai-immutable (' + typeEnv + ')', function () { 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()); - }); - } + it('should work if using different copies of Immtuable', function () { + assert.notStrictEqual(clonedImmutableList, clonedImmutable.List()); + assert.notDeepEqual(clonedImmutableList, clonedImmutable.List()); + }); }); describe('sizeOf assertion', function () { @@ -715,11 +706,9 @@ describe('chai-immutable (' + typeEnv + ')', 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); - }); - } + it('should work if using different copies of Immtuable', function () { + assert.sizeOf(clonedImmutableList, 3); + }); }); }); });