Repository navigation
Expand file tree
/
Copy pathchunkReloadGuard.test.ts
More file actions
56 lines (46 loc) · 1.73 KB
/
Copy pathchunkReloadGuard.test.ts
File metadata and controls
56 lines (46 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { describe, expect, it, vi } from "vite-plus/test";
import { clearChunkReloadGuard, reloadOnceForChunkLoadError } from "./chunkReloadGuard";
function createStorageStub(): Storage {
const store = new Map<string, string>();
return {
getItem: (key) => store.get(key) ?? null,
setItem: (key, value) => {
store.set(key, value);
},
removeItem: (key) => {
store.delete(key);
},
clear: () => store.clear(),
key: (index) => [...store.keys()][index] ?? null,
get length() {
return store.size;
},
};
}
describe("reloadOnceForChunkLoadError", () => {
it("reloads on the first failure and lets the second one surface", () => {
const storage = createStorageStub();
const reload = vi.fn();
expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(true);
expect(reload).toHaveBeenCalledTimes(1);
expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(false);
expect(reload).toHaveBeenCalledTimes(1);
});
it("reloads again after a successful boot cleared the guard", () => {
const storage = createStorageStub();
const reload = vi.fn();
reloadOnceForChunkLoadError(() => storage, reload);
clearChunkReloadGuard(() => storage);
expect(reloadOnceForChunkLoadError(() => storage, reload)).toBe(true);
expect(reload).toHaveBeenCalledTimes(2);
});
it("never reloads when storage is blocked, so a persistent failure cannot loop", () => {
const reload = vi.fn();
const blocked = () => {
throw new DOMException("blocked", "SecurityError");
};
expect(reloadOnceForChunkLoadError(blocked, reload)).toBe(false);
expect(reload).not.toHaveBeenCalled();
expect(() => clearChunkReloadGuard(blocked)).not.toThrow();
});
});