Files
MeshChatX/tests/frontend/ConfirmDialog.test.js
Sudo-Ivan 39b8d5fef8
Some checks failed
Build Test / Build and Test (push) Failing after 6m1s
CI / lint (push) Failing after 1m27s
CI / build-frontend (push) Failing after 1m30s
CI / test-backend (push) Successful in 1m17s
CI / test-lang (push) Failing after 1m15s
Build and Publish Docker Image / build (push) Failing after 4m28s
Build and Publish Docker Image / build-dev (push) Failing after 5m38s
Security Scans / scan (push) Successful in 2m12s
Tests / test (push) Failing after 1m17s
Cleanup
2026-02-17 17:59:46 -06:00

79 lines
3.1 KiB
JavaScript

import { mount } from "@vue/test-utils";
import { describe, it, expect, vi, beforeEach } from "vitest";
import ConfirmDialog from "../../meshchatx/src/frontend/components/ConfirmDialog.vue";
vi.mock("../../meshchatx/src/frontend/js/GlobalEmitter", () => ({
default: { on: vi.fn(), off: vi.fn(), emit: vi.fn() },
}));
import GlobalEmitter from "../../meshchatx/src/frontend/js/GlobalEmitter";
const MaterialDesignIcon = { template: '<div class="mdi"></div>', props: ["iconName"] };
function mountDialog() {
return mount(ConfirmDialog, {
global: { components: { MaterialDesignIcon } },
});
}
describe("ConfirmDialog UI", () => {
beforeEach(() => {
vi.mocked(GlobalEmitter.on).mockClear();
vi.mocked(GlobalEmitter.off).mockClear();
});
it("registers confirm listener on mount", () => {
mountDialog();
expect(GlobalEmitter.on).toHaveBeenCalledWith("confirm", expect.any(Function));
});
it("does not show dialog when pendingConfirm is null", () => {
const wrapper = mountDialog();
expect(wrapper.vm.pendingConfirm).toBeNull();
expect(wrapper.find(".fixed.inset-0").exists()).toBe(false);
});
it("shows dialog with message when show is called", async () => {
const wrapper = mountDialog();
const showFn = GlobalEmitter.on.mock.calls.find((c) => c[0] === "confirm")?.[1];
expect(showFn).toBeDefined();
showFn({ message: "Delete this item?", resolve: vi.fn() });
await wrapper.vm.$nextTick();
expect(wrapper.vm.pendingConfirm).toEqual({ message: "Delete this item?" });
expect(wrapper.text()).toContain("Confirm Action");
expect(wrapper.text()).toContain("Delete this item?");
});
it("has Cancel and Confirm buttons when visible", async () => {
const wrapper = mountDialog();
const showFn = GlobalEmitter.on.mock.calls.find((c) => c[0] === "confirm")?.[1];
showFn({ message: "Sure?", resolve: vi.fn() });
await wrapper.vm.$nextTick();
expect(wrapper.text()).toContain("Cancel");
expect(wrapper.text()).toContain("Confirm");
});
it("calls resolve(true) and clears when Confirm clicked", async () => {
const resolve = vi.fn();
const wrapper = mountDialog();
const showFn = GlobalEmitter.on.mock.calls.find((c) => c[0] === "confirm")?.[1];
showFn({ message: "Sure?", resolve });
await wrapper.vm.$nextTick();
await wrapper.find("button.bg-red-600").trigger("click");
expect(resolve).toHaveBeenCalledWith(true);
expect(wrapper.vm.pendingConfirm).toBeNull();
});
it("calls resolve(false) when Cancel clicked", async () => {
const resolve = vi.fn();
const wrapper = mountDialog();
const showFn = GlobalEmitter.on.mock.calls.find((c) => c[0] === "confirm")?.[1];
showFn({ message: "Sure?", resolve });
await wrapper.vm.$nextTick();
const cancelBtn = wrapper.findAll("button").find((b) => b.text() === "Cancel");
await cancelBtn.trigger("click");
expect(resolve).toHaveBeenCalledWith(false);
expect(wrapper.vm.pendingConfirm).toBeNull();
});
});