Skip to content

Commit 80c5742

Browse files
refactor confirm dialog tests to avoid vm access for Vue 3 compatibility
1 parent 4ace8ca commit 80c5742

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

client/src/components/ConfirmDialog.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,44 @@ import { mount } from "@vue/test-utils";
22
import { afterEach, beforeEach, describe, expect, it } from "vitest";
33
import { nextTick } from "vue";
44

5-
import ConfirmDialog from "./ConfirmDialog.vue";
5+
import { ConfirmDialog as confirmService, setConfirmDialogComponentRef } from "@/composables/confirmDialog";
66

7-
type ConfirmDialogVM = InstanceType<typeof ConfirmDialog>;
7+
import ConfirmDialogComponent from "./ConfirmDialog.vue";
88

99
describe("ConfirmDialog", () => {
1010
let wrapper: ReturnType<typeof mount>;
11-
let vm: ConfirmDialogVM;
1211

1312
beforeEach(() => {
14-
wrapper = mount(ConfirmDialog as object, { attachTo: document.body });
15-
vm = wrapper.vm as unknown as ConfirmDialogVM;
13+
wrapper = mount(ConfirmDialogComponent as object, { attachTo: document.body });
14+
setConfirmDialogComponentRef(wrapper.vm as unknown as InstanceType<typeof ConfirmDialogComponent>);
1615
});
1716

1817
afterEach(() => {
18+
setConfirmDialogComponentRef(null);
1919
wrapper.destroy();
2020
document.body.innerHTML = "";
2121
});
2222

2323
it("resolves true when OK is clicked", async () => {
24-
const promise = vm.confirm("Are you sure?");
24+
const promise = confirmService.confirm("Are you sure?");
2525
await wrapper.find('[data-description="confirm dialog ok"]').trigger("click");
2626
expect(await promise).toBe(true);
2727
});
2828

2929
it("resolves false when Cancel is clicked", async () => {
30-
const promise = vm.confirm("Are you sure?");
30+
const promise = confirmService.confirm("Are you sure?");
3131
await wrapper.find('[data-description="confirm dialog cancel"]').trigger("click");
3232
expect(await promise).toBe(false);
3333
});
3434

3535
it("resolves null when dialog is dismissed (closed without choosing)", async () => {
36-
const promise = vm.confirm("Are you sure?");
36+
const promise = confirmService.confirm("Are you sure?");
3737
wrapper.find("dialog").element.dispatchEvent(new Event("close"));
3838
expect(await promise).toBe(null);
3939
});
4040

4141
it("renders message and respects custom options", async () => {
42-
vm.confirm("Delete this item?", { title: "Confirm deletion", okText: "Delete" });
42+
confirmService.confirm("Delete this item?", { title: "Confirm deletion", okText: "Delete" });
4343
await nextTick();
4444
const dialogText = wrapper.find("dialog").text();
4545
expect(dialogText).toContain("Delete this item?");
@@ -48,8 +48,8 @@ describe("ConfirmDialog", () => {
4848
});
4949

5050
it("resolves first pending promise as false on concurrent call", async () => {
51-
const first = vm.confirm("First message");
52-
const second = vm.confirm("Second message");
51+
const first = confirmService.confirm("First message");
52+
const second = confirmService.confirm("Second message");
5353
expect(await first).toBe(false);
5454
// resolve second cleanly
5555
await wrapper.find('[data-description="confirm dialog cancel"]').trigger("click");
@@ -58,7 +58,7 @@ describe("ConfirmDialog", () => {
5858

5959
it("resolves false when abort signal fires", async () => {
6060
const controller = new AbortController();
61-
const promise = vm.confirm("Are you sure?", { signal: controller.signal });
61+
const promise = confirmService.confirm("Are you sure?", { signal: controller.signal });
6262
controller.abort();
6363
expect(await promise).toBe(false);
6464
});

client/src/composables/confirmDialog.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ describe("useConfirmDialog", () => {
2323
}),
2424
} as ConfirmDialogInstance);
2525

26+
let confirm: ReturnType<typeof useConfirmDialog>["confirm"] = () => Promise.resolve(null);
27+
2628
const CallerComponent = defineComponent({
2729
setup() {
28-
const { confirm } = useConfirmDialog();
29-
return { confirm };
30+
({ confirm } = useConfirmDialog());
31+
return {};
3032
},
3133
template: "<div />",
3234
});
3335

3436
const wrapper = mount(CallerComponent as object, { localVue });
35-
const vm = wrapper.vm as typeof wrapper.vm & ReturnType<typeof useConfirmDialog>;
3637

37-
const promise = vm.confirm("Are you sure?");
38+
const promise = confirm("Are you sure?");
3839

3940
wrapper.destroy(); // triggers onUnmounted → controller.abort()
4041

0 commit comments

Comments
 (0)