import { describe, test, expect, afterEach } from "vitest"; import { enableAutoUnmount, mount } from "@vue/test-utils"; import { setTimeout } from "timers/promises"; import OLoading from "@/components/loading/Loading.vue"; describe("OLoading tests", () => { enableAutoUnmount(afterEach); test("render correctly", () => { const wrapper = mount(OLoading, { props: { active: true }, }); expect(!!wrapper.vm).toBeTruthy(); expect(wrapper.exists()).toBeTruthy(); expect(wrapper.html()).toMatchSnapshot(); const element = wrapper.find("div"); expect(element.attributes("data-oruga")).toBe("loading"); }); test("render label slot correctly", () => { const labelHTML = `

My Label

`; const wrapper = mount(OLoading, { props: { active: true }, slots: { default: `

My Label

` }, }); const label = wrapper.find(".label"); expect(label.html()).toBe(labelHTML); expect(label.text()).toBe("My Label"); }); test("react accordingly when has fullPage", () => { const wrapper = mount(OLoading, { props: { active: true, fullPage: true }, }); const container = wrapper.find('[data-oruga="loading"]'); expect(container.classes("o-loading--fullpage")).toBeTruthy(); }); describe("verify trigger", () => { test("react accordingly when pressing escape with close on escape", async () => { const wrapper = mount(OLoading, { props: { active: true, closeOnEscape: true }, attachTo: document.body, }); await setTimeout(); // await event handler get set const container = wrapper.find('[data-oruga="loading"]'); expect(container.exists()).toBeTruthy(); expect(container.isVisible()).toBeTruthy(); await container.trigger("keyup", { key: "Esc" }); expect(wrapper.find('[data-oruga="loading"]').exists()).toBeFalsy(); expect(wrapper.emitted("close")).toHaveLength(1); const activeEmits = wrapper.emitted("update:active"); expect(activeEmits).toHaveLength(1); expect(activeEmits?.[0][0]).toBeFalsy(); }); test("react accordingly when pressing escape with close on escape false", async () => { const wrapper = mount(OLoading, { props: { active: true, closeOnEscape: false }, }); await setTimeout(); // await event handler get set const container = wrapper.find('[data-oruga="loading"]'); expect(container.exists()).toBeTruthy(); expect(wrapper.isVisible()).toBeTruthy(); await wrapper.trigger("keyup", { key: "Esc" }); expect( wrapper.find('[data-oruga="loading"]').exists(), ).toBeTruthy(); expect(wrapper.emitted("close")).toBeUndefined(); expect(wrapper.emitted("update:active")).toBeUndefined(); }); test("react accordingly when clicking outside with close on outside", async () => { const wrapper = mount(OLoading, { props: { active: true, closeOnOutside: true }, attachTo: document.body, }); await setTimeout(); // await event handler get set const overlay = wrapper.find(".o-loading__overlay"); expect(overlay.exists()).toBeTruthy(); expect(overlay.isVisible()).toBeTruthy(); // check Do close on click outside await overlay.trigger("click"); expect(wrapper.find(".o-loading__overlay").exists()).toBeFalsy(); expect(wrapper.emitted("close")).toHaveLength(1); const activeEmits = wrapper.emitted("update:active"); expect(activeEmits).toHaveLength(1); expect(activeEmits?.[0][0]).toBeFalsy(); }); test("react accordingly when clicking outside with close on outside false", async () => { const wrapper = mount(OLoading, { props: { active: true, closeOnOutside: false }, attachTo: document.body, }); await setTimeout(); // await event handler get set const overlay = wrapper.find(".o-loading__overlay"); expect(overlay.exists()).toBeTruthy(); expect(overlay.isVisible()).toBeTruthy(); // check Do close on click outside await overlay.trigger("click"); expect(wrapper.find(".o-loading__overlay").exists()).toBeTruthy(); expect(wrapper.emitted("close")).toBeUndefined(); expect(wrapper.emitted("update:active")).toBeUndefined(); }); }); describe("accessible name", () => { test("without a slot the label is rendered and no aria-label is set", () => { const wrapper = mount(OLoading, { props: { active: true, label: "Loading data" }, }); expect(wrapper.find(".o-loading__label").text()).toBe( "Loading data", ); expect(wrapper.attributes("aria-label")).toBeUndefined(); }); test("with the default slot the label names the status", () => { const wrapper = mount(OLoading, { props: { active: true, label: "Loading data" }, slots: { default: "custom" }, }); const div = wrapper.find('[data-oruga="loading"]'); expect(div.exists()).toBeTruthy(); expect(div.attributes("role")).toBe("status"); expect(div.attributes("aria-label")).toBe("Loading data"); expect(div.find(".o-loading__label").exists()).toBe(false); }); }); });