chore(tests): drop console mock, fix all tests that throw console errors

This commit is contained in:
Łukasz Mierzwa
2018-09-08 21:11:15 +01:00
parent a4557cfc49
commit 4d64b0b3ea
15 changed files with 77 additions and 26 deletions
-6
View File
@@ -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",
-1
View File
@@ -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"
+3 -1
View File
@@ -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);
+8
View File
@@ -29,6 +29,12 @@ afterEach(() => {
global.fetch.mockRestore();
});
const MockEmptyAPIResponseWithoutFilters = () => {
const response = EmptyAPIResponse();
response.filters = [];
fetch.mockResponse(JSON.stringify(response));
};
const MountedFetcher = () => {
return mount(
<Fetcher alertStore={alertStore} settingsStore={settingsStore} />
@@ -52,6 +58,7 @@ describe("<Fetcher />", () => {
});
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("<Fetcher />", () => {
});
it("calls alertStore.fetchWithThrottle again after filter change", () => {
MockEmptyAPIResponseWithoutFilters();
const fetchSpy = jest.spyOn(alertStore, "fetchWithThrottle");
MountedFetcher();
alertStore.filters.values = [];
@@ -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) => {
<Provider alertStore={alertStore}>
<Alert
alert={alert}
group={{}}
showAlertmanagers={showAlertmanagers}
showReceiver={showReceiver}
afterUpdate={MockAfterUpdate}
silenceFormStore={silenceFormStore}
/>
</Provider>
);
@@ -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 => {
@@ -24,19 +24,19 @@ describe("<AlertGroupConfiguration /> 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);
});
});
@@ -158,6 +158,7 @@ describe("<FilterInput Autosuggest />", () => {
});
it("handles invalid JSON in suggestion fetches", async () => {
jest.spyOn(console, "error").mockImplementation(() => {});
fetch.mockResponseOnce("this is not JSON");
const tree = MountedInput();
@@ -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("<LabelNameInput />", () => {
}, 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);
@@ -11,6 +11,10 @@ let alertStore;
let settingsStore;
let silenceFormStore;
beforeAll(() => {
fetch.mockResponse(JSON.stringify([]));
});
beforeEach(() => {
alertStore = new AlertStore([]);
settingsStore = new Settings();
@@ -17,9 +17,17 @@ beforeEach(() => {
matcher = NewEmptyMatcher();
});
const MockOnDelete = jest.fn();
const ShallowLabelValueInput = () => {
return shallow(
<SilenceMatch matcher={matcher} silenceFormStore={silenceFormStore} />
<SilenceMatch
matcher={matcher}
silenceFormStore={silenceFormStore}
showDelete={false}
onDelete={MockOnDelete}
isValid={true}
/>
);
};
@@ -11,6 +11,10 @@ let alertStore;
let settingsStore;
let silenceFormStore;
beforeAll(() => {
fetch.mockResponse(JSON.stringify([]));
});
beforeEach(() => {
alertStore = new AlertStore([]);
settingsStore = new Settings();
+4 -2
View File
@@ -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([]);
+5 -1
View File
@@ -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();
});
-5
View File
@@ -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");