Skip to content

Commit f1c544a

Browse files
claudeahmedhamidawan
authored andcommitted
add vitest tests for ConfirmDialog component and composable
1 parent 4bb3264 commit f1c544a

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { getLocalVue } from "@tests/vitest/helpers";
2+
import { mount } from "@vue/test-utils";
3+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
4+
5+
import ConfirmDialog from "./ConfirmDialog.vue";
6+
7+
const localVue = getLocalVue();
8+
9+
type ConfirmDialogVM = InstanceType<typeof ConfirmDialog>;
10+
11+
describe("ConfirmDialog", () => {
12+
let wrapper: ReturnType<typeof mount>;
13+
let vm: ConfirmDialogVM;
14+
15+
beforeEach(() => {
16+
wrapper = mount(ConfirmDialog as object, { localVue, attachTo: document.body });
17+
vm = wrapper.vm as unknown as ConfirmDialogVM;
18+
});
19+
20+
afterEach(() => {
21+
wrapper.destroy();
22+
document.body.innerHTML = "";
23+
});
24+
25+
it("resolves true when OK is clicked", async () => {
26+
const promise = vm.confirm("Are you sure?");
27+
await localVue.nextTick();
28+
await wrapper.find('[data-description="confirm dialog ok"]').trigger("click");
29+
expect(await promise).toBe(true);
30+
});
31+
32+
it("resolves false when Cancel is clicked", async () => {
33+
const promise = vm.confirm("Are you sure?");
34+
await localVue.nextTick();
35+
await wrapper.find('[data-description="confirm dialog cancel"]').trigger("click");
36+
expect(await promise).toBe(false);
37+
});
38+
39+
it("resolves false when dialog is closed externally", async () => {
40+
const promise = vm.confirm("Are you sure?");
41+
await localVue.nextTick();
42+
wrapper.find("dialog").element.dispatchEvent(new Event("close"));
43+
await localVue.nextTick();
44+
expect(await promise).toBe(false);
45+
});
46+
47+
it("renders message and respects custom options", async () => {
48+
vm.confirm("Delete this item?", { title: "Confirm deletion", okText: "Delete" });
49+
await localVue.nextTick();
50+
const dialogText = wrapper.find("dialog").text();
51+
expect(dialogText).toContain("Delete this item?");
52+
expect(dialogText).toContain("Confirm deletion");
53+
expect(dialogText).toContain("Delete");
54+
});
55+
56+
it("resolves first pending promise as false on concurrent call", async () => {
57+
const first = vm.confirm("First message");
58+
await localVue.nextTick();
59+
const second = vm.confirm("Second message");
60+
expect(await first).toBe(false);
61+
// resolve second cleanly
62+
await wrapper.find('[data-description="confirm dialog cancel"]').trigger("click");
63+
await second;
64+
});
65+
66+
it("resolves false when abort signal fires", async () => {
67+
const controller = new AbortController();
68+
const promise = vm.confirm("Are you sure?", { signal: controller.signal });
69+
await localVue.nextTick();
70+
controller.abort();
71+
expect(await promise).toBe(false);
72+
});
73+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createLocalVue, mount } from "@vue/test-utils";
2+
import { afterEach, describe, expect, it } from "vitest";
3+
import { defineComponent } from "vue";
4+
5+
import type ConfirmDialogComponent from "@/components/ConfirmDialog.vue";
6+
7+
import { type ConfirmDialogOptions, setConfirmDialogComponentRef, useConfirmDialog } from "./confirmDialog";
8+
9+
type ConfirmDialogInstance = InstanceType<typeof ConfirmDialogComponent>;
10+
11+
const localVue = createLocalVue();
12+
13+
describe("useConfirmDialog", () => {
14+
afterEach(() => {
15+
setConfirmDialogComponentRef(null);
16+
});
17+
18+
it("cancels pending confirm when caller component unmounts", async () => {
19+
setConfirmDialogComponentRef({
20+
confirm: (_msg: string, options: ConfirmDialogOptions) =>
21+
new Promise<boolean>((resolve) => {
22+
options.signal?.addEventListener("abort", () => resolve(false), { once: true });
23+
}),
24+
} as ConfirmDialogInstance);
25+
26+
const CallerComponent = defineComponent({
27+
setup() {
28+
const { confirm } = useConfirmDialog();
29+
return { confirm };
30+
},
31+
template: "<div />",
32+
});
33+
34+
const wrapper = mount(CallerComponent as object, { localVue });
35+
const vm = wrapper.vm as typeof wrapper.vm & ReturnType<typeof useConfirmDialog>;
36+
37+
const promise = vm.confirm("Are you sure?");
38+
39+
wrapper.destroy(); // triggers onUnmounted → controller.abort()
40+
41+
expect(await promise).toBe(false);
42+
});
43+
});

0 commit comments

Comments
 (0)