From cdd74396e8335df573d9f9312a1f4ee05dae3879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 20 Dec 2019 21:57:52 +0000 Subject: [PATCH] fix(tests): correctly fail mock requests --- .../AlertGroupSortConfiguration.test.js | 2 +- .../ManagedSilence/DeleteSilence.test.js | 6 +-- .../NavBar/FilterInput/index.test.js | 2 +- .../SilenceMatch/LabelNameInput.test.js | 2 +- .../SilenceMatch/MatchCounter.test.js | 4 +- .../SilenceModal/SilencePreview/index.test.js | 2 +- ui/src/Stores/AlertStore.test.js | 38 +++++-------------- 7 files changed, 19 insertions(+), 37 deletions(-) diff --git a/ui/src/Components/MainModal/Configuration/AlertGroupSortConfiguration.test.js b/ui/src/Components/MainModal/Configuration/AlertGroupSortConfiguration.test.js index 822e20701..18d92918d 100644 --- a/ui/src/Components/MainModal/Configuration/AlertGroupSortConfiguration.test.js +++ b/ui/src/Components/MainModal/Configuration/AlertGroupSortConfiguration.test.js @@ -146,7 +146,7 @@ describe("", () => { }); it("label select handles fetch errors", async () => { - fetch.mockReject("error"); + fetch.mockReject(new Error("Fetch error")); const tree = await ExpandSortLabelSuggestions(); const options = tree.find("div.react-select__option"); expect(options).toHaveLength(0); diff --git a/ui/src/Components/ManagedSilence/DeleteSilence.test.js b/ui/src/Components/ManagedSilence/DeleteSilence.test.js index 520341727..296b8d828 100644 --- a/ui/src/Components/ManagedSilence/DeleteSilence.test.js +++ b/ui/src/Components/ManagedSilence/DeleteSilence.test.js @@ -136,7 +136,7 @@ describe("", () => { it("renders ErrorMessage on failed fetch", async () => { jest.spyOn(console, "trace").mockImplementation(() => {}); fetch.resetMocks(); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); const tree = MountedDeleteSilenceModalContent(); await expect(tree.instance().previewState.fetch).resolves.toBeUndefined(); @@ -148,7 +148,7 @@ describe("", () => { fetch.mockResponseOnce("not json"); jest.spyOn(console, "trace").mockImplementation(() => {}); fetch.resetMocks(); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); const tree = MountedDeleteSilenceModalContent(); await expect(tree.instance().previewState.fetch).resolves.toBeUndefined(); @@ -248,7 +248,7 @@ describe("", () => { jest.spyOn(console, "trace").mockImplementation(() => {}); fetch.resetMocks(); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); tree.find(".btn-danger").simulate("click"); await expect(tree.instance().deleteState.fetch).resolves.toBeUndefined(); diff --git a/ui/src/Components/NavBar/FilterInput/index.test.js b/ui/src/Components/NavBar/FilterInput/index.test.js index 79cf862d7..d5a15c2ad 100644 --- a/ui/src/Components/NavBar/FilterInput/index.test.js +++ b/ui/src/Components/NavBar/FilterInput/index.test.js @@ -196,7 +196,7 @@ describe("", () => { }); it("handles failed suggestion fetches", async () => { - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); const tree = MountedInput(); const instance = tree.instance(); diff --git a/ui/src/Components/SilenceModal/SilenceMatch/LabelNameInput.test.js b/ui/src/Components/SilenceModal/SilenceMatch/LabelNameInput.test.js index abf313ac3..f098b47e7 100644 --- a/ui/src/Components/SilenceModal/SilenceMatch/LabelNameInput.test.js +++ b/ui/src/Components/SilenceModal/SilenceMatch/LabelNameInput.test.js @@ -114,7 +114,7 @@ describe("", () => { }); it("handles fetch errors when populating suggestions", async () => { - fetch.mockReject("error"); + fetch.mockReject(new Error("Fetch error")); const tree = MountedLabelNameInput(true); const instance = tree.instance(); await expect(instance.nameSuggestionsFetch).resolves.toBeUndefined(); diff --git a/ui/src/Components/SilenceModal/SilenceMatch/MatchCounter.test.js b/ui/src/Components/SilenceModal/SilenceMatch/MatchCounter.test.js index bbe8be648..781f612a8 100644 --- a/ui/src/Components/SilenceModal/SilenceMatch/MatchCounter.test.js +++ b/ui/src/Components/SilenceModal/SilenceMatch/MatchCounter.test.js @@ -42,7 +42,7 @@ describe("", () => { const consoleSpy = jest .spyOn(console, "trace") .mockImplementation(() => {}); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); // we need to set name & value to trigger fetch matcher.name = "foo"; @@ -55,7 +55,7 @@ describe("", () => { it("renders error icon on failed fetch", async () => { jest.spyOn(console, "trace").mockImplementation(() => {}); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); // we need to set name & value to trigger fetch matcher.name = "foo"; diff --git a/ui/src/Components/SilenceModal/SilencePreview/index.test.js b/ui/src/Components/SilenceModal/SilencePreview/index.test.js index dee89e625..18c123d9b 100644 --- a/ui/src/Components/SilenceModal/SilencePreview/index.test.js +++ b/ui/src/Components/SilenceModal/SilencePreview/index.test.js @@ -125,7 +125,7 @@ describe("", () => { const consoleSpy = jest .spyOn(console, "trace") .mockImplementation(() => {}); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); const tree = MountedSilencePreview(); await expect(tree.instance().matchedAlerts.fetch).resolves.toBeUndefined(); diff --git a/ui/src/Stores/AlertStore.test.js b/ui/src/Stores/AlertStore.test.js index 8741cd1ab..aa7792d0c 100644 --- a/ui/src/Stores/AlertStore.test.js +++ b/ui/src/Stores/AlertStore.test.js @@ -370,14 +370,9 @@ describe("AlertStore.fetch", () => { const consoleSpy = jest .spyOn(console, "trace") .mockImplementation(() => {}); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); const store = new AlertStore([]); - store.retryConfig = { - retries: 5, - minTimeout: 10, - maxTimeout: 10 - }; await expect(store.fetch()).resolves.toHaveProperty("error"); expect(global.fetch).toHaveBeenCalledTimes(6); @@ -388,28 +383,20 @@ describe("AlertStore.fetch", () => { }); it("fetch() retries on failure", async () => { + jest.spyOn(console, "trace").mockImplementation(() => {}); const store = new AlertStore([]); - store.retryConfig = { - retries: 5, - minTimeout: 10, - maxTimeout: 10 - }; - fetch.mockReject("Fetch error"); - await expect(store.fetch()).rejects.toBeTruthy(); + fetch.mockReject(new Error("Fetch error")); + await expect(store.fetch()).resolves.toHaveProperty("error"); expect(global.fetch).toHaveBeenCalledTimes(6); }); it("fetch() retry counter is reset after successful fetch", async () => { + jest.spyOn(console, "trace").mockImplementation(() => {}); const store = new AlertStore(["label=value"]); - store.retryConfig = { - retries: 5, - minTimeout: 10, - maxTimeout: 10 - }; - fetch.mockReject("Fetch error"); - await expect(store.fetch()).rejects.toBeTruthy(); + fetch.mockReject(new Error("Fetch error")); + await expect(store.fetch()).resolves.toHaveProperty("error"); expect(global.fetch).toHaveBeenCalledTimes(6); const response = EmptyAPIResponse(); @@ -417,22 +404,17 @@ describe("AlertStore.fetch", () => { await expect(store.fetch()).resolves.toBeUndefined(); expect(global.fetch).toHaveBeenCalledTimes(7); - fetch.mockReject("Fetch error"); - await expect(store.fetch()).rejects.toBeTruthy(); + fetch.mockReject(new Error("Fetch error")); + await expect(store.fetch()).resolves.toHaveProperty("error"); expect(global.fetch).toHaveBeenCalledTimes(13); }); it("unapplied filters are marked as applied on fetch error", async () => { const store = new AlertStore([NewUnappliedFilter("foo")]); - store.retryConfig = { - retries: 5, - minTimeout: 10, - maxTimeout: 10 - }; store.filters.values[0].applied = false; jest.spyOn(console, "trace").mockImplementation(() => {}); - fetch.mockReject("Fetch error"); + fetch.mockReject(new Error("Fetch error")); await expect(store.fetch()).resolves.toHaveProperty("error"); expect(store.filters.values[0].applied).toBe(true); });