diff --git a/ui/package-lock.json b/ui/package-lock.json index 9222dd98e..856c3dc75 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -6834,12 +6834,6 @@ "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-20.0.3.tgz", "integrity": "sha1-i8Bw6QQUqhVcEajWTIaaDVxx2lk=" }, - "jest-mock-console": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/jest-mock-console/-/jest-mock-console-0.4.0.tgz", - "integrity": "sha512-WElCbNvfqQlD7cpfHfTn1ytZ+RjKg1Ftrvr5wEjdWP7a9esXmaiZuEAPeYUSK5fd0Cra+dR1oF8HAjjKKxDQdg==", - "dev": true - }, "jest-regex-util": { "version": "20.0.3", "resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-20.0.3.tgz", diff --git a/ui/package.json b/ui/package.json index b28e458fd..8e0a6701b 100644 --- a/ui/package.json +++ b/ui/package.json @@ -61,7 +61,6 @@ "jest-date-mock": "1.0.3", "jest-fetch-mock": "1.6.5", "jest-localstorage-mock": "2.2.0", - "jest-mock-console": "0.4.0", "markdownlint-cli": "0.13.0", "node-sass-chokidar": "1.3.3", "onchange": "4.1.0" diff --git a/ui/src/AppBoot.test.js b/ui/src/AppBoot.test.js index 0f390bf58..d69c20602 100644 --- a/ui/src/AppBoot.test.js +++ b/ui/src/AppBoot.test.js @@ -61,7 +61,9 @@ describe("SetupRaven()", () => { }); it("logs an error when invalid DSN is passed to raven", () => { - const consoleSpy = jest.spyOn(console, "error"); + const consoleSpy = jest + .spyOn(console, "error") + .mockImplementation(() => {}); const client = RavenClient("invalidDSN"); expect(client.isSetup()).toBeFalsy(); expect(consoleSpy).toHaveBeenCalledTimes(1); diff --git a/ui/src/Components/Fetcher/index.test.js b/ui/src/Components/Fetcher/index.test.js index 5de2ec694..b10db3132 100644 --- a/ui/src/Components/Fetcher/index.test.js +++ b/ui/src/Components/Fetcher/index.test.js @@ -29,6 +29,12 @@ afterEach(() => { global.fetch.mockRestore(); }); +const MockEmptyAPIResponseWithoutFilters = () => { + const response = EmptyAPIResponse(); + response.filters = []; + fetch.mockResponse(JSON.stringify(response)); +}; + const MountedFetcher = () => { return mount( @@ -52,6 +58,7 @@ describe("", () => { }); it("re-renders on filters change", () => { + MockEmptyAPIResponseWithoutFilters(); const tree = MountedFetcher(); expect(tree.html()).toBe(FetcherSpan("label=value", 30)); alertStore.filters.values = []; @@ -72,6 +79,7 @@ describe("", () => { }); it("calls alertStore.fetchWithThrottle again after filter change", () => { + MockEmptyAPIResponseWithoutFilters(); const fetchSpy = jest.spyOn(alertStore, "fetchWithThrottle"); MountedFetcher(); alertStore.filters.values = []; diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js index 67f424f33..a1e8dfc2f 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Alert/index.test.js @@ -10,13 +10,16 @@ import toDiffableHtml from "diffable-html"; import { MockAlert, MockAnnotation } from "__mocks__/Alerts.js"; import { AlertStore } from "Stores/AlertStore"; +import { SilenceFormStore } from "Stores/SilenceFormStore"; import { Alert } from "."; let alertStore; +let silenceFormStore; beforeEach(() => { advanceTo(new Date(2018, 7, 15, 20, 40, 0)); alertStore = new AlertStore([]); + silenceFormStore = new SilenceFormStore(); }); afterEach(() => { @@ -43,9 +46,11 @@ const MountedAlert = (alert, showAlertmanagers, showReceiver) => { ); diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js index 3739ac412..1a520ba30 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js @@ -17,9 +17,9 @@ let settingsStore; let silenceFormStore; let group; -const MockGroup = (groupName, alertCount) => { +const MockGroup = groupName => { const group = MockAlertGroup( - { alertname: "Fake Alert", group: groupName }, + { alertname: "Fake Alert", groupName: groupName }, [], [], {} @@ -31,7 +31,7 @@ beforeEach(() => { alertStore = new AlertStore([]); settingsStore = new Settings(); silenceFormStore = new SilenceFormStore(); - group = MockGroup(); + group = MockGroup("fakeGroup"); }); const MockAlerts = alertCount => { diff --git a/ui/src/Components/MainModal/Configuration/AlertGroupConfiguration.test.js b/ui/src/Components/MainModal/Configuration/AlertGroupConfiguration.test.js index 1fdb9b821..b9525489c 100644 --- a/ui/src/Components/MainModal/Configuration/AlertGroupConfiguration.test.js +++ b/ui/src/Components/MainModal/Configuration/AlertGroupConfiguration.test.js @@ -24,19 +24,19 @@ describe(" className", () => { it("call to onChange() updates internal state", () => { const tree = FakeConfiguration(); - tree.instance().onChange(11); - expect(tree.instance().config.defaultRenderCount).toBe(11); + tree.instance().onChange(9); + expect(tree.instance().config.defaultRenderCount).toBe(9); }); it("settings are updated on completed change", () => { const tree = FakeConfiguration(); - tree.instance().onChangeComplete(96); - expect(settingsStore.alertGroupConfig.config.defaultRenderCount).toBe(96); + tree.instance().onChangeComplete(8); + expect(settingsStore.alertGroupConfig.config.defaultRenderCount).toBe(8); }); it("custom interval value is rendered correctly", () => { - settingsStore.alertGroupConfig.config.defaultRenderCount = 55; + settingsStore.alertGroupConfig.config.defaultRenderCount = 4; const component = FakeConfiguration(); - expect(component.find("InputRange").props().value).toBe(55); + expect(component.find("InputRange").props().value).toBe(4); }); }); diff --git a/ui/src/Components/NavBar/FilterInput/index.test.js b/ui/src/Components/NavBar/FilterInput/index.test.js index 296835244..8468f041c 100644 --- a/ui/src/Components/NavBar/FilterInput/index.test.js +++ b/ui/src/Components/NavBar/FilterInput/index.test.js @@ -158,6 +158,7 @@ describe("", () => { }); it("handles invalid JSON in suggestion fetches", async () => { + jest.spyOn(console, "error").mockImplementation(() => {}); fetch.mockResponseOnce("this is not JSON"); const tree = MountedInput(); diff --git a/ui/src/Components/SilenceModal/LabelNameInput.test.js b/ui/src/Components/SilenceModal/LabelNameInput.test.js index 221323357..1e54994d5 100644 --- a/ui/src/Components/SilenceModal/LabelNameInput.test.js +++ b/ui/src/Components/SilenceModal/LabelNameInput.test.js @@ -7,6 +7,10 @@ import { LabelNameInput } from "./LabelNameInput"; let matcher; +beforeAll(() => { + fetch.mockResponse(JSON.stringify([])); +}); + beforeEach(() => { matcher = NewEmptyMatcher(); matcher.name = "name"; @@ -95,6 +99,27 @@ describe("", () => { }, 100); }); + it("handles fetch errors when populating suggestions", done => { + fetch.mockReject("error"); + ShallowLabelNameInput(true); + // use timeout since mount will call fetch + setTimeout(() => { + expect(matcher.suggestions.names).toHaveLength(0); + done(); + }, 100); + }); + + it("handles invalid JSON when populating suggestions", done => { + jest.spyOn(console, "error").mockImplementation(() => {}); + fetch.mockResponse("this is not JSON"); + ShallowLabelNameInput(true); + // use timeout since mount will call fetch + setTimeout(() => { + expect(matcher.suggestions.names).toHaveLength(0); + done(); + }, 100); + }); + it("suggestions are emptied on failed fetch", done => { fetch.mockReject(new Error("fake error message")); ShallowLabelNameInput(true); diff --git a/ui/src/Components/SilenceModal/SilenceForm.test.js b/ui/src/Components/SilenceModal/SilenceForm.test.js index 20d52490f..3765ccfe5 100644 --- a/ui/src/Components/SilenceModal/SilenceForm.test.js +++ b/ui/src/Components/SilenceModal/SilenceForm.test.js @@ -11,6 +11,10 @@ let alertStore; let settingsStore; let silenceFormStore; +beforeAll(() => { + fetch.mockResponse(JSON.stringify([])); +}); + beforeEach(() => { alertStore = new AlertStore([]); settingsStore = new Settings(); diff --git a/ui/src/Components/SilenceModal/SilenceMatch.test.js b/ui/src/Components/SilenceModal/SilenceMatch.test.js index 958a80adc..ffd7897df 100644 --- a/ui/src/Components/SilenceModal/SilenceMatch.test.js +++ b/ui/src/Components/SilenceModal/SilenceMatch.test.js @@ -17,9 +17,17 @@ beforeEach(() => { matcher = NewEmptyMatcher(); }); +const MockOnDelete = jest.fn(); + const ShallowLabelValueInput = () => { return shallow( - + ); }; diff --git a/ui/src/Components/SilenceModal/index.test.js b/ui/src/Components/SilenceModal/index.test.js index 6ccaebbb9..ad858cf0e 100644 --- a/ui/src/Components/SilenceModal/index.test.js +++ b/ui/src/Components/SilenceModal/index.test.js @@ -11,6 +11,10 @@ let alertStore; let settingsStore; let silenceFormStore; +beforeAll(() => { + fetch.mockResponse(JSON.stringify([])); +}); + beforeEach(() => { alertStore = new AlertStore([]); settingsStore = new Settings(); diff --git a/ui/src/Stores/AlertStore.test.js b/ui/src/Stores/AlertStore.test.js index 56cd6216a..005658060 100644 --- a/ui/src/Stores/AlertStore.test.js +++ b/ui/src/Stores/AlertStore.test.js @@ -207,7 +207,7 @@ describe("UpdateLocationSearch", () => { describe("AlertStore.fetch", () => { it("parseAPIResponse() rejects a response with mismatched filters", () => { - const consoleSpy = jest.spyOn(console, "info"); + const consoleSpy = jest.spyOn(console, "info").mockImplementation(() => {}); const response = EmptyAPIResponse(); const store = new AlertStore([]); @@ -256,7 +256,9 @@ describe("AlertStore.fetch", () => { }); it("fetch() handles response that throws an error correctly", async () => { - const consoleSpy = jest.spyOn(console, "trace"); + const consoleSpy = jest + .spyOn(console, "trace") + .mockImplementation(() => {}); fetch.mockReject("Fetch error"); const store = new AlertStore([]); diff --git a/ui/src/index.test.js b/ui/src/index.test.js index be85982c3..69a001431 100644 --- a/ui/src/index.test.js +++ b/ui/src/index.test.js @@ -1,5 +1,9 @@ -import Index from "./index.js"; +import { EmptyAPIResponse } from "__mocks__/Fetch"; it("renders without crashing", () => { + const response = EmptyAPIResponse(); + response.filters = []; + fetch.mockResponse(JSON.stringify(response)); + const Index = require("./index.js"); expect(Index).toBeTruthy(); }); diff --git a/ui/src/setupTests.js b/ui/src/setupTests.js index da247227d..ac6671f30 100644 --- a/ui/src/setupTests.js +++ b/ui/src/setupTests.js @@ -1,14 +1,9 @@ import Enzyme from "enzyme"; import Adapter from "enzyme-adapter-react-16"; -import mockConsole from "jest-mock-console"; - // https://github.com/airbnb/enzyme Enzyme.configure({ adapter: new Adapter() }); -// mock console -mockConsole(["error", "warn", "info", "log", "trace"]); - // localStorage is used for Settings store require("jest-localstorage-mock");