Skip to content

Commit

Permalink
Add matcher for Array.toSatisfyAny (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
blockscoped authored Oct 6, 2021
1 parent 4c14fba commit ee70268
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toIncludeAnyMembers([members])](#toincludeanymembersmembers)
- [.toIncludeSameMembers([members])](#toincludesamemembersmembers)
- [.toSatisfyAll(predicate)](#tosatisfyallpredicate)
- [.toSatisfyAny(predicate)](#tosatisfyanypredicate)
- [Boolean](#boolean)
- [.toBeBoolean()](#tobeboolean)
- [.toBeTrue()](#tobetrue)
Expand Down Expand Up @@ -307,6 +308,18 @@ test('passes when all values in array pass given predicate', () => {
});
```

#### .toSatisfyAny(predicate)

Use `.toSatisfyAny` when you want to use a custom matcher by supplying a predicate function that returns `true` for any matching value in an array.

```js
test('passes when any value in array pass given predicate', () => {
const isOdd = el => el % 2 === 1;
expect([2,3,6,8]).toSatisfyAny(isOdd);
expect([2,4,8,12]).not.toSatisfyAny(isOdd);
});
```

### Boolean

#### .toBeBoolean()
Expand Down
19 changes: 19 additions & 0 deletions src/matchers/toSatisfyAny/__snapshots__/index.test.js.snap
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]</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toSatisfyAny/index.js
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) };
},
};
30 changes: 30 additions & 0 deletions src/matchers/toSatisfyAny/index.test.js
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();
});
});
1 change: 1 addition & 0 deletions src/matchers/toSatisfyAny/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (array, predicate) => array.some(predicate);
25 changes: 25 additions & 0 deletions src/matchers/toSatisfyAny/predicate.test.js
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);
});
});
});
12 changes: 12 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ declare namespace jest {
*/
toSatisfyAll<E = any>(predicate: (x: E) => boolean): R;

/**
* Use `.toSatisfyAny` when you want to use a custom matcher by supplying a predicate function that returns `true` for any matching value in an array.
* @param {Function} predicate
*/
toSatisfyAny(predicate: (x: any) => boolean): R;

/**
* Use `.toBeBoolean` when checking if a value is a `Boolean`.
*/
Expand Down Expand Up @@ -456,6 +462,12 @@ declare namespace jest {
*/
toSatisfyAll<E = any>(predicate: (x: E) => boolean): any;

/**
* Use `.toSatisfyAny` when you want to use a custom matcher by supplying a predicate function that returns `true` for any matching value in an array.
* @param {Function} predicate
*/
toSatisfyAny(predicate: (x: any) => boolean): any;

/**
* Use `.toBeBoolean` when checking if a value is a `Boolean`.
*/
Expand Down

0 comments on commit ee70268

Please sign in to comment.