Skip to content

Commit

Permalink
Add spy matchers tests using ES6 Map and Set
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Jun 27, 2017
1 parent f43a04a commit 4a0daf8
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ Expected mock function to not have been last called with:
<green>[Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]</>"
`;

exports[`lastCalledWith works with Map 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.lastCalledWith(<green>expected</><dim>)

Expected mock function to not have been last called with:
<green>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`lastCalledWith works with Map 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).lastCalledWith(<green>expected</><dim>)

Expected mock function to have been last called with:
<green>[Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}]</>
But it was last called with:
<red>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`lastCalledWith works with Set 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.lastCalledWith(<green>expected</><dim>)

Expected mock function to not have been last called with:
<green>[Set {1, 2}]</>"
`;

exports[`lastCalledWith works with Set 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).lastCalledWith(<green>expected</><dim>)

Expected mock function to have been last called with:
<green>[Set {3, 4}]</>
But it was last called with:
<red>[Set {1, 2}]</>"
`;

exports[`lastCalledWith works with arguments that don't match 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).lastCalledWith(<green>expected</><dim>)

Expand Down Expand Up @@ -219,6 +251,38 @@ Expected mock function not to have been called with:
<green>[Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]</>"
`;

exports[`toHaveBeenCalledWith works with Map 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.toHaveBeenCalledWith(<green>expected</><dim>)

Expected mock function not to have been called with:
<green>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`toHaveBeenCalledWith works with Map 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenCalledWith(<green>expected</><dim>)

Expected mock function to have been called with:
<green>[Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}]</>
But it was called with:
<red>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`toHaveBeenCalledWith works with Set 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.toHaveBeenCalledWith(<green>expected</><dim>)

Expected mock function not to have been called with:
<green>[Set {1, 2}]</>"
`;

exports[`toHaveBeenCalledWith works with Set 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenCalledWith(<green>expected</><dim>)

Expected mock function to have been called with:
<green>[Set {3, 4}]</>
But it was called with:
<red>[Set {1, 2}]</>"
`;

exports[`toHaveBeenCalledWith works with arguments that don't match 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenCalledWith(<green>expected</><dim>)

Expand Down Expand Up @@ -274,6 +338,38 @@ Expected mock function to not have been last called with:
<green>[Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]</>"
`;

exports[`toHaveBeenLastCalledWith works with Map 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.toHaveBeenLastCalledWith(<green>expected</><dim>)

Expected mock function to not have been last called with:
<green>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`toHaveBeenLastCalledWith works with Map 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenLastCalledWith(<green>expected</><dim>)

Expected mock function to have been last called with:
<green>[Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}]</>
But it was last called with:
<red>[Map {1 => 2, 2 => 1}]</>"
`;

exports[`toHaveBeenLastCalledWith works with Set 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).not.toHaveBeenLastCalledWith(<green>expected</><dim>)

Expected mock function to not have been last called with:
<green>[Set {1, 2}]</>"
`;

exports[`toHaveBeenLastCalledWith works with Set 2`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenLastCalledWith(<green>expected</><dim>)

Expected mock function to have been last called with:
<green>[Set {3, 4}]</>
But it was last called with:
<red>[Set {1, 2}]</>"
`;

exports[`toHaveBeenLastCalledWith works with arguments that don't match 1`] = `
"<dim>expect(<red>jest.fn()</><dim>).toHaveBeenLastCalledWith(<green>expected</><dim>)

Expand Down
36 changes: 36 additions & 0 deletions packages/jest-matchers/src/__tests__/spy_matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,42 @@ describe('toHaveBeenCalledTimes', () => {
).toThrowErrorMatchingSnapshot();
});

test(`${calledWith} works with Map`, () => {
const fn = jest.fn();

const m1 = new Map([[1, 2], [2, 1]]);
const m2 = new Map([[1, 2], [2, 1]]);
const m3 = new Map([['a', 'b'], ['b', 'a']]);

fn(m1);

jestExpect(fn)[calledWith](m2);
jestExpect(fn).not[calledWith](m3);

expect(() =>
jestExpect(fn).not[calledWith](m2),
).toThrowErrorMatchingSnapshot();
expect(() => jestExpect(fn)[calledWith](m3)).toThrowErrorMatchingSnapshot();
});

test(`${calledWith} works with Set`, () => {
const fn = jest.fn();

const s1 = new Set([1, 2]);
const s2 = new Set([1, 2]);
const s3 = new Set([3, 4]);

fn(s1);

jestExpect(fn)[calledWith](s2);
jestExpect(fn).not[calledWith](s3);

expect(() =>
jestExpect(fn).not[calledWith](s2),
).toThrowErrorMatchingSnapshot();
expect(() => jestExpect(fn)[calledWith](s3)).toThrowErrorMatchingSnapshot();
});

test(`${calledWith} works with Immutable.js objects`, () => {
const fn = jest.fn();
const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]);
Expand Down

0 comments on commit 4a0daf8

Please sign in to comment.