Skip to content

Commit

Permalink
Add object contains property string
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Apr 9, 2016
1 parent b0d0003 commit b8fed4b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ expect([ 1, 2, 3 ]).toExclude(4)
Asserts the given `object` contains all keys and values in `value`, recursively. The `comparator` function, if given, should compare two objects and either `return false` or `throw` if they are not equal. It defaults to `assert.deepEqual`.

```js
expect({ a: 1, b: 2 }).toInclude('a')
expect({ a: 1, b: 2 }).toInclude({ b: 2 })
expect({ a: 1, b: 2, c: { d: 3 } }).toInclude({ b: 2, c: { d: 3 } })
```
Expand Down
6 changes: 6 additions & 0 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,17 @@ export const arrayContains = (array, value, compareValues) => {
* Returns true if the given object contains the value, false
* otherwise. The compareValues function must return false to
* indicate a non-match.
*
* If the value is a string returns true if a value exists on
* the object for the string property.
*/
export const objectContains = (object, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

if (typeof value === 'string')
return object.hasOwnProperty(value)

return Object.keys(value).every(k => {
if (isObject(object[k])) {
return objectContains(object[k], value[k], compareValues)
Expand Down
12 changes: 12 additions & 0 deletions modules/__tests__/toInclude-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('toInclude', () => {
}).toNotThrow()
})

it('does not throw when an object contains an expected key', () => {
expect(() => {
expect({ a: 1 }).toInclude('a')
}).toNotThrow()
})

it('throws when an object contains an unexpected key', () => {
expect(() => {
expect({ a: 1 }).toInclude('b')
}).toThrow(/to include/)
})

it('does not throw when an object contains an expected object', () => {
expect(() => {
expect({ a: 1, b: 2, c: 3 }).toInclude({ b: 2 })
Expand Down

0 comments on commit b8fed4b

Please sign in to comment.