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

Add toIncludeSameMembers matcher #122

Merged
merged 3 commits into from
Jul 30, 2018
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@
"doc",
"test"
]
},
{
"login": "denizdogan",
"name": "Deniz Dogan",
"avatar_url": "https://avatars0.githubusercontent.com/u/50928?v=4",
"profile": "https://github.com/denizdogan",
"contributions": [
"code"
]
}
]
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ test('passes when given array values match any of the members in the set', () =>
});
```

#### .toMatchArray([members])

Use `.toMatchArray` when checking if two arrays contain equal values, in any order.

```js
test('passes when arrays match in a different order', () => {
expect([1, 2, 3]).toMatchArray([3, 1, 2]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toMatchArray([{ baz: 'qux' }, { foo: 'bar' }]);
});
```


#### .toSatisfyAll(predicate)

Expand Down Expand Up @@ -836,7 +847,7 @@ test('passes when value includes all substrings', () => {
| [<img src="https://avatars2.githubusercontent.com/u/33098064?v=4" width="100px;"/><br /><sub>mjmiles</sub>](https://github.com/mjmiles)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/13333582?v=4" width="100px;"/><br /><sub>Gary Meehan</sub>](https://github.com/garmeeh)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [<img src="https://avatars2.githubusercontent.com/u/3191489?v=4" width="100px;"/><br /><sub>Fredrik Mäkilä</sub>](https://github.com/GitHug)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [<img src="https://avatars2.githubusercontent.com/u/9046616?v=4" width="100px;"/><br /><sub>Daniel Reinoso</sub>](http://kloc.io/)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/4359781?v=4" width="100px;"/><br /><sub>Chris Hut</sub>](https://github.com/tophernuts)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [<img src="https://avatars2.githubusercontent.com/u/1513183?v=4" width="100px;"/><br /><sub>Kelvin Ly</sub>](https://github.com/cactorium)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [<img src="https://avatars0.githubusercontent.com/u/11182826?v=4" width="100px;"/><br /><sub>Francis Ngo</sub>](https://github.com/francisngo)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/10330923?v=4" width="100px;"/><br /><sub>Amish Shah</sub>](https://hydrabolt.me/)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [<img src="https://avatars3.githubusercontent.com/u/2045206?v=4" width="100px;"/><br /><sub>Dave Cooper</sub>](http://davecooper.org)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3630495?v=4" width="100px;"/><br /><sub>Swann Polydor</sub>](https://github.com/soueuls)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2027003?v=4" width="100px;"/><br /><sub>vikneshwar</sub>](https://github.com/vikneshwar)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1243921?v=4" width="100px;"/><br /><sub>Budi Irawan</sub>](http://budiirawan.com)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [<img src="https://avatars2.githubusercontent.com/u/980783?v=4" width="100px;"/><br /><sub>Tejas Bubane</sub>](http://foss-geek.blogspot.com/)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [📖](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/13134653?v=4" width="100px;"/><br /><sub>Subinoy Ghosh</sub>](https://github.com/subinoy7)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") |
| [<img src="https://avatars1.githubusercontent.com/u/1404810?v=4" width="100px;"/><br /><sub>Simen Bekkhus</sub>](https://github.com/SimenB)<br />[📖](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/49038?v=4" width="100px;"/><br /><sub>Orta</sub>](http://orta.io)<br />[📖](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/17221813?v=4" width="100px;"/><br /><sub>Tom</sub>](https://jsdevtom.com)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [💡](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [<img src="https://avatars0.githubusercontent.com/u/15064535?v=4" width="100px;"/><br /><sub>Lucian Buzzo</sub>](https://github.com/LucianBuzzo)<br /> | [<img src="https://avatars3.githubusercontent.com/u/2997844?v=4" width="100px;"/><br /><sub>Thiago Delgado Pinto</sub>](https://github.com/thiagodp)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [💡](#example-thiagodp "Examples") [🤔](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [<img src="https://avatars0.githubusercontent.com/u/3042904?v=4" width="100px;"/><br /><sub>Ragnar Laud</sub>](https://github.com/xprn)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3047126?v=4" width="100px;"/><br /><sub>Luiz Américo</sub>](https://github.com/blikblum)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [🤔](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/868844?v=4" width="100px;"/><br /><sub>Gilles De Mey</sub>](https://demey.io)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") |
| [<img src="https://avatars0.githubusercontent.com/u/615334?v=4" width="100px;"/><br /><sub>Frederick Fogerty</sub>](https://github.com/frederickfogerty)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [🤔](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/10714808?v=4" width="100px;"/><br /><sub>Benjamin Kay</sub>](https://github.com/benjaminkay93)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/868844?v=4" width="100px;"/><br /><sub>Gilles De Mey</sub>](https://demey.io)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [📖](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [<img src="https://avatars0.githubusercontent.com/u/50928?v=4" width="100px;"/><br /><sub>Deniz Dogan</sub>](https://github.com/denizdogan)<br />[💻](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

## LICENSE
Expand Down
10 changes: 10 additions & 0 deletions src/matchers/toMatchArray/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.toMatchArray fails when the arrays are not equal in length 1`] = `
"<dim>expect(</><red>received</><dim>).toMatchArray(</><green>expected</><dim>)</>

Expected list to have the following members and no more:
<green>[1]</>
Received:
<red>[1, 2]</>"
`;
29 changes: 29 additions & 0 deletions src/matchers/toMatchArray/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (actual, expected) => () =>
matcherHint('.not.toMatchArray') +
'\n\n' +
'Expected list to not exactly match the members of:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

