Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object includes a string key and exclusion test for objects #86

Merged
merged 5 commits into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ expect({ a: 1, b: 2 }).toInclude({ b: 2 })
expect({ a: 1, b: 2, c: { d: 3 } }).toInclude({ b: 2, c: { d: 3 } })
```

### (object) toExclude

> `expect(object).toExclude(value, [comparator], [message])`<br>
> `expect(object).toNotContain(value, [comparator], [message])`

Asserts the given `object` does not contain 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 }).toExclude({ c: 2 })
expect({ a: 1, b: 2 }).toExclude({ b: 3 })
expect({ a: 1, b: 2, c: { d: 3 } }).toExclude({ c: { d: 4 } })
```

### (string) toInclude

> `expect(string).toInclude(value, [message])`<br>
Expand Down
91 changes: 44 additions & 47 deletions modules/Expectation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import assert from './assert'
import { isSpy } from './SpyUtils'
import {
functionThrows,
arrayContains,
stringContains,
objectContains,
isFunction,
isArray,
isObject,
isFunction,
isA
isA,
arrayContains,
objectContains,
stringContains
} from './TestUtils'

/**
Expand Down Expand Up @@ -294,73 +294,70 @@ class Expectation {
}

toInclude(value, compareValues, message) {
assert(
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toInclude() must be an array, object, or a string'
)

if (typeof compareValues === 'string') {
message = compareValues
compareValues = null
}

message = message || 'Expected %s to include %s'
if (compareValues == null)
compareValues = isEqual

assert(
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toInclude() must be an array, object, or a string'
)

let contains = false

if (isArray(this.actual)) {
assert(
arrayContains(this.actual, value, compareValues),
message,
this.actual,
value
)
contains = arrayContains(this.actual, value, compareValues)
} else if (isObject(this.actual)) {
assert(
objectContains(this.actual, value, compareValues),
message,
this.actual,
value
)
contains = objectContains(this.actual, value, compareValues)
} else {
assert(
stringContains(this.actual, value),
message,
this.actual,
value
)
contains = stringContains(this.actual, value)
}

assert(
contains,
message || 'Expected %s to include %s',
this.actual,
value
)

return this
}

toExclude(value, compareValues, message) {
assert(
isArray(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toExclude() must be an array or a string'
)

if (typeof compareValues === 'string') {
message = compareValues
compareValues = null
}

message = message || 'Expected %s to exclude %s'
if (compareValues == null)
compareValues = isEqual

assert(
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string',
'The "actual" argument in expect(actual).toExclude() must be an array, object, or a string'
)

let contains = false

if (isArray(this.actual)) {
assert(
!arrayContains(this.actual, value, compareValues),
message,
this.actual,
value
)
contains = arrayContains(this.actual, value, compareValues)
} else if (isObject(this.actual)) {
contains = objectContains(this.actual, value, compareValues)
} else {
assert(
!stringContains(this.actual, value),
message,
this.actual,
value
)
contains = stringContains(this.actual, value)
}

assert(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Well done. This is some de-duplication I can live with!

!contains,
message || 'Expected %s to exclude %s',
this.actual,
value
)

return this
}

Expand Down
17 changes: 4 additions & 13 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import isEqual from 'is-equal'
import isRegExp from 'is-regex'

/**
Expand Down Expand Up @@ -71,30 +70,22 @@ export const functionThrows = (fn, context, args, value) => {
* otherwise. The compareValues function must return false to
* indicate a non-match.
*/
export const arrayContains = (array, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

return array.some(item => compareValues(item, value) !== false)
}
export const arrayContains = (array, value, compareValues) =>
array.some(item => compareValues(item, value) !== false)

/**
* Returns true if the given object contains the value, false
* otherwise. The compareValues function must return false to
* indicate a non-match.
*/
export const objectContains = (object, value, compareValues) => {
if (compareValues == null)
compareValues = isEqual

return Object.keys(value).every(k => {
export const objectContains = (object, value, compareValues) =>
Object.keys(value).every(k => {
if (isObject(object[k])) {
return objectContains(object[k], value[k], compareValues)
}

return compareValues(object[k], value[k])
})
}

/**
* Returns true if the given string contains the value, false otherwise.
Expand Down
20 changes: 19 additions & 1 deletion modules/__tests__/toExclude-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('toExclude', () => {
it('requires the actual value to be an array or string', () => {
expect(() => {
expect(1).toExclude(2)
}).toThrow(/must be an array or a string/)
}).toThrow(/must be an array, object, or a string/)
})

it('does not throw when an array does not contain the expected value', () => {
Expand All @@ -19,6 +19,24 @@ describe('toExclude', () => {
}).toThrow(/to exclude/)
})

it('throws when an object contains an expected object', () => {
expect(() => {
expect({ a: 1 }).toExclude({ a: 1 })
}).toThrow(/to exclude/)
})

it('does not throw when an array contains an unexpected object', () => {
expect(() => {
expect({ a: 1 }).toExclude({ b: 2 })
}).toNotThrow()
})

it('does not throw when an object contains an expected object with an unexpected value', () => {
expect(() => {
expect({ a: 1 }).toExclude({ a: 2 })
}).toNotThrow()
})

it('does not throw when an array does not contain the expected value', () => {
expect(() => {
expect('hello world').toExclude('goodbye')
Expand Down