Skip to content
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 @@ -53,6 +53,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toIncludeAllPartialMembers([members])](#toincludeallpartialmembersmembers)
- [.toIncludeAnyMembers([members])](#toincludeanymembersmembers)
- [.toIncludeSameMembers([members])](#toincludesamemembersmembers)
- [.toPartiallyContain(member)](#topartiallycontain)
- [.toSatisfyAll(predicate)](#tosatisfyallpredicate)
- [.toSatisfyAny(predicate)](#tosatisfyanypredicate)
- [Boolean](#boolean)
Expand Down Expand Up @@ -352,6 +353,18 @@ test('passes when arrays match in a different order', () => {
});
```

#### .toPartiallyContain(member)

Use `.toPartiallyContain` when checking if any array value matches the partial member.

```js
test('passes when a string has a given substring', () => {
expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).toPartiallyContain({ foo: 'bar' });
expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).toPartiallyContain({ baz: 'qux' });
expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).not.toPartiallyContain({ foo: 'qux' });
});
```

#### .toSatisfyAll(predicate)

Use `.toSatisfyAll` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean` for all values in an array.
Expand Down
1 change: 1 addition & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ export { toSatisfyAny } from './toSatisfyAny';
export { toStartWith } from './toStartWith';
export { toThrowWithMessage } from './toThrowWithMessage';
export { toEqualIgnoringWhitespace } from './toEqualIgnoringWhitespace';
export { toPartiallyContain } from './toPartiallyContain';
19 changes: 19 additions & 0 deletions src/matchers/toPartiallyContain/__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[`.toPartiallyContain .not.toPartiallyContain fails when a string does have a given substring 1`] = `
"<dim>expect(</><red>received</><dim>).not.toPartiallyContain(</><green>expected</><dim>)</>

Expected array not to partially contain:
<green>{\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}</>
Received:
<red>[{\\"bax\\": \\"zax\\", \\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>"
`;

exports[`.toPartiallyContain fails when a string does not have a given substring 1`] = `
"<dim>expect(</><red>received</><dim>).toPartiallyContain(</><green>expected</><dim>)</>

Expected array to partially contain:
<green>{\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}</>
Received:
<red>[{\\"a\\": 1, \\"b\\": 2}]</>"
`;
28 changes: 28 additions & 0 deletions src/matchers/toPartiallyContain/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (actual, expected) => () =>
matcherHint('.not.toPartiallyContain') +
'\n\n' +
'Expected array not to partially contain:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const failMessage = (actual, expected) => () =>
matcherHint('.toPartiallyContain') +
'\n\n' +
'Expected array to partially contain:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

export function toPartiallyContain(actual, expected) {
const pass = predicate(actual, expected);
if (pass) {
return { pass: true, message: passMessage(actual, expected) };
}

return { pass: false, message: failMessage(actual, expected) };
}
27 changes: 27 additions & 0 deletions src/matchers/toPartiallyContain/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as matcher from './';

expect.extend(matcher);

describe('.toPartiallyContain', () => {
const item = { foo: 'bar', baz: 'qux' };

test('passes when a string has a given substring', () => {
expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).toPartiallyContain(item);
});

test('fails when a string does not have a given substring', () => {
expect(() => expect([{ a: 1, b: 2 }]).toPartiallyContain(item)).toThrowErrorMatchingSnapshot();
});

describe('.not.toPartiallyContain', () => {
test('passes when a string does not have a given substring', () => {
expect([{ a: 1, b: 2 }]).not.toPartiallyContain(item);
});

test('fails when a string does have a given substring', () => {
expect(() =>
expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).not.toPartiallyContain(item),
).toThrowErrorMatchingSnapshot();
});
});
});
3 changes: 3 additions & 0 deletions src/matchers/toPartiallyContain/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import toIncludeAllPartialMembers from '../toIncludeAllPartialMembers/predicate';

export default (array, item) => toIncludeAllPartialMembers(array, [item]);
13 changes: 13 additions & 0 deletions src/matchers/toPartiallyContain/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import predicate from './predicate';

describe('.toPartiallyContain', () => {
const item = { foo: 'bar', baz: 'qux' };

test('passes when array partially contains the given item', () => {
expect(predicate([{ foo: 'bar', baz: 'qux', bax: 'zax' }], item)).toBe(true);
});

test('fails when array does not contain the given item', () => {
expect(predicate([{ a: 1, b: 2 }], item)).toBe(false);
});
});