-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add matcher for Array.toSatisfyAny (#227)
- Loading branch information
1 parent
4c14fba
commit ee70268
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/matchers/toSatisfyAny/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toSatisfyAll fails when any value satisfies predicate 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toSatisfyAny(</><green>expected</><dim>)</> | ||
Expected array to not satisfy predicate for any value: | ||
<green>[Function isOdd]</> | ||
Received: | ||
<red>[2, 3, 6, 8]</>" | ||
`; | ||
exports[`.toSatisfyAny fails when no value satisfies the predicate 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toSatisfyAny(</><green>expected</><dim>)</> | ||
Expected array to satisfy predicate for any values: | ||
<green>[Function isOdd]</> | ||
Received: | ||
<red>[2, 4, 6, 8]</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; | ||
|
||
import predicate from './predicate'; | ||
|
||
const passMessage = (actual, expected) => () => | ||
matcherHint('.not.toSatisfyAny') + | ||
'\n\n' + | ||
'Expected array to not satisfy predicate for any value:\n' + | ||
` ${printExpected(expected)}\n` + | ||
'Received:\n' + | ||
` ${printReceived(actual)}`; | ||
|
||
const failMessage = (actual, expected) => () => | ||
matcherHint('.toSatisfyAny') + | ||
'\n\n' + | ||
'Expected array to satisfy predicate for any values:\n' + | ||
` ${printExpected(expected)}\n` + | ||
'Received:\n' + | ||
` ${printReceived(actual)}`; | ||
|
||
export default { | ||
toSatisfyAny: (actual, expected) => { | ||
const pass = predicate(actual, expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(actual, expected) }; | ||
} | ||
|
||
return { pass: false, message: failMessage(actual, expected) }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
let isEven = el => el % 2 === 0; | ||
let isOdd = el => el % 2 === 1; | ||
|
||
describe('.toSatisfyAny', () => { | ||
test('passes when any values satisfy predicate', () => { | ||
expect([2, 3, 6, 8]).toSatisfyAny(isOdd); | ||
expect([1, 4, 7, 9]).toSatisfyAny(isEven); | ||
expect([11]).toSatisfyAny(isOdd); | ||
expect([10]).toSatisfyAny(isEven); | ||
}); | ||
|
||
test('fails when no value satisfies the predicate', () => { | ||
expect(() => expect([2, 4, 6, 8]).toSatisfyAny(isOdd)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toSatisfyAll', () => { | ||
test('passes when all values does not satisfy the predicate', () => { | ||
expect([2, 4, 6, 8]).not.toSatisfyAny(isOdd); | ||
expect([1, 3, 5, 7]).not.toSatisfyAny(isEven); | ||
}); | ||
|
||
test('fails when any value satisfies predicate', () => { | ||
expect(() => expect([2, 3, 6, 8]).not.toSatisfyAny(isOdd)).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default (array, predicate) => array.some(predicate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import predicate from './predicate'; | ||
|
||
describe('toSatisfyAny', () => { | ||
let isOdd = el => el % 2 === 1; | ||
|
||
describe('returns true', () => { | ||
test('when any elements satisfy', () => { | ||
expect(predicate([2, 3, 6, 8], isOdd)).toBe(true); | ||
}); | ||
|
||
test('works for repeated elements', () => { | ||
expect(predicate([2, 3, 3, 8], isOdd)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
test('when all elements fail', () => { | ||
expect(predicate([10, 2, 4, 6], isOdd)).toBe(false); | ||
}); | ||
|
||
test('works for repeated elements', () => { | ||
expect(predicate([2, 4, 4, 8, 10], isOdd)).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters