|
| 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 | +}); |
0 commit comments