Skip to content

Commit

Permalink
modify toInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
wuct committed Apr 5, 2016
1 parent 7284416 commit f78fada
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
12 changes: 1 addition & 11 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export const isArray = (object) =>
export const isObject = (object) =>
object && !isArray(object) && typeof object === 'object'

/**
* Returns true if the given object is an empty object.
*/
export const isEmptyObject = (object) =>
isObject(object)
&& ownKeys(object).length === 0

/**
* Returns true if the given object is an instanceof value
* or its typeof is the given value.
Expand Down Expand Up @@ -95,11 +88,8 @@ export const objectContains = (object, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

if (isEmptyObject(value))
return true

return ownKeys(value).every(k => {
if (isObject(object[k]) && !isEmptyObject(value[k])) {
if (isObject(object[k]) && isObject(value[k])) {
return objectContains(object[k], value[k], compareValues)
}

Expand Down
25 changes: 21 additions & 4 deletions modules/__tests__/toInclude-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ describe('toInclude', () => {
expect(() => {
expect({ a: 1, b: 2, c: 3 }).toInclude({ b: 4 })
}).toThrow(/to include/)

expect(() => {
expect({ a: { b: 2 } }).toInclude({ a: {} })
}).toThrow(/to include/)
})

it('does not throw when an object contains an expected object in a deep key', () => {
Expand All @@ -64,6 +60,27 @@ describe('toInclude', () => {
}).toThrow(/to include/)
})

it('compares partial object only when both expected and actual properties are object', () => {
expect(() => {
expect({ a: { b: 2 } }).toInclude({ a: {} })
}).toNotThrow()

expect(() => {
expect({ a: 1 }).toInclude({ a: {} })
}).toThrow(/to include/)

expect(() => {
expect({ a: [] }).toInclude({ a: {} })
}).toThrow(/to include/)

expect(() => {
expect({ a: '1' }).toInclude({ a: {} })
}).toThrow(/to include/)

expect(() => {
expect({ a: () => {} }).toInclude({ a: {} })
}).toThrow(/to include/)
})

if (typeof Object.create === 'function') {
it('ignores nonenumerable properties of an expected object', () => {
Expand Down

0 comments on commit f78fada

Please sign in to comment.