From de64e7be2bd3d276f5c9e77ffdd9541ed740f671 Mon Sep 17 00:00:00 2001 From: Vasyl Date: Fri, 20 Jan 2017 00:25:38 +0100 Subject: [PATCH] Immutable comparison fixed, new methods added. --- index-test.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++--- index.js | 42 +++++++++++++++++++++++++-- package.json | 6 ++-- 3 files changed, 119 insertions(+), 9 deletions(-) diff --git a/index-test.js b/index-test.js index 02b8607..6168995 100644 --- a/index-test.js +++ b/index-test.js @@ -35,7 +35,7 @@ describe('expect immutable', () => { expect(() => expect(Immutable.Map({})).toEqualImmutable(Immutable.Map({ a: 1 })) ).toThrow( - 'Expected {} to equal { a: 1 }' + 'Expected Map {} to equal Map { "a": 1 }' ); }); @@ -51,7 +51,7 @@ describe('expect immutable', () => { expect(() => expect(Immutable.List([])).toEqualImmutable(Immutable.List([1])) ).toThrow( - 'Expected [] to equal [ 1 ]' + 'Expected List [] to equal List [ 1 ]' ); }); @@ -62,6 +62,22 @@ describe('expect immutable', () => { it('does not throw when Lists are the same', () => { expect(Immutable.List([1])).toEqualImmutable(Immutable.List([1])); }); + + it('throws when object types are different', () => { + expect(() => + expect(Immutable.List()).toEqualImmutable(Immutable.Seq()) + ).toThrow( + 'Expected List [] and Seq [] to be of the same type' + ); + }); + + it('throws when Map keys are of different type', () => { + expect(() => + expect(Immutable.Map().set(1, 2)).toEqualImmutable(Immutable.Map().set('1', 2)) + ).toThrow( + 'Expected Map { 1: 2 } to equal Map { "1": 2 }' + ); + }); }); context('toNotEqualImmutable', () => { @@ -89,7 +105,7 @@ describe('expect immutable', () => { expect(() => expect(Immutable.Map({ a: 1 })).toNotEqualImmutable(Immutable.Map({ a: 1 })) ).toThrow( - 'Expected { a: 1 } to not equal { a: 1 }' + 'Expected Map { "a": 1 } not to equal Map { "a": 1 }' ); }); @@ -101,8 +117,64 @@ describe('expect immutable', () => { expect(() => expect(Immutable.List([1])).toNotEqualImmutable(Immutable.List([1])) ).toThrow( - 'Expected [ 1 ] to not equal [ 1 ]' + 'Expected List [ 1 ] not to equal List [ 1 ]' + ); + }); + }); + + context('toBeSupersetImmutable', () => { + it('throws when each List has unique values', () => { + expect(() => { + const pseudoSuperset = Immutable.List([1, Immutable.List([2, 3])]); + const pseudoSubset = Immutable.List([1, Immutable.List([2, 4])]); + expect(pseudoSuperset).toBeSupersetImmutable(pseudoSubset); + }).toThrow( + 'Expected List [ 1, List [ 2, 3 ] ] to contain List [ 1, List [ 2, 4 ] ]' + ); + }); + + it('throws when types are different', () => { + expect(() => + expect(Immutable.List([1, 2])).toBeSupersetImmutable(Immutable.Seq([1])) + ).toThrow( + 'Expected List [ 1, 2 ] and Seq [ 1 ] to be of the same type' + ); + }); + + it('does not throw when first List is superset of the other', () => { + expect(Immutable.List([1, 2])).toBeSupersetImmutable(Immutable.List([1])); + }); + + it('does not throw when Lists are equal', () => { + expect(Immutable.List([1])).toBeSupersetImmutable(Immutable.List([1])); + }); + }); + + context('toBeSubsetImmutable', () => { + it('throws when each List has unique values', () => { + expect(() => { + const pseudoSuperset = Immutable.List([1, Immutable.List([2, 3])]); + const pseudoSubset = Immutable.List([1, Immutable.List([2, 4])]); + expect(pseudoSuperset).toBeSubsetImmutable(pseudoSubset); + }).toThrow( + 'Expected List [ 1, List [ 2, 3 ] ] to be contained by List [ 1, List [ 2, 4 ] ]' + ); + }); + + it('throws when types are different', () => { + expect(() => + expect(Immutable.List([1, 2])).toBeSubsetImmutable(Immutable.Seq([1])) + ).toThrow( + 'Expected List [ 1, 2 ] and Seq [ 1 ] to be of the same type' ); }); + + it('does not throw when first List is superset of the other', () => { + expect(Immutable.List([1])).toBeSubsetImmutable(Immutable.List([1, 2])); + }); + + it('does not throw when Lists are equal', () => { + expect(Immutable.List([1])).toBeSubsetImmutable(Immutable.List([1])); + }); }); }); diff --git a/index.js b/index.js index 2912fb6..2fbc166 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ import expect from 'expect'; -import { Iterable } from 'immutable'; +import { Iterable, is } from 'immutable'; function ensureIterable(object) { expect.assert( @@ -9,19 +9,55 @@ function ensureIterable(object) { ); } +function ensureSameType(actual, iterable) { + expect.assert( + actual.toString().split(' ')[0] === iterable.toString().split(' ')[0], + `Expected ${actual} and ${iterable} to be of the same type` + ); +} + const api = { toEqualImmutable(iterable) { ensureIterable(this.actual); ensureIterable(iterable); + ensureSameType(this.actual, iterable); - expect(this.actual.toJS()).toEqual(iterable.toJS()); + expect.assert( + is(this.actual, iterable), + `Expected ${this.actual} to equal ${iterable}` + ); }, toNotEqualImmutable(iterable) { ensureIterable(this.actual); ensureIterable(iterable); - expect(this.actual.toJS()).toNotEqual(iterable.toJS()); + expect.assert( + !is(this.actual, iterable), + `Expected ${this.actual} not to equal ${iterable}` + ); + }, + + toBeSupersetImmutable(iterable) { + ensureIterable(this.actual); + ensureIterable(iterable); + ensureSameType(this.actual, iterable); + + expect.assert( + this.actual.isSuperset(iterable), + `Expected ${this.actual} to contain ${iterable}` + ); + }, + + toBeSubsetImmutable(iterable) { + ensureIterable(this.actual); + ensureIterable(iterable); + ensureSameType(this.actual, iterable); + + expect.assert( + this.actual.isSubset(iterable), + `Expected ${this.actual} to be contained by ${iterable}` + ); } }; diff --git a/package.json b/package.json index 9110f59..a98e468 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "expect-immutable", - "version": "0.0.2", + "version": "0.0.3", "description": "immutable js extension for expect.js", "main": "index-dist.js", "scripts": { @@ -22,6 +22,7 @@ "immutable": "^3.7.5" }, "devDependencies": { + "babel-cli": "^6.18.0", "babel-core": "^6.3.17", "babel-eslint": "^4.1.6", "babel-preset-es2015": "^6.3.13", @@ -29,7 +30,8 @@ "eslint-config-airbnb": "^2.1.1", "eslint-plugin-react": "^3.11.3", "expect": "^1.13.4", - "immutable": "^3.7.6" + "immutable": "^3.7.6", + "mocha": "^3.2.0" }, "dependencies": { "expect": "^1.13.4",