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

feat: add a '.toIncludeSamePartialMembers()' custom matcher #697

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export { toHaveBeenCalledExactlyOnceWith } from './toHaveBeenCalledExactlyOnceWi
export { toInclude } from './toInclude';
export { toIncludeAllMembers } from './toIncludeAllMembers';
export { toIncludeAllPartialMembers } from './toIncludeAllPartialMembers';
export { toIncludeSamePartialMembers } from './toIncludeSamePartialMembers';
export { toIncludeAnyMembers } from './toIncludeAnyMembers';
export { toIncludeMultiple } from './toIncludeMultiple';
export { toIncludeRepeated } from './toIncludeRepeated';
Expand Down
47 changes: 47 additions & 0 deletions src/matchers/toIncludeSamePartialMembers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { containsEntry } from '../utils';

export function toIncludeSamePartialMembers(actual, expected) {
const { printReceived, printExpected, matcherHint } = this.utils;

const pass = predicate(this.equals, actual, expected);

return {
pass,
message: () =>
pass
? matcherHint('.not.toIncludeSamePartialMembers') +
'\n\n' +
'Expected list to not exactly match the partial members of:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`
: matcherHint('.toIncludeSamePartialMembers') +
'\n\n' +
'Expected list to have the following partial members and no more:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`,
};
}

const predicate = (equals, actual, expected) => {
if (!Array.isArray(actual) || !Array.isArray(expected) || actual.length !== expected.length) {
return false;
}

const remaining = expected.reduce((remaining, expectedPartial) => {
if (remaining === null) return remaining;

const index = remaining.findIndex(actualValue =>
Object.entries(expectedPartial).every(entry => containsEntry(equals, actualValue, entry)),
);

if (index === -1) {
return null;
}

return remaining.slice(0, index).concat(remaining.slice(index + 1));
}, actual);

return !!remaining && remaining.length === 0;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toIncludeSamePartialMembers fails when array values matches the members of the set 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).not.toIncludeSamePartialMembers(</intensity><green>expected</color><dim>)</intensity>

Expected list to not exactly match the partial members of:
<green>[{"hello": "world"}, {"foo": "bar"}]</color>
Received:
<red>[{"hello": "world"}, {"baz": "qux", "foo": "bar"}]</color>"
`;

exports[`.toIncludeSamePartialMembers fails when array values contain only some members of the set 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeSamePartialMembers(</intensity><green>expected</color><dim>)</intensity>

Expected list to have the following partial members and no more:
<green>[{"hello": "world"}]</color>
Received:
<red>[{"hello": "world"}, {"baz": "qux", "foo": "bar"}]</color>"
`;

exports[`.toIncludeSamePartialMembers fails when array values do not contain any of the members of the set 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeSamePartialMembers(</intensity><green>expected</color><dim>)</intensity>

Expected list to have the following partial members and no more:
<green>[{"foo": "qux"}]</color>
Received:
<red>[{"hello": "world"}, {"baz": "qux", "foo": "bar"}]</color>"
`;

exports[`.toIncludeSamePartialMembers fails when expected object is not an array 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeSamePartialMembers(</intensity><green>expected</color><dim>)</intensity>

Expected list to have the following partial members and no more:
<green>1</color>
Received:
<red>[{"hello": "world"}, {"baz": "qux", "foo": "bar"}]</color>"
`;

exports[`.toIncludeSamePartialMembers fails when given object is not an array 1`] = `
"<dim>expect(</intensity><red>received</color><dim>).toIncludeSamePartialMembers(</intensity><green>expected</color><dim>)</intensity>

Expected list to have the following partial members and no more:
<green>[{"foo": "bar"}]</color>
Received:
<red>1</color>"
`;
64 changes: 64 additions & 0 deletions test/matchers/toIncludeSamePartialMembers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as matcher from 'src/matchers/toIncludeSamePartialMembers';

expect.extend(matcher);

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

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

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

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

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

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

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

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

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

test('fails when array values matches the members of the set', () => {
expect(() =>
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).not.toIncludeSamePartialMembers([
{ hello: 'world' },
{ foo: 'bar' },
]),
).toThrowErrorMatchingSnapshot();
});
});
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,12 @@ declare namespace jest {
*/
toIncludeAllPartialMembers<E = unknown>(members: readonly E[]): R;

/**
* Use `.toIncludeSamePartialMembers` when checking if an `Array` contains exactly the same partial members as a given set, in any order
* @param {Array.<*>} members
*/
toIncludeSamePartialMembers<E = unknown>(members: readonly E[]): R;

/**
* Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.
* @param {Array.<*>} members
Expand Down
10 changes: 10 additions & 0 deletions website/docs/matchers/Array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Use `.toIncludeAllPartialMembers` when checking if an `Array` contains all of th
});`}
</TestFile>

### .toIncludeSamePartialMembers([members])

Use `.toIncludeSamePartialMembers` when checking if an `Array` contains exactly the same partial members as a given set, in any order

<TestFile name="toIncludeSamePartialMembers">
{`test('passes when given array values match the partial members of the set', () => {
expect([{ foo: 'bar', baz: 'qux' }, { property: 'hello', another: 'world' }]).toIncludeSamePartialMembers([{ foo: 'bar' }, { property: 'hello' }]);
});`}
</TestFile>

### .toIncludeAnyMembers([members])

Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set.
Expand Down
1 change: 1 addition & 0 deletions website/docs/matchers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sidebar_position: 1
- [.toBeArrayOfSize()](/docs/matchers/array/#tobearrayofsize)
- [.toIncludeAllMembers([members])](/docs/matchers/array/#toincludeallmembersmembers)
- [.toIncludeAllPartialMembers([members])](/docs/matchers/array/#toincludeallpartialmembersmembers)
- [.toIncludeSamePartialMembers([members])](/docs/matchers/array/#toincludesamepartialmembers)
- [.toIncludeAnyMembers([members])](/docs/matchers/array/#toincludeanymembersmembers)
- [.toIncludeSameMembers([members])](/docs/matchers/array/#toincludesamemembersmembers)
- [.toPartiallyContain(member)](/docs/matchers/array/#topartiallycontainmember)
Expand Down