From 07ef0ca7fcc31b0495a6ef1812e6b6c876acff3a Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Fri, 26 May 2017 14:18:10 +0200 Subject: [PATCH] Add spy matchers tests using ES6 Map and Set --- .../__snapshots__/spyMatchers-test.js.snap | 96 +++++++++++++++++++ .../src/__tests__/spyMatchers-test.js | 36 +++++++ 2 files changed, 132 insertions(+) diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap index c4e5ba06fbd6..548fea9d77a3 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap @@ -23,6 +23,38 @@ Expected mock function to not have been last called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`lastCalledWith works with Map 1`] = ` +"expect(jest.fn()).not.lastCalledWith(expected) + +Expected mock function to not have been last called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`lastCalledWith works with Map 2`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + [Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}] +But it was last called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`lastCalledWith works with Set 1`] = ` +"expect(jest.fn()).not.lastCalledWith(expected) + +Expected mock function to not have been last called with: + [Set {1, 2}]" +`; + +exports[`lastCalledWith works with Set 2`] = ` +"expect(jest.fn()).lastCalledWith(expected) + +Expected mock function to have been last called with: + [Set {3, 4}] +But it was last called with: + [Set {1, 2}]" +`; + exports[`lastCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).lastCalledWith(expected) @@ -219,6 +251,38 @@ Expected mock function not to have been called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toHaveBeenCalledWith works with Map 1`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenCalledWith works with Map 2`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + [Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}] +But it was called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenCalledWith works with Set 1`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [Set {1, 2}]" +`; + +exports[`toHaveBeenCalledWith works with Set 2`] = ` +"expect(jest.fn()).toHaveBeenCalledWith(expected) + +Expected mock function to have been called with: + [Set {3, 4}] +But it was called with: + [Set {1, 2}]" +`; + exports[`toHaveBeenCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) @@ -274,6 +338,38 @@ Expected mock function to not have been last called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toHaveBeenLastCalledWith works with Map 1`] = ` +"expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) + +Expected mock function to not have been last called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenLastCalledWith works with Map 2`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + [Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}] +But it was last called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenLastCalledWith works with Set 1`] = ` +"expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) + +Expected mock function to not have been last called with: + [Set {1, 2}]" +`; + +exports[`toHaveBeenLastCalledWith works with Set 2`] = ` +"expect(jest.fn()).toHaveBeenLastCalledWith(expected) + +Expected mock function to have been last called with: + [Set {3, 4}] +But it was last called with: + [Set {1, 2}]" +`; + exports[`toHaveBeenLastCalledWith works with arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenLastCalledWith(expected) diff --git a/packages/jest-matchers/src/__tests__/spyMatchers-test.js b/packages/jest-matchers/src/__tests__/spyMatchers-test.js index e5360079c234..ac7adf91f5ec 100644 --- a/packages/jest-matchers/src/__tests__/spyMatchers-test.js +++ b/packages/jest-matchers/src/__tests__/spyMatchers-test.js @@ -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'}]]);