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();
});
});