From 709966faf40ff9e98346592336d18a766e2e2680 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 | 192 ++++++++++++++++++ .../src/__tests__/spyMatchers-test.js | 36 ++++ 2 files changed, 228 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 5dd794604ce4..9be75ae22f92 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/spyMatchers-test.js.snap @@ -15,6 +15,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 jest.fn and 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 jest.fn and 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 jest.fn and 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 jest.fn and 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 jest.fn and arguments that don't match 1`] = ` "expect(jest.fn()).lastCalledWith(expected) @@ -121,6 +153,38 @@ Expected mock function not to have been called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toBeCalledWith works with jest.fn and Map 1`] = ` +"expect(jest.fn()).not.toBeCalledWith(expected) + +Expected mock function not to have been called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toBeCalledWith works with jest.fn and Map 2`] = ` +"expect(jest.fn()).toBeCalledWith(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[`toBeCalledWith works with jest.fn and Set 1`] = ` +"expect(jest.fn()).not.toBeCalledWith(expected) + +Expected mock function not to have been called with: + [Set {1, 2}]" +`; + +exports[`toBeCalledWith works with jest.fn and Set 2`] = ` +"expect(jest.fn()).toBeCalledWith(expected) + +Expected mock function to have been called with: + [Set {3, 4}] +But it was called with: + [Set {1, 2}]" +`; + exports[`toBeCalledWith works with jest.fn and arguments that don't match 1`] = ` "expect(jest.fn()).toBeCalledWith(expected) @@ -300,6 +364,38 @@ Expected spy not to have been called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toHaveBeenCalledWith works with jasmine.createSpy and Map 1`] = ` +"expect(spy).not.toHaveBeenCalledWith(expected) + +Expected spy not to have been called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenCalledWith works with jasmine.createSpy and Map 2`] = ` +"expect(spy).toHaveBeenCalledWith(expected) + +Expected spy to have been called with: + [Map {\\"a\\" => \\"b\\", \\"b\\" => \\"a\\"}] +But it was called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenCalledWith works with jasmine.createSpy and Set 1`] = ` +"expect(spy).not.toHaveBeenCalledWith(expected) + +Expected spy not to have been called with: + [Set {1, 2}]" +`; + +exports[`toHaveBeenCalledWith works with jasmine.createSpy and Set 2`] = ` +"expect(spy).toHaveBeenCalledWith(expected) + +Expected spy to have been called with: + [Set {3, 4}] +But it was called with: + [Set {1, 2}]" +`; + exports[`toHaveBeenCalledWith works with jasmine.createSpy and arguments that don't match 1`] = ` "expect(spy).toHaveBeenCalledWith(expected) @@ -347,6 +443,38 @@ Expected mock function not to have been called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toHaveBeenCalledWith works with jest.fn and 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 jest.fn and 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 jest.fn and Set 1`] = ` +"expect(jest.fn()).not.toHaveBeenCalledWith(expected) + +Expected mock function not to have been called with: + [Set {1, 2}]" +`; + +exports[`toHaveBeenCalledWith works with jest.fn and 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 jest.fn and arguments that don't match 1`] = ` "expect(jest.fn()).toHaveBeenCalledWith(expected) @@ -402,6 +530,38 @@ Expected spy to not have been last called with: [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" `; +exports[`toHaveBeenLastCalledWith works with jasmine.createSpy and Map 1`] = ` +"expect(spy).not.toHaveBeenLastCalledWith(expected) + +Expected spy to not have been last called with: + [Map {1 => 2, 2 => 1}]" +`; + +exports[`toHaveBeenLastCalledWith works with jasmine.createSpy and Map 2`] = ` +"expect(spy).toHaveBeenLastCalledWith(expected) + +Expected spy 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 jasmine.createSpy and Set 1`] = ` +"expect(spy).not.toHaveBeenLastCalledWith(expected) + +Expected spy to not have been last called with: + [Set {1, 2}]" +`; + +exports[`toHaveBeenLastCalledWith works with jasmine.createSpy and Set 2`] = ` +"expect(spy).toHaveBeenLastCalledWith(expected) + +Expected spy to have been last called with: + [Set {3, 4}] +But it was last called with: + [Set {1, 2}]" +`; + exports[`toHaveBeenLastCalledWith works with jasmine.createSpy and arguments that don't match 1`] = ` "expect(spy).toHaveBeenLastCalledWith(expected) @@ -450,6 +610,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 jest.fn and 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 jest.fn and 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 jest.fn and 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 jest.fn and 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 jest.fn and 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 c7d49e2c4a6c..d66ecbd55b78 100644 --- a/packages/jest-matchers/src/__tests__/spyMatchers-test.js +++ b/packages/jest-matchers/src/__tests__/spyMatchers-test.js @@ -189,6 +189,42 @@ describe('toHaveBeenCalledTimes', () => { ).toThrowErrorMatchingSnapshot(); }); + test(`${calledWith} works with ${mockName} and Map`, () => { + const fn = getFunction(); + + 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 ${mockName} and Set`, () => { + const fn = getFunction(); + + 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 ${mockName} and Immutable.js objects`, () => { const fn = getFunction(); const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]);