Skip to content

Commit

Permalink
Deep clone Immutable to create other copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Lazaroff committed Feb 22, 2016
1 parent feeb3af commit d8e2005
Showing 1 changed file with 45 additions and 56 deletions.
101 changes: 45 additions & 56 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});

Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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);
});
});
});
});

0 comments on commit d8e2005

Please sign in to comment.