Skip to content

Commit

Permalink
Implement toIncludeAllPartialMembers (jest-community#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibarsi authored Oct 11, 2021
1 parent adb82b8 commit 32afe6f
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeArray()](#tobearray)
- [.toBeArrayOfSize()](#tobearrayofsize)
- [.toIncludeAllMembers([members])](#toincludeallmembersmembers)
- [.toIncludeAllPartialMembers([members])](#toincludeallpartialmembersmembers)
- [.toIncludeAnyMembers([members])](#toincludeanymembersmembers)
- [.toIncludeSameMembers([members])](#toincludesamemembersmembers)
- [.toSatisfyAll(predicate)](#tosatisfyallpredicate)
Expand Down Expand Up @@ -295,6 +296,18 @@ test('passes when given array values match the members of the set', () => {
});
```

#### .toIncludeAllPartialMembers([members])

Use `.toIncludeAllPartialMembers` when checking if an `Array` contains all of the same partial members of a given set.

```js
test('passes when given array values match the partial members of the set', () => {
expect([{ foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([
{ foo: 'bar' },
]);
});
```

#### .toIncludeAnyMembers([members])

Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.
Expand Down
2 changes: 2 additions & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import toHaveBeenCalledAfterMatcher from './toHaveBeenCalledAfter';
import toHaveBeenCalledBeforeMatcher from './toHaveBeenCalledBefore';
import toIncludeMatcher from './toInclude';
import toIncludeAllMembersMatcher from './toIncludeAllMembers';
import toIncludeAllPartialMembersMatcher from './toIncludeAllPartialMembers';
import toIncludeAnyMembersMatcher from './toIncludeAnyMembers';
import toIncludeMultipleMatcher from './toIncludeMultiple';
import toIncludeRepeatedMatcher from './toIncludeRepeated';
Expand Down Expand Up @@ -114,6 +115,7 @@ export const toHaveBeenCalledAfter = toHaveBeenCalledAfterMatcher.toHaveBeenCall
export const toHaveBeenCalledBefore = toHaveBeenCalledBeforeMatcher.toHaveBeenCalledBefore;
export const toInclude = toIncludeMatcher.toInclude;
export const toIncludeAllMembers = toIncludeAllMembersMatcher.toIncludeAllMembers;
export const toIncludeAllPartialMembers = toIncludeAllPartialMembersMatcher.toIncludeAllPartialMembers;
export const toIncludeAnyMembers = toIncludeAnyMembersMatcher.toIncludeAnyMembers;
export const toIncludeMultiple = toIncludeMultipleMatcher.toIncludeMultiple;
export const toIncludeRepeated = toIncludeRepeatedMatcher.toIncludeRepeated;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toIncludeAllPartialMembers fails when array values matches the members of the set 1`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAllPartialMembers(</><green>expected</><dim>)</>
Expected list to not have all of the following partial members:
<green>[{\\"foo\\": \\"bar\\"}]</>
Received:
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>"
`;
exports[`.toIncludeAllPartialMembers fails when array values do not contain any of the members of the set 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</>
Expected list to have all of the following partial members:
<green>[{\\"foo\\": \\"qux\\"}]</>
Received:
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>"
`;
exports[`.toIncludeAllPartialMembers fails when expected object is not an array 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</>
Expected list to have all of the following partial members:
<green>1</>
Received:
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>"
`;
exports[`.toIncludeAllPartialMembers fails when given object is not an array 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</>
Expected list to have all of the following partial members:
<green>[{\\"foo\\": \\"bar\\"}]</>
Received:
<red>1</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toIncludeAllPartialMembers/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.toIncludeAllPartialMembers') +
'\n\n' +
'Expected list to not have all of the following partial members:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const failMessage = (actual, expected) => () =>
matcherHint('.toIncludeAllPartialMembers') +
'\n\n' +
'Expected list to have all of the following partial members:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

export default {
toIncludeAllPartialMembers: (actual, expected) => {
const pass = predicate(actual, expected);
if (pass) {
return { pass: true, message: passMessage(actual, expected) };
}

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

expect.extend(matcher);

describe('.toIncludeAllPartialMembers', () => {
test('passes when array values matches the partial members of the set', () => {
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'bar' }]);
});

test('fails when array values do not contain any of the members of the set', () => {
expect(() =>
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'qux' }]),
).toThrowErrorMatchingSnapshot();
});

test('fails when given object is not an array', () => {
expect(() => expect(1).toIncludeAllPartialMembers([{ foo: 'bar' }])).toThrowErrorMatchingSnapshot();
});

test('fails when expected object is not an array', () => {
expect(() =>
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers(1),
).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toIncludeAllPartialMembers', () => {
test('passes when array values does not contain any members of the set', () => {
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).not.toIncludeAllPartialMembers([{ foo: 'qux' }]);
});

test('passes when given object is not an array', () => {
expect(1).not.toIncludeAllPartialMembers([{ foo: 'bar' }]);
});

test('fails when array values matches the members of the set', () => {
expect(() =>
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).not.toIncludeAllPartialMembers([{ foo: 'bar' }]),
).toThrowErrorMatchingSnapshot();
});
});
6 changes: 6 additions & 0 deletions src/matchers/toIncludeAllPartialMembers/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import toContainAnyEntries from '../toContainAnyEntries/predicate';

export default (array, set) =>
Array.isArray(array) &&
Array.isArray(set) &&
set.every(partial => array.some(value => toContainAnyEntries(value, Object.entries(partial))));
15 changes: 15 additions & 0 deletions src/matchers/toIncludeAllPartialMembers/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import predicate from './predicate';

describe('toIncludeAllPartialMembers Predicate', () => {
describe('returns true', () => {
test('when Array contains all of the same nested partial members of given set', () => {
expect(predicate([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'bar' }])).toBe(true);
});
});

describe('returns false', () => {
test('when Array contains does not contain all of the same nested partial members of given set', () => {
expect(predicate([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'qux' }])).toBe(false);
});
});
});
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ declare namespace jest {
*/
toIncludeAllMembers<E = any>(members: E[]): R;

/**
* Use `.toIncludeAllPartialMembers` when checking if an `Array` contains all of the same partial members of a given set.
* @param {Array.<*>} members
*/
toIncludeAllMembers(members: any[]): R;

/**
* Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.
* @param {Array.<*>} members
Expand Down

0 comments on commit 32afe6f

Please sign in to comment.