diff --git a/README.md b/README.md index ecf12a3..b940b5d 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ expect(a).to.equal(b); ### .keys(key1[, key2, ...[, keyN]]) -- **@param** *{ String... | Array | Object }* key*N* +- **@param** *{ String... | Array | Object | Collection }* key*N* Asserts that the keyed collection contains any or all of the passed-in keys. Use in combination with `any`, `all`, `contains`, or `have` will @@ -76,8 +76,12 @@ have all and only all of the passed-in keys). ```js expect(new Map({ foo: 1 })).to.have.key('foo'); expect(new Map({ foo: 1, bar: 2 })).to.have.keys('foo', 'bar'); +expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new List(['bar', 'foo'])); +expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Set(['bar', 'foo'])); +expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Stack(['bar', 'foo'])); expect(new Map({ foo: 1, bar: 2 })).to.have.keys(['bar', 'foo']); expect(new Map({ foo: 1, bar: 2 })).to.have.keys({ 'bar': 6, 'foo': 7 }); +expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Map({ 'bar': 6, 'foo': 7 })); expect(new Map({ foo: 1, bar: 2 })).to.have.any.keys('foo', 'not-foo'); expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys('foo', 'bar'); expect(new Map({ foo: 1, bar: 2 })).to.contain.key('foo'); diff --git a/chai-immutable.js b/chai-immutable.js index 9a0ef9e..89ada1a 100644 --- a/chai-immutable.js +++ b/chai-immutable.js @@ -2,7 +2,9 @@ var Immutable = require('immutable'); var Collection = Immutable.Collection; +var IndexedCollection = Immutable.Collection.Indexed; var KeyedCollection = Immutable.Collection.Keyed; +var SetCollection = Immutable.Collection.Set; module.exports = function (chai, utils) { var Assertion = chai.Assertion; @@ -111,15 +113,19 @@ module.exports = function (chai, utils) { * ```js * expect(new Map({ foo: 1 })).to.have.key('foo'); * expect(new Map({ foo: 1, bar: 2 })).to.have.keys('foo', 'bar'); + * expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new List(['bar', 'foo'])); + * expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Set(['bar', 'foo'])); + * expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Stack(['bar', 'foo'])); * expect(new Map({ foo: 1, bar: 2 })).to.have.keys(['bar', 'foo']); * expect(new Map({ foo: 1, bar: 2 })).to.have.keys({ 'bar': 6, 'foo': 7 }); + * expect(new Map({ foo: 1, bar: 2 })).to.have.keys(new Map({ 'bar': 6, 'foo': 7 })); * expect(new Map({ foo: 1, bar: 2 })).to.have.any.keys('foo', 'not-foo'); * expect(new Map({ foo: 1, bar: 2 })).to.have.all.keys('foo', 'bar'); * expect(new Map({ foo: 1, bar: 2 })).to.contain.key('foo'); * ``` * * @name keys - * @param {String...|Array|Object} keyN + * @param {String...|Array|Object|Collection} keyN * @alias key * @api public */ @@ -133,10 +139,15 @@ module.exports = function (chai, utils) { if (obj && obj instanceof KeyedCollection) { switch (utils.type(keys)) { case 'object': - keys = Object.keys(keys); + if (keys instanceof IndexedCollection || keys instanceof SetCollection) { + keys = keys.toJS(); + } + else if (keys instanceof KeyedCollection) keys = keys.keySeq().toJS(); + else keys = Object.keys(keys); case 'array': if (arguments.length > 1) throw new Error( - 'keys must be given single argument of Array|Object|String, ' + + 'keys must be given single argument of ' + + 'Array|Object|String|Collection, ' + 'or multiple String arguments' ); break; diff --git a/test/test.js b/test/test.js index 5ebf4e0..635d422 100644 --- a/test/test.js +++ b/test/test.js @@ -7,6 +7,8 @@ var expect = chai.expect; var Immutable = require('immutable'); var List = Immutable.List; var Map = Immutable.Map; +var Set = Immutable.Set; +var Stack = Immutable.Stack; chai.use(chaiImmutable); @@ -88,11 +90,27 @@ describe('chai-immutable', function () { expect(objectFoobar).to.have.keys(['bar', 'foo']); }); + it('should accept an List of keys to check against', function () { + expect(mapFoobar).to.have.keys(new List(['bar', 'foo'])); + }); + + it('should accept an Set of keys to check against', function () { + expect(mapFoobar).to.have.keys(new Set(['bar', 'foo'])); + }); + + it('should accept an Stack of keys to check against', function () { + expect(mapFoobar).to.have.keys(new Stack(['bar', 'foo'])); + }); + it('should accept an Object to check against', function () { expect(mapFoobar).to.have.keys({ 'bar': 6, 'foo': 7 }); expect(objectFoobar).to.have.keys({ 'bar': 6, 'foo': 7 }); }); + it('should accept a Map to check against', function () { + expect(mapFoobar).to.have.keys(new Map({ 'bar': 6, 'foo': 7 })); + }); + it('should be true when used with any and an existing key', function () { expect(mapFoobar).to.have.any.keys('foo', 'not-foo'); expect(objectFoobar).to.have.any.keys('foo', 'not-foo');