import { afterEach, expect, it } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { nextTick, defineComponent, h } from "vue"; import { ToastQueue, ToastProvider, Table, TableContent, TableHeader, TableColumn, TableColumnResizer, TableBody, TableRow, TableCell, } from "../../../packages/vue/src"; const wrappers: ReturnType[] = []; const queues: ToastQueue[] = []; afterEach(() => { wrappers.splice(0).forEach((w) => w.unmount()); queues.splice(0).forEach((q) => q.destroy()); document.body.innerHTML = ""; }); it("column minWidth constrains keyboard resizing", async () => { const w = mount( defineComponent({ render: () => h(Table, {}, () => h(TableContent, {}, () => h(TableHeader, {}, () => h(TableColumn, { minWidth: 160 }, () => h(TableColumnResizer)), ), ), ), }), ); wrappers.push(w); const th = w.get("th").element as HTMLElement; Object.defineProperty(th, "getBoundingClientRect", { value: () => ({ width: 160 }), }); await w .get('[data-slot="table-column-resizer"]') .trigger("keydown", { key: "ArrowLeft" }); expect(parseFloat(th.style.width)).toBeGreaterThanOrEqual(160); }); it("an unrelated pointer cancellation does not reset an active column drag", async () => { const w = mount( defineComponent({ render: () => h(Table, {}, () => h(TableContent, {}, () => h(TableHeader, {}, () => h(TableColumn, {}, () => h(TableColumnResizer)), ), ), ), }), ); wrappers.push(w); const th = w.get("th").element as HTMLElement; Object.defineProperty(th, "getBoundingClientRect", { value: () => ({ width: 100 }), }); const handle = w.get('[data-slot="table-column-resizer"]').element; const pointer = (type: string, id: number, x: number) => { const e = new MouseEvent(type, { bubbles: true, button: 0, clientX: x }); Object.defineProperty(e, "pointerId", { value: id }); handle.dispatchEvent(e); }; pointer("pointerdown", 1, 100); pointer("pointermove", 1, 120); await nextTick(); expect(th.style.width).toBe("120px"); pointer("pointercancel", 2, 120); await nextTick(); expect(th.style.width).toBe("120px"); }); it("Space selects the focused selectable table row", async () => { const w = mount( defineComponent({ render: () => h(Table, {}, () => h(TableContent, { selectionMode: "multiple" }, () => h(TableBody, {}, () => h(TableRow, { rowKey: "one", tabindex: 0 }, () => h(TableCell, {}, () => "One"), ), ), ), ), }), { attachTo: document.body }, ); wrappers.push(w); const row = w.get("tr"); (row.element as HTMLElement).focus(); expect(document.activeElement).toBe(row.element); await row.trigger("keydown", { key: " ", code: "Space" }); expect(row.attributes("aria-selected")).toBe("true"); }); it("default toast groups title and description in its content layout", () => { const q = new ToastQueue(); queues.push(q); q.add({ title: "Title", description: "Description" }, { timeout: 0 }); const w = mount(ToastProvider, { props: { queue: q } }); wrappers.push(w); expect( w.find('[data-slot="toast-content"] [data-slot="toast-title"]').exists(), ).toBe(true); expect( w .find('[data-slot="toast-content"] [data-slot="toast-description"]') .exists(), ).toBe(true); }); it("the provider exposed toast factory uses the current queue after replacement", async () => { const a = new ToastQueue({ exitDuration: 0 }), b = new ToastQueue({ exitDuration: 0 }); queues.push(a, b); const w = mount(ToastProvider, { props: { queue: a } }); wrappers.push(w); const api = w.vm as unknown as { toast: () => (message: string, options: { timeout: number }) => string; }; api.toast()("initial", { timeout: 0 }); await w.setProps({ queue: b }); api.toast()("current", { timeout: 0 }); await flushPromises(); expect(b.visibleToasts.map((t) => t.content.title)).toContain("current"); }); it("selection demo select-all checks the actual row controls", async () => { const mod = await import("../../../apps/docs/demos/table-selection.vue"); const w = mount(mod.default); wrappers.push(w); const controls = w.findAll('input[type="checkbox"]'); expect(controls.length).toBeGreaterThan(1); await controls[0].setValue(true); await flushPromises(); expect( controls.slice(1).every((c) => (c.element as HTMLInputElement).checked), ).toBe(true); });