From 5160901241193e35afac1bcccc7eda5518a52370 Mon Sep 17 00:00:00 2001 From: haliphax Date: Wed, 8 Jan 2025 13:56:26 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20export=20profile=20exception=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/front-end/app/views/home/profile.test.ts | 58 ++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/front-end/app/views/home/profile.test.ts b/src/front-end/app/views/home/profile.test.ts index 1ee2f41..cd61eba 100644 --- a/src/front-end/app/views/home/profile.test.ts +++ b/src/front-end/app/views/home/profile.test.ts @@ -14,6 +14,7 @@ describe("Profile", () => { }); afterEach(() => { + vi.unstubAllGlobals(); profile.vm.$store.unregisterModule("test"); profile.unmount(); }); @@ -35,6 +36,26 @@ describe("Profile", () => { expect(profile.vm.$store.state.session.name).toBe("test"); }); + it("exports session data", async ({ expect }) => { + const { mockAnchor } = vi.hoisted(() => ({ + mockAnchor: { + href: "", + download: "", + click: vi.fn(), + }, + })); + vi.stubGlobal("document", { createElement: () => mockAnchor }); + + profile + .findAll("button") + .filter((v) => v.text().includes("Export"))[0] + .trigger("click"); + await profile.vm.$nextTick(); + + expect(mockAnchor.href).toContain("data:application/json,"); + expect(mockAnchor.download).toBe("narf.json"); + }); + describe("import", () => { const makeFileList = (files: File[]) => { const input = document.createElement("input"); @@ -50,6 +71,43 @@ describe("Profile", () => { return fileList; }; + it("raises alert on import exception", async ({ expect }) => { + vi.stubGlobal("JSON", { + parse() { + throw new Error(); + }, + }); + let alerted = false; + store.subscribeAction((o) => { + if (o.type !== "alert" || !o.payload.text?.includes("Error: ")) return; + alerted = true; + }); + + const fileInput = profile.get("ul").get("input").element; + fileInput.files = makeFileList([ + new File([""], "test1.json", { type: "application/json" }), + ]); + fileInput.dispatchEvent(new Event("change")); + await profile.vm.$nextTick(); + + expect(alerted).toBe(true); + }); + + it("clicks files button on user's behalf", async ({ expect }) => { + let clicked = false; + profile + .findAll("input") + .filter((v) => (v.attributes() as { type?: string }).type === "file")[0] + .element.addEventListener("click", () => (clicked = true)); + + profile + .findAll("button") + .filter((v) => v.text().includes("Import"))[0] + .trigger("click"); + + expect(clicked).toBe(true); + }); + it("throws error if multiple files selected", async ({ expect }) => { let alertMessage = ""; profile.vm.$store.subscribeAction((o) => {