Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test:add message-box component unit test #151

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions apps/client/composables/messageBox/tests/modal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { mount, type VueWrapper } from "@vue/test-utils";
import { type IMessageBoxProps } from "../modal";
import MessageBoxVue from "~/components/main/MessageBox.vue";

function mockProps(isShowModal: boolean): IMessageBoxProps {
return {
isShowModal,
content: "content",
title: "title",
confirmBtnText: "confirm",
cancelBtnText: "cancel",
};
}
let wrapper: VueWrapper<any>;
function mockWrapper() {
return mount(MessageBoxVue, {
props: {
...mockProps(true),
"onUpdate:isShowModal": (e: any) => wrapper.setProps({ isShowModal: e }),
},
});
}

describe("message box modal", () => {
beforeEach(() => {
wrapper = mockWrapper();
});
afterEach(() => {
wrapper.unmount();
});

it("should have default props", () => {
const wrapper = mount(MessageBoxVue, {
props: {
isShowModal: true,
},
});
expect(wrapper.props()).toMatchInlineSnapshot(`
{
"cancelBtnText": "Cancel",
"confirmBtnText": "Confirm",
"content": "Are you sure?",
"isShowModal": true,
"title": "Tips",
}
`);
wrapper.unmount();
});

it("should open modal when isShowModal is true", () => {
const modalContainerAttributes = wrapper.find(".modal").attributes();

expect(modalContainerAttributes).toHaveProperty("open");
});

it("should close modal when cancel btn was clicked", async () => {
await wrapper.find("form > .btn").trigger("click");

expect(wrapper.emitted("update:isShowModal")?.[0][0]).toBe(false);
expect(wrapper.get(".modal").attributes()).not.toHaveProperty("open");
expect(wrapper.props("isShowModal")).toBe(false);
});

it("should close modal when confirm btn was clicked", async () => {
await wrapper.find(".modal-action > .btn").trigger("click");

expect(wrapper.get(".modal").attributes()).not.toHaveProperty("open");
expect(wrapper.props("isShowModal")).toBe(false);
});

it("should remove event listener when modal is closed", async () => {
vi.spyOn(document, "removeEventListener");
await wrapper.find(".modal-action > .btn").trigger("click");

expect(document.removeEventListener).toBeCalled();
expect(document.removeEventListener).toBeCalledTimes(1);
});
});
Loading