Skip to content

Commit

Permalink
Add Immutable collections support for the keys method
Browse files Browse the repository at this point in the history
  • Loading branch information
astorije committed May 31, 2015
1 parent f4c7ecb commit 2f0e2f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down
17 changes: 14 additions & 3 deletions chai-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 2f0e2f5

Please sign in to comment.