fix(ui): fix typescript 4.4 errors

This commit is contained in:
Łukasz Mierzwa
2021-08-27 10:20:03 +01:00
committed by Łukasz Mierzwa
parent 2ad51a5e92
commit 9dc15872a3
6 changed files with 74 additions and 3 deletions

View File

@@ -22,6 +22,9 @@ describe("useFetchAny", () => {
fetchMock.mock("http://localhost/error", {
throws: new TypeError("failed to fetch"),
});
fetchMock.mock("http://localhost/unknown", {
throws: "foo",
});
});
beforeEach(() => {
@@ -168,6 +171,25 @@ describe("useFetchAny", () => {
expect(result.current.responseURI).toBe(null);
});
it("error is updated after unknown error", async () => {
const upstreams = [{ uri: "http://localhost/unknown", options: {} }];
const { result, waitForNextUpdate } = renderHook(() =>
useFetchAny(upstreams)
);
expect(result.current.response).toBe(null);
expect(result.current.error).toBe(null);
expect(result.current.inProgress).toBe(true);
expect(result.current.responseURI).toBe(null);
await waitForNextUpdate();
expect(result.current.response).toBe(null);
expect(result.current.error).toBe("unknown error: foo");
expect(result.current.inProgress).toBe(false);
expect(result.current.responseURI).toBe(null);
});
it("doesn't update response after cleanup", async () => {
fetchMock.mock(
"http://localhost/slow/ok",

View File

@@ -107,7 +107,10 @@ const useFetchAny = <T>(
} else {
setResponse({
response: null,
error: error.message,
error:
error instanceof Error
? error.message
: `unknown error: ${error}`,
responseURI: null,
inProgress: false,
});

View File

@@ -18,6 +18,9 @@ describe("useFetchDelete", () => {
fetchMock.mock("http://localhost/error", {
throws: new TypeError("failed to fetch"),
});
fetchMock.mock("http://localhost/unknown", {
throws: "foo",
});
});
beforeEach(() => {
@@ -108,6 +111,22 @@ describe("useFetchDelete", () => {
expect(result.current.isDeleting).toBe(false);
});
it("error is updated after unknown error", async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useFetchDelete("http://localhost/unknown", EmptyOptions)
);
expect(result.current.response).toBe(null);
expect(result.current.error).toBe(null);
expect(result.current.isDeleting).toBe(true);
await waitForNextUpdate();
expect(result.current.response).toBe(null);
expect(result.current.error).toBe("unknown error: foo");
expect(result.current.isDeleting).toBe(false);
});
it("doesn't update response after cleanup", async () => {
fetchMock.mock(
"http://localhost/slow/ok",

View File

@@ -45,7 +45,9 @@ const useFetchDelete = (
}
} catch (error) {
if (!isCancelled) {
setError(error.message);
setError(
error instanceof Error ? error.message : `unknown error: ${error}`
);
setIsDeleting(false);
}
}

View File

@@ -20,6 +20,9 @@ describe("useFetchGet", () => {
fetchMock.mock("http://localhost/error", {
throws: new TypeError("failed to fetch"),
});
fetchMock.mock("http://localhost/unknown", {
throws: "foo",
});
});
beforeEach(() => {
@@ -207,6 +210,27 @@ describe("useFetchGet", () => {
expect(result.current.isRetrying).toBe(false);
});
it("error is updated after unknown error", async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useFetchGet<string>("http://localhost/unknown")
);
expect(result.current.response).toBe(null);
expect(result.current.error).toBe(null);
expect(result.current.isLoading).toBe(true);
expect(result.current.isRetrying).toBe(false);
for (let i = 0; i <= FetchRetryConfig.retries; i++) {
jest.runOnlyPendingTimers();
await waitForNextUpdate();
}
expect(result.current.response).toBe(null);
expect(result.current.error).toBe("unknown error: foo");
expect(result.current.isLoading).toBe(false);
expect(result.current.isRetrying).toBe(false);
});
it("error is updated on uparsable JSON", async () => {
fetchMock.mock("http://localhost/json/invalid", {
headers: { "Content-Type": "application/json" },

View File

@@ -119,7 +119,8 @@ const useFetchGet = <T>(
} catch (error) {
setResponse((r) => ({
...r,
error: error.message,
error:
error instanceof Error ? error.message : `unknown error: ${error}`,
isLoading: false,
isRetrying: false,
}));