Skip to content

Commit

Permalink
✅ export profile exception test
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Jan 8, 2025
1 parent 9b3f157 commit 5160901
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/front-end/app/views/home/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("Profile", () => {
});

afterEach(() => {
vi.unstubAllGlobals();
profile.vm.$store.unregisterModule("test");
profile.unmount();
});
Expand All @@ -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");
Expand All @@ -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) => {
Expand Down

0 comments on commit 5160901

Please sign in to comment.