test(frontend/copilot): cover localStorage initialiser branches with resetModules

The copilotChatMode and copilotLlmModel store fields are initialised from
localStorage via IIFEs that run once at module-load time. The existing tests
reset state with setState() which bypasses the initialiser, leaving the 'fast'
and 'advanced' branches uncovered for codecov patch.

Use vi.resetModules() + dynamic import to get a fresh store instance with
pre-populated localStorage, covering both initialiser branches.
This commit is contained in:
majdyz
2026-04-15 15:10:34 +07:00
parent b589b37897
commit 432202c259

View File

@@ -1,4 +1,4 @@
import { describe, expect, it, beforeEach, vi } from "vitest";
import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
import { useCopilotUIStore } from "../store";
vi.mock("@sentry/nextjs", () => ({
@@ -243,3 +243,24 @@ describe("useCopilotUIStore", () => {
});
});
});
describe("useCopilotUIStore localStorage initialisation", () => {
afterEach(() => {
vi.resetModules();
window.localStorage.clear();
});
it("reads fast chat mode from localStorage on store creation", async () => {
window.localStorage.setItem("copilot-mode", "fast");
vi.resetModules();
const { useCopilotUIStore: fresh } = await import("../store");
expect(fresh.getState().copilotChatMode).toBe("fast");
});
it("reads advanced model from localStorage on store creation", async () => {
window.localStorage.setItem("copilot-model", "advanced");
vi.resetModules();
const { useCopilotUIStore: fresh } = await import("../store");
expect(fresh.getState().copilotLlmModel).toBe("advanced");
});
});