Files
heroui-vue/tests/m35-m36-review-5.probe.test.ts
T

118 lines
3.7 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h } from "vue";
import {
Table,
TableContent,
TableHeader,
TableColumn,
TableColumnResizer,
TableBody,
TableRow,
TableCell,
ToastProvider,
ToastQueue,
} from "../packages/vue/src";
const wrappers: ReturnType<typeof mount>[] = [];
const queues: ToastQueue[] = [];
afterEach(() => {
wrappers.splice(0).forEach((w) => w.unmount());
queues.splice(0).forEach((q) => q.destroy());
if (vi.isFakeTimers()) vi.runOnlyPendingTimers();
vi.useRealTimers();
document.body.innerHTML = "";
});
it("column defaultWidth is applied to its rendered column", () => {
const w = mount(
defineComponent({
render: () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(TableHeader, {}, () =>
h(TableColumn, { defaultWidth: 180 }, () => "Name"),
),
),
),
}),
);
wrappers.push(w);
expect((w.get("th").element as HTMLElement).style.width).toBe("180px");
});
it("a selectable table supplies a keyboard focus entry without caller tabindex", () => {
const w = mount(
defineComponent({
render: () =>
h(Table, {}, () =>
h(TableContent, { selectionMode: "multiple" }, () =>
h(TableBody, {}, () =>
h(TableRow, { rowKey: "one" }, () =>
h(TableCell, {}, () => "One"),
),
),
),
),
}),
);
wrappers.push(w);
expect(w.findAll('[tabindex="0"]').length).toBeGreaterThan(0);
});
it("unmount during an active column drag releases capture", () => {
const w = mount(
defineComponent({
render: () =>
h(Table, {}, () =>
h(TableContent, {}, () =>
h(TableHeader, {}, () =>
h(TableColumn, {}, () => h(TableColumnResizer)),
),
),
),
}),
);
const handle = w.get('[data-slot="table-column-resizer"]')
.element as HTMLElement;
const release = vi.fn();
Object.defineProperty(handle, "setPointerCapture", { value: vi.fn() });
Object.defineProperty(handle, "releasePointerCapture", { value: release });
const th = w.get("th").element;
Object.defineProperty(th, "getBoundingClientRect", {
value: () => ({ width: 100 }),
});
for (const [type, x] of [
["pointerdown", 100],
["pointermove", 120],
] as const) {
const e = new MouseEvent(type, { bubbles: true, button: 0, clientX: x });
Object.defineProperty(e, "pointerId", { value: 7 });
handle.dispatchEvent(e);
}
w.unmount();
expect(release).toHaveBeenCalledWith(7);
});
it("default action respects the caller's disabled state", () => {
const q = new ToastQueue();
queues.push(q);
q.add(
{ title: "Action", actionProps: { label: "Blocked", isDisabled: true } },
{ timeout: 0 },
);
const w = mount(ToastProvider, { props: { queue: q } });
wrappers.push(w);
const button = w.findAll("button").find((b) => b.text() === "Blocked")!;
expect((button.element as HTMLButtonElement).disabled).toBe(true);
});
it("the default toast demo escapes the containing preview", async () => {
vi.useFakeTimers();
const mod = await import("../apps/docs/demos/toast-default.vue");
const host = document.createElement("div");
host.className = "component-preview-container";
document.body.append(host);
const w = mount(mod.default, { attachTo: host });
wrappers.push(w);
await w.get("button").trigger("click");
await flushPromises();
const region = document.querySelector('[data-slot="toast-region"]')!;
expect(region).not.toBeNull();
expect(region.closest(".component-preview-container")).toBeNull();
});