const failMessage = (actual, expected) => () =>
matcherHint('.toMatchArray') +
'\n\n' +
'Expected list to have the following members and no more:\n' +
` ${printExpected(expected)}\n` +
'Received:\n' +
` ${printReceived(actual)}`;

export default {
toMatchArray: (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/toMatchArray/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import matcher from './';

expect.extend(matcher);

describe('.toMatchArray', () => {
test('passes when arrays are empty', () => {
expect([]).toMatchArray([]);
});

test('passes when arrays match', () => {
expect([1, 2, 3]).toMatchArray([1, 2, 3]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toMatchArray([{ foo: 'bar' }, { baz: 'qux' }]);
});

test('passes when arrays match in a different order', () => {
expect([1, 2, 3]).toMatchArray([3, 1, 2]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toMatchArray([{ baz: 'qux' }, { foo: 'bar' }]);
});

test('fails when the arrays are not equal in length', () => {
expect(() => expect([1, 2]).toMatchArray([1])).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toMatchArray', () => {
test('passes when the arrays are different in length', () => {
expect([1, 2, 3]).not.toMatchArray([1, 2]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).not.toMatchArray([{ hello: 'world' }]);
});

test('passes when given object is not an array', () => {
expect(4).not.toMatchArray([4, 5, 6]);
});

test('passes when arrays do not match', () => {
expect([]).not.toMatchArray([1]);
expect([1]).not.toMatchArray([1, 1]);
expect([1, 2]).not.toMatchArray([1, 2, 2]);
expect([1, 2, 3]).not.toMatchArray([2, 3, 4]);
});
});
36 changes: 36 additions & 0 deletions src/matchers/toMatchArray/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { equals } from '../../utils';

export default (first, second) => {
// check that the arguments are both arrays
if (!Array.isArray(first) || !Array.isArray(second)) {
return false;
}

// make sure the array lengths are equal
if (first.length !== second.length) {
return false;
}

// make copies of the inputs to retain their values after the test. for very
// large arrays this could potentially become expensive, but it's required for
// the currently implemented method below.
first = Array.from(first);
second = Array.from(second);

// while there are more elements of `first`, get the first element and try to
// find an equal element in `second`. if not found, fail, otherwise remove the
// elements from both `first` and `second`.
while (first.length) {
const head = first[0];

const idx = second.findIndex(e => equals(head, e));
if (idx === -1) {
return false;
}

first.splice(0, 1);
second.splice(idx, 1);
}

return true;
};
53 changes: 53 additions & 0 deletions src/matchers/toMatchArray/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import predicate from './predicate';

describe('toMatchArray Predicate', () => {
test('returns true when arrays are empty', () => {
expect(predicate([], [])).toBe(true);
});

test('returns true when arrays match with the same order', () => {
expect(predicate([1, 2, 3], [1, 2, 3])).toBe(true);
});

test('returns true when arrays match with reverse order', () => {
expect(predicate([1, 2, 3], [3, 2, 1])).toBe(true);
});

test('returns true when arrays match with duplicate elements and same order', () => {
expect(predicate([1, 2, 2], [1, 2, 2])).toBe(true);
});

test('returns true when arrays match with duplicate elements and reverse order', () => {
expect(predicate([1, 2, 2], [2, 2, 1])).toBe(true);
});

test('returns false when inputs are not arrays', () => {
expect(predicate(null, null)).toBe(false);
expect(predicate(null, [])).toBe(false);
expect(predicate([], null)).toBe(false);
});

test('returns false when arrays have different lengths', () => {
expect(predicate([], [1])).toBe(false);
expect(predicate([1], [])).toBe(false);
expect(predicate([1, 2, 3], [1, 2])).toBe(false);
});

test('returns false when no elements match', () => {
expect(predicate([1, 2], [3, 4])).toBe(false);
});

test('returns false when only one element matches', () => {
expect(predicate([1, 2, 3], [3, 4, 5])).toBe(false);
});

test('returns false when all but one element matches', () => {
expect(predicate([1, 2, 3], [2, 3, 4])).toBe(false);
});

test('does not modify the array content', () => {
const arr = [1, 2, 3];
predicate(arr, arr);
expect(arr).toEqual([1, 2, 3]);
});
});