-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): try to reload when specified error occurs
INFRA-0
- Loading branch information
1 parent
a66abb0
commit c91b3d2
Showing
12 changed files
with
236 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HttpFetchError } from "@next-core/http"; | ||
import { BrickLoadError } from "@next-core/loader"; | ||
import { isNetworkError } from "./isNetworkError.js"; | ||
|
||
describe("isNetworkError", () => { | ||
it("should return true for BrickLoadError", () => { | ||
const error = new BrickLoadError("Brick load error"); | ||
expect(isNetworkError(error)).toBe(true); | ||
}); | ||
|
||
it("should return true for HttpFetchError", () => { | ||
const error = new HttpFetchError("Http fetch error"); | ||
expect(isNetworkError(error)).toBe(true); | ||
}); | ||
|
||
it("should return true for ChunkLoadError", () => { | ||
const error = new Error("Chunk load error"); | ||
error.name = "ChunkLoadError"; | ||
expect(isNetworkError(error)).toBe(true); | ||
}); | ||
|
||
it("should return true for Event with error type and HTMLScriptElement target", () => { | ||
const scriptElement = document.createElement("script"); | ||
const event = new Event("error"); | ||
Object.defineProperty(event, "target", { value: scriptElement }); | ||
expect(isNetworkError(event)).toBe(true); | ||
}); | ||
|
||
it("should return false for other errors", () => { | ||
const error = new Error("Some other error"); | ||
expect(isNetworkError(error)).toBe(false); | ||
}); | ||
|
||
it("should return false for null or undefined", () => { | ||
expect(isNetworkError(null)).toBe(false); | ||
expect(isNetworkError(undefined)).toBe(false); | ||
}); | ||
|
||
it("should return false for non-error objects", () => { | ||
const error = { message: "Not an error instance" }; | ||
expect(isNetworkError(error)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { HttpFetchError } from "@next-core/http"; | ||
import { BrickLoadError } from "@next-core/loader"; | ||
|
||
export function isNetworkError(error: unknown): boolean { | ||
return ( | ||
!!error && | ||
(error instanceof BrickLoadError || | ||
error instanceof HttpFetchError || | ||
(error instanceof Error && error.name === "ChunkLoadError") || | ||
(error instanceof Event && | ||
error.type === "error" && | ||
error.target instanceof HTMLScriptElement)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { shouldReloadForError } from "./shouldReloadForError.js"; | ||
import { BrickLoadError } from "@next-core/loader"; | ||
|
||
const mockGetItem = jest.spyOn(Storage.prototype, "getItem"); | ||
const mockSetItem = jest.spyOn(Storage.prototype, "setItem"); | ||
const mockRemoveItem = jest.spyOn(Storage.prototype, "removeItem"); | ||
|
||
describe("shouldReloadForError", () => { | ||
beforeEach(() => { | ||
Object.defineProperty(navigator, "userAgent", { | ||
value: "upchat", | ||
configurable: true, | ||
}); | ||
mockGetItem.mockReturnValue(null); | ||
}); | ||
|
||
it("should reload if error is a network error and count is less than MAX_RELOAD_COUNT", () => { | ||
const result = shouldReloadForError(new BrickLoadError("Network error")); | ||
|
||
expect(result).toBe(true); | ||
expect(mockSetItem).toHaveBeenCalledWith("reload-for-error-count", "1"); | ||
}); | ||
|
||
it("should not reload if count is equal to MAX_RELOAD_COUNT", () => { | ||
mockGetItem.mockReturnValue("2"); | ||
|
||
const result = shouldReloadForError(new BrickLoadError("Network error")); | ||
|
||
expect(result).toBe(false); | ||
expect(mockSetItem).not.toHaveBeenCalled(); | ||
expect(mockRemoveItem).toHaveBeenCalledWith("reload-for-error-count"); | ||
}); | ||
|
||
it("should not reload if userAgent does not match", () => { | ||
Object.defineProperty(navigator, "userAgent", { | ||
value: "Chrome", | ||
configurable: true, | ||
}); | ||
|
||
const result = shouldReloadForError(new BrickLoadError("Network error")); | ||
|
||
expect(result).toBe(false); | ||
expect(mockSetItem).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not reload if error is not a network error", () => { | ||
const result = shouldReloadForError(new Error("Other error")); | ||
|
||
expect(result).toBe(false); | ||
expect(mockSetItem).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.