mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
fix(tests): correctly fail mock requests
This commit is contained in:
@@ -146,7 +146,7 @@ describe("<AlertGroupSortConfiguration />", () => {
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
@@ -136,7 +136,7 @@ describe("<DeleteSilenceModalContent />", () => {
|
||||
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("<DeleteSilenceModalContent />", () => {
|
||||
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("<DeleteSilenceModalContent />", () => {
|
||||
|
||||
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();
|
||||
|
||||
@@ -196,7 +196,7 @@ describe("<FilterInput Autosuggest />", () => {
|
||||
});
|
||||
|
||||
it("handles failed suggestion fetches", async () => {
|
||||
fetch.mockReject("Fetch error");
|
||||
fetch.mockReject(new Error("Fetch error"));
|
||||
|
||||
const tree = MountedInput();
|
||||
const instance = tree.instance();
|
||||
|
||||
@@ -114,7 +114,7 @@ describe("<LabelNameInput />", () => {
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
@@ -42,7 +42,7 @@ describe("<MatchCounter />", () => {
|
||||
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("<MatchCounter />", () => {
|
||||
|
||||
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";
|
||||
|
||||
@@ -125,7 +125,7 @@ describe("<SilencePreview />", () => {
|
||||
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();
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user