From 093dce1a58c5398d5fde999e4b4fd898f4a32e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 3 Oct 2018 09:56:41 +0100 Subject: [PATCH] fix(tests): new jsdom mocks localStorage, no need to provide a mock This also needs to be mocked a little different: https://github.com/facebook/jest/issues/6798#issuecomment-412871616 --- ui/package.json | 1 - ui/src/App.test.js | 16 +++++++++++++--- ui/src/setupTests.js | 3 --- ui/yarn.lock | 5 ----- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ui/package.json b/ui/package.json index 166a96358..af44e13a1 100644 --- a/ui/package.json +++ b/ui/package.json @@ -57,7 +57,6 @@ "jest-canvas-mock": "1.1.0", "jest-date-mock": "1.0.5", "jest-fetch-mock": "1.6.6", - "jest-localstorage-mock": "2.2.0", "markdownlint-cli": "0.13.0", "node-sass": "4.9.3" }, diff --git a/ui/src/App.test.js b/ui/src/App.test.js index aac84206d..7085a972c 100644 --- a/ui/src/App.test.js +++ b/ui/src/App.test.js @@ -36,10 +36,13 @@ describe("", () => { }) ); + // https://github.com/facebook/jest/issues/6798#issuecomment-412871616 + const getItemSpy = jest.spyOn(Storage.prototype, "getItem"); + const tree = shallow(); const instance = tree.instance(); - expect(localStorage.getItem).toHaveBeenCalledWith("savedFilters"); + expect(getItemSpy).toHaveBeenCalledWith("savedFilters"); expect(instance.alertStore.filters.values).toHaveLength(2); expect(instance.alertStore.filters.values[0]).toMatchObject( @@ -48,9 +51,11 @@ describe("", () => { expect(instance.alertStore.filters.values[1]).toMatchObject( NewUnappliedFilter("abc!=cba") ); + + getItemSpy.mockRestore(); }); - it("ignores saved filters if 'preset' key is falsey (use passed defaults)", () => { + it("ignores saved filters if 'present' key is falsey (use passed defaults)", () => { expect(window.location.search).toBe(""); localStorage.setItem( "savedFilters", @@ -60,15 +65,20 @@ describe("", () => { }) ); + // https://github.com/facebook/jest/issues/6798#issuecomment-412871616 + const getItemSpy = jest.spyOn(Storage.prototype, "getItem"); + const tree = shallow(); const instance = tree.instance(); - expect(localStorage.getItem).toHaveBeenCalledWith("savedFilters"); + expect(getItemSpy).toHaveBeenCalledWith("savedFilters"); expect(instance.alertStore.filters.values).toHaveLength(1); expect(instance.alertStore.filters.values[0]).toMatchObject( NewUnappliedFilter("use=defaults") ); + + getItemSpy.mockRestore(); }); it("uses filters passed via ?q= query args (ignoring saved filters and passed defaults)", () => { diff --git a/ui/src/setupTests.js b/ui/src/setupTests.js index 276cd1343..b1c5defd8 100644 --- a/ui/src/setupTests.js +++ b/ui/src/setupTests.js @@ -4,9 +4,6 @@ import Adapter from "enzyme-adapter-react-16"; // https://github.com/airbnb/enzyme Enzyme.configure({ adapter: new Adapter() }); -// localStorage is used for Settings store -import("jest-localstorage-mock"); - // favico.js needs canvas import("jest-canvas-mock"); diff --git a/ui/yarn.lock b/ui/yarn.lock index 879a98ac9..015766a1a 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -6071,11 +6071,6 @@ jest-leak-detector@^23.6.0: dependencies: pretty-format "^23.6.0" -jest-localstorage-mock@2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/jest-localstorage-mock/-/jest-localstorage-mock-2.2.0.tgz#ce9a9de01dfdde2ad8aa08adf73acc7e5cc394cf" - integrity sha512-x+P0vcwr4540bCAYzTEpiD9rs+zh/QZzyiABV+MU6yM2OPwPlrrLyUx/6gValMyt6tg5lX6Z53o2rHWfUht5Xw== - jest-matcher-utils@^23.6.0: version "23.6.0" resolved "https://registry.yarnpkg.com/jest-matcher-utils/-/jest-matcher-utils-23.6.0.tgz#726bcea0c5294261a7417afb6da3186b4b8cac80"