Skip to content

Commit

Permalink
fix Map equality test (#4404)
Browse files Browse the repository at this point in the history
* fix Map equality test

#4303 added code to check Set equality in an order-independent way, but
applied that logic to all iterable objects. This change limits the scope of
the special-case to just Set and Map, and corrects the Map case to test
that both keys and values are equal.

* use isA instead of instanceof
  • Loading branch information
nigelzor authored and cpojer committed Sep 14, 2017
1 parent 8bc795c commit 6414c5d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 8 deletions.
135 changes: 135 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,35 @@ Received:
<red>[1, 2, 3]</>"
`;

exports[`.toEqual() {pass: false} expect([1, 2]).not.toEqual([1, 2]) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>[1, 2]</>
Received:
<red>[1, 2]</>"
`;

exports[`.toEqual() {pass: false} expect([1, 2]).toEqual([2, 1]) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>[2, 1]</>
Received:
<red>[1, 2]</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Array [
<red>+ 1,</>
<dim> 2,
<green>- 1,</>
<dim> ]"
`;

exports[`.toEqual() {pass: false} expect([1, 3]).toEqual(ArrayContaining [1, 2]) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expand All @@ -1975,6 +2004,34 @@ Difference:
<dim> ]"
`;

exports[`.toEqual() {pass: false} expect([1]).not.toEqual([1]) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>[1]</>
Received:
<red>[1]</>"
`;

exports[`.toEqual() {pass: false} expect([1]).toEqual([2]) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>[2]</>
Received:
<red>[1]</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Array [
<green>- 2,</>
<red>+ 1,</>
<dim> ]"
`;

exports[`.toEqual() {pass: false} expect([Function anonymous]).not.toEqual(Any<Function>) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expand Down Expand Up @@ -2051,6 +2108,15 @@ Received:
<red>{\\"a\\": 99}</>"
`;

exports[`.toEqual() {pass: false} expect({}).not.toEqual({}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>{}</>
Received:
<red>{}</>"
`;

exports[`.toEqual() {pass: false} expect(0).toEqual(-0) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expand Down Expand Up @@ -2095,6 +2161,66 @@ Difference:
Comparing two different types of values. Expected <green>array</> but received <red>number</>."
`;

exports[`.toEqual() {pass: false} expect(Map {"a" => 0}).toEqual(Map {"b" => 0}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Map {\\"b\\" => 0}</>
Received:
<red>Map {\\"a\\" => 0}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Map {
<green>- \\"b\\" => 0,</>
<red>+ \\"a\\" => 0,</>
<dim> }"
`;

exports[`.toEqual() {pass: false} expect(Map {"v" => 1}).toEqual(Map {"v" => 2}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Map {\\"v\\" => 2}</>
Received:
<red>Map {\\"v\\" => 1}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Map {
<green>- \\"v\\" => 2,</>
<red>+ \\"v\\" => 1,</>
<dim> }"
`;

exports[`.toEqual() {pass: false} expect(Map {}).not.toEqual(Map {}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>Map {}</>
Received:
<red>Map {}</>"
`;

exports[`.toEqual() {pass: false} expect(Map {}).toEqual(Set {}) 1`] = `
"<dim>expect(<red>received</><dim>).toEqual(<green>expected</><dim>)

Expected value to equal:
<green>Set {}</>
Received:
<red>Map {}</>

Difference:

Comparing two different types of values. Expected <green>set</> but received <red>map</>."
`;

exports[`.toEqual() {pass: false} expect(Map {1 => "one", 2 => "two"}).not.toEqual(Map {1 => "one", 2 => "two"}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expand Down Expand Up @@ -2132,6 +2258,15 @@ Difference:
<dim> }"
`;

exports[`.toEqual() {pass: false} expect(Set {}).not.toEqual(Set {}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expected value to not equal:
<green>Set {}</>
Received:
<red>Set {}</>"
`;

exports[`.toEqual() {pass: false} expect(Set {1, 2}).not.toEqual(Set {1, 2}) 1`] = `
"<dim>expect(<red>received</><dim>).not.toEqual(<green>expected</><dim>)

Expand Down
10 changes: 10 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ describe('.toEqual()', () => {
[{a: 5}, {b: 6}],
['banana', 'apple'],
[null, undefined],
[[1], [2]],
[[1, 2], [2, 1]],
[new Map(), new Set()],
[new Set([1, 2]), new Set()],
[new Set([1, 2]), new Set([1, 2, 3])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one']])],
[new Map([['a', 0]]), new Map([['b', 0]])],
[new Map([['v', 1]]), new Map([['v', 2]])],
[{a: 1, b: 2}, jestExpect.objectContaining({a: 2})],
[false, jestExpect.objectContaining({a: 2})],
[[1, 3], jestExpect.arrayContaining([1, 2])],
Expand Down Expand Up @@ -198,9 +203,14 @@ describe('.toEqual()', () => {
[true, true],
[1, 1],
['abc', 'abc'],
[[1], [1]],
[[1, 2], [1, 2]],
[{}, {}],
[{a: 99}, {a: 99}],
[new Set(), new Set()],
[new Set([1, 2]), new Set([1, 2])],
[new Set([1, 2]), new Set([2, 1])],
[new Map(), new Map()],
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one'], [2, 'two']])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[2, 'two'], [1, 'one']])],
[{a: 1, b: 2}, jestExpect.objectContaining({a: 1})],
Expand Down
29 changes: 21 additions & 8 deletions packages/expect/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import {equals} from './jasmine_utils';
import {equals, isA} from './jasmine_utils';

type GetPath = {
hasEndProp?: boolean,
Expand Down Expand Up @@ -114,16 +114,29 @@ export const iterableEquality = (a: any, b: any) => {
if (a.size !== undefined) {
if (a.size !== b.size) {
return false;
} else {
const args = [];
} else if (isA('Set', a)) {
let allFound = true;
for (const aValue of a) {
args.push(aValue);
if (!b.has(aValue)) {
allFound = false;
break;
}
}
for (const bValue of b) {
args.push(bValue);
if (allFound) {
return true;
}
} else if (isA('Map', a)) {
let allFound = true;
for (const aEntry of a) {
if (
!b.has(aEntry[0]) ||
!equals(aEntry[1], b.get(aEntry[0]), [iterableEquality])
) {
allFound = false;
break;
}
}
const merged = new a.constructor(args);
if (merged.size === a.size) {
if (allFound) {
return true;
}
}
Expand Down

0 comments on commit 6414c5d

Please sign in to comment.