chore: remove legacy SSR checks and consolidate migration maintenance
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h, ref } from "vue";
|
||||
import {
|
||||
Table,
|
||||
TableContent,
|
||||
TableBody,
|
||||
TableCollection,
|
||||
TableRow,
|
||||
TableCell,
|
||||
ToastProvider,
|
||||
ToastQueue,
|
||||
createToastFunction,
|
||||
ToastContent,
|
||||
ToastTitle,
|
||||
ToastDescription,
|
||||
} from "../../../packages/vue/src";
|
||||
const cleanup: (() => void)[] = [];
|
||||
afterEach(() => {
|
||||
cleanup
|
||||
.splice(0)
|
||||
.reverse()
|
||||
.forEach((fn) => fn());
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
it("TableBody preserves a populated TableCollection when #empty is supplied", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{},
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
TableCollection,
|
||||
{ items: ["A"] },
|
||||
{
|
||||
default: ({ item }: { item: string }) =>
|
||||
h(TableRow, { rowKey: item }, () =>
|
||||
h(TableCell, {}, () => item),
|
||||
),
|
||||
},
|
||||
),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(false);
|
||||
});
|
||||
it("Toast root follows explicit Title and Description ids", async () => {
|
||||
const q = new ToastQueue();
|
||||
cleanup.push(() => q.destroy());
|
||||
const toast = createToastFunction(q);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
ToastProvider,
|
||||
{ queue: q, to: "body" },
|
||||
{
|
||||
default: () =>
|
||||
h(ToastContent, {}, () => [
|
||||
h(ToastTitle, { id: "custom-title" }, () => "Title"),
|
||||
h(
|
||||
ToastDescription,
|
||||
{ id: "custom-description" },
|
||||
() => "Description",
|
||||
),
|
||||
]),
|
||||
},
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
toast("Title", { timeout: 0 });
|
||||
await flushPromises();
|
||||
const root = document.querySelector('[data-slot="toast"]')!;
|
||||
expect(root.getAttribute("aria-labelledby")).toBe("custom-title");
|
||||
expect(root.getAttribute("aria-describedby")).toBe("custom-description");
|
||||
});
|
||||
it("title-only Toast does not reference a nonexistent description", async () => {
|
||||
const q = new ToastQueue();
|
||||
cleanup.push(() => q.destroy());
|
||||
const toast = createToastFunction(q);
|
||||
const w = mount(ToastProvider, {
|
||||
props: { queue: q, to: "body" },
|
||||
attachTo: document.body,
|
||||
});
|
||||
cleanup.push(() => w.unmount());
|
||||
toast("Title only", { timeout: 0 });
|
||||
await flushPromises();
|
||||
const root = document.querySelector('[data-slot="toast"]')!;
|
||||
const refs = (root.getAttribute("aria-describedby") ?? "")
|
||||
.split(/\s+/)
|
||||
.filter(Boolean);
|
||||
for (const id of refs) expect(document.getElementById(id)).not.toBeNull();
|
||||
});
|
||||
|
||||
it("Toast part ids react to later id attribute changes", async () => {
|
||||
const title = ref("title-before");
|
||||
const description = ref("description-before");
|
||||
const q = new ToastQueue();
|
||||
cleanup.push(() => q.destroy());
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(
|
||||
ToastProvider,
|
||||
{ queue: q, to: "body" },
|
||||
{
|
||||
default: () =>
|
||||
h(ToastContent, {}, () => [
|
||||
h(ToastTitle, { id: title.value }, () => "Title"),
|
||||
h(
|
||||
ToastDescription,
|
||||
{ id: description.value },
|
||||
() => "Description",
|
||||
),
|
||||
]),
|
||||
},
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
createToastFunction(q)("Title", { timeout: 0 });
|
||||
await flushPromises();
|
||||
title.value = "title-after";
|
||||
description.value = "description-after";
|
||||
await flushPromises();
|
||||
const root = document.querySelector('[data-slot="toast"]')!;
|
||||
expect(root.querySelector('[data-slot="toast-title"]')?.id).toBe(
|
||||
"title-after",
|
||||
);
|
||||
expect(root.getAttribute("aria-labelledby")).toBe("title-after");
|
||||
expect(root.querySelector('[data-slot="toast-description"]')?.id).toBe(
|
||||
"description-after",
|
||||
);
|
||||
expect(root.getAttribute("aria-describedby")).toBe("description-after");
|
||||
});
|
||||
|
||||
it("wrapped collection transitions back to empty without swallowing its future rows", async () => {
|
||||
const items = ref<string[]>(["A"]);
|
||||
const Rows = defineComponent({
|
||||
name: "Rows",
|
||||
setup: () => () =>
|
||||
h(
|
||||
TableCollection,
|
||||
{ items: items.value },
|
||||
{
|
||||
default: ({ item }: { item: string }) =>
|
||||
h(TableRow, { rowKey: item }, () => h(TableCell, {}, () => item)),
|
||||
},
|
||||
),
|
||||
});
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{},
|
||||
{
|
||||
default: () => h(Rows),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
|
||||
items.value = [];
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
items.value = ["B"];
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(false);
|
||||
});
|
||||
|
||||
it("forced empty state does not expose existing data rows", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{ empty: true },
|
||||
{
|
||||
default: () =>
|
||||
h(TableRow, { rowKey: "A" }, () =>
|
||||
h(TableCell, {}, () => "A"),
|
||||
),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("observes native rows rendered by an anonymous wrapper as data changes", async () => {
|
||||
const items = ref<string[]>([]);
|
||||
const Rows = () =>
|
||||
items.value.map((item) => h("tr", { key: item }, [h("td", item)]));
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{},
|
||||
{
|
||||
default: () => h(Rows),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
items.value = ["A"];
|
||||
await flushPromises();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(false);
|
||||
expect(w.find("tbody").text()).toBe("A");
|
||||
items.value = [];
|
||||
await flushPromises();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
});
|
||||
|
||||
it("switches forced empty off and restores the data row", async () => {
|
||||
const empty = ref(false);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, { selectionMode: "multiple" }, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{ empty: empty.value },
|
||||
{
|
||||
default: () =>
|
||||
h(TableRow, { rowKey: "A" }, () =>
|
||||
h(TableCell, {}, () => "A"),
|
||||
),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
cleanup.push(() => w.unmount());
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
|
||||
empty.value = true;
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(0);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
empty.value = false;
|
||||
await flushPromises();
|
||||
expect(w.findAll('[data-slot="table-row"]')).toHaveLength(1);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(false);
|
||||
});
|
||||
@@ -0,0 +1,117 @@
|
||||
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();
|
||||
});
|
||||
@@ -0,0 +1,165 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h } from "vue";
|
||||
import {
|
||||
ToastQueue,
|
||||
ToastProvider,
|
||||
toast,
|
||||
toastQueue,
|
||||
createToastFunction,
|
||||
Table,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableBody,
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableColumn,
|
||||
TableColumnResizer,
|
||||
TableSortableColumnHeader,
|
||||
} 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());
|
||||
toastQueue.clear();
|
||||
if (vi.isFakeTimers()) vi.runOnlyPendingTimers();
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
it("the exported toast helper is rendered by the default provider", async () => {
|
||||
vi.useFakeTimers();
|
||||
const w = mount(ToastProvider);
|
||||
wrappers.push(w);
|
||||
toast("global notification", { timeout: 0 });
|
||||
await flushPromises();
|
||||
expect(w.text()).toContain("global notification");
|
||||
});
|
||||
it("row click listener is forwarded exactly once", async () => {
|
||||
const onClick = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableBody, {}, () =>
|
||||
h(TableRow, { rowKey: "one", onClick }, () =>
|
||||
h(TableCell, {}, () => "One"),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("tr").trigger("click");
|
||||
expect(onClick).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("caller preventDefault vetoes sort before the sort proposal", async () => {
|
||||
const change = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, { onSortChange: change }, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableHeader, {}, () =>
|
||||
h(TableRow, { rowKey: "head" }, () =>
|
||||
h(
|
||||
TableSortableColumnHeader,
|
||||
{
|
||||
columnKey: "name",
|
||||
onClick: (e: MouseEvent) => e.preventDefault(),
|
||||
},
|
||||
() => "Name",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("button").trigger("click");
|
||||
expect(change).not.toHaveBeenCalled();
|
||||
});
|
||||
it("the first column resize handle changes its containing column", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableHeader, {}, () =>
|
||||
h(TableRow, { rowKey: "head" }, () => [
|
||||
h(TableColumn, {}, () => ["First", h(TableColumnResizer)]),
|
||||
h(TableColumn, {}, () => "Second"),
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
const th = w.get("th").element as HTMLElement;
|
||||
Object.defineProperty(th, "getBoundingClientRect", {
|
||||
value: () => ({ width: 100 }),
|
||||
});
|
||||
await w
|
||||
.get('[data-slot="table-column-resizer"]')
|
||||
.trigger("keydown", { key: "ArrowRight" });
|
||||
expect(th.style.width).toBe("108px");
|
||||
});
|
||||
it("changing provider queue subscribes to and renders the new queue", async () => {
|
||||
const a = new ToastQueue(),
|
||||
b = new ToastQueue();
|
||||
queues.push(a, b);
|
||||
const w = mount(ToastProvider, { props: { queue: a } });
|
||||
wrappers.push(w);
|
||||
await w.setProps({ queue: b });
|
||||
b.add({ title: "new queue" }, { timeout: 0 });
|
||||
await flushPromises();
|
||||
expect(w.text()).toContain("new queue");
|
||||
});
|
||||
it("a completed promise cannot overwrite an explicitly replaced loading entry", async () => {
|
||||
const q = new ToastQueue({ exitDuration: 0 });
|
||||
queues.push(q);
|
||||
const api = createToastFunction(q);
|
||||
let resolve!: (n: number) => void;
|
||||
const id = api.promise(
|
||||
new Promise<number>((r) => {
|
||||
resolve = r;
|
||||
}),
|
||||
{ loading: "loading", success: "old result", error: "failed" },
|
||||
);
|
||||
api.update(id, "new content", { timeout: 0 });
|
||||
resolve(1);
|
||||
await flushPromises();
|
||||
expect(q.visibleToasts.find((t) => t.key === id)?.content.title).toBe(
|
||||
"new content",
|
||||
);
|
||||
});
|
||||
it("default loading content renders the shared Spinner", () => {
|
||||
const q = new ToastQueue();
|
||||
queues.push(q);
|
||||
q.add({ title: "Loading", isLoading: true }, { timeout: 0 });
|
||||
const w = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(w);
|
||||
expect(w.find('[data-slot="spinner"]').exists()).toBe(true);
|
||||
});
|
||||
it("default action forwards its callback and visual options", async () => {
|
||||
const action = vi.fn();
|
||||
const q = new ToastQueue();
|
||||
queues.push(q);
|
||||
q.add(
|
||||
{
|
||||
title: "Action",
|
||||
actionProps: { label: "Retry", onClick: action, variant: "danger" },
|
||||
},
|
||||
{ timeout: 0 },
|
||||
);
|
||||
const w = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(w);
|
||||
const button = w.findAll("button").find((b) => b.text() === "Retry")!;
|
||||
await button.trigger("click");
|
||||
expect(action).toHaveBeenCalledTimes(1);
|
||||
expect(button.classes()).toContain("hv-button--danger");
|
||||
});
|
||||
@@ -0,0 +1,392 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, h, nextTick, ref } from "vue";
|
||||
import {
|
||||
Table,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableColumn,
|
||||
TableColumnResizer,
|
||||
TableBody,
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableSortableColumnHeader,
|
||||
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());
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
function table(
|
||||
options: Record<string, unknown> = {},
|
||||
keys: (string | number)[] = [0, "0", "", 3],
|
||||
) {
|
||||
const items = ref(keys);
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableContent, options, () => [
|
||||
h(TableHeader, () => [
|
||||
h(TableColumn, { id: "name", isRowHeader: true }, () => "Name"),
|
||||
h(TableColumn, { id: "value" }, () => "Value"),
|
||||
]),
|
||||
h(TableBody, () =>
|
||||
items.value.map((key) =>
|
||||
h(TableRow, { rowKey: key, key }, () => [
|
||||
h(TableCell, () => String(key)),
|
||||
h(TableCell, () => "Value"),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
return { w, items };
|
||||
}
|
||||
it("roves across numeric/string/empty keys, skips disabled rows and extends a range", async () => {
|
||||
const { w } = table({ selectionMode: "multiple", disabledKeys: ["0"] });
|
||||
await nextTick();
|
||||
const rows = w.findAll("tbody tr");
|
||||
expect(rows.map((r) => r.attributes("tabindex"))).toEqual([
|
||||
"0",
|
||||
"-1",
|
||||
"-1",
|
||||
"-1",
|
||||
]);
|
||||
(rows[0].element as HTMLElement).focus();
|
||||
await rows[0].trigger("keydown", { key: " " });
|
||||
await rows[0].trigger("keydown", { key: "ArrowDown", shiftKey: true });
|
||||
await nextTick();
|
||||
expect(document.activeElement).toBe(rows[2].element);
|
||||
expect(
|
||||
w.findComponent(TableContent).emitted("selectionChange")?.at(-1),
|
||||
).toEqual([[0, ""]]);
|
||||
await rows[2].trigger("keydown", { key: "a", ctrlKey: true });
|
||||
expect(
|
||||
w.findComponent(TableContent).emitted("selectionChange")?.at(-1),
|
||||
).toEqual([[0, "", 3]]);
|
||||
});
|
||||
it("uses accepted controlled selection and permits the same rejected range proposal again", async () => {
|
||||
const { w } = table({ selectionMode: "multiple", selectedKeys: [] });
|
||||
await nextTick();
|
||||
const rows = w.findAll("tbody tr");
|
||||
(rows[0].element as HTMLElement).focus();
|
||||
await rows[2].trigger("click", { shiftKey: true });
|
||||
await rows[2].trigger("click", { shiftKey: true });
|
||||
expect(w.findComponent(TableContent).emitted("selectionChange")).toEqual([
|
||||
[[0, "0", ""]],
|
||||
[[0, "0", ""]],
|
||||
]);
|
||||
expect(rows.every((row) => row.attributes("aria-selected") === "false")).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
it("moves through cells and honors preventDefault without stealing child controls", async () => {
|
||||
const { w } = table();
|
||||
await nextTick();
|
||||
const rows = w.findAll("tbody tr");
|
||||
(rows[0].element as HTMLElement).focus();
|
||||
await rows[0].trigger("keydown", { key: "ArrowRight" });
|
||||
expect(document.activeElement).toBe(rows[0].findAll("th,td")[0].element);
|
||||
await w.get("tbody th").trigger("keydown", { key: "ArrowDown" });
|
||||
expect(document.activeElement).toBe(rows[1].findAll("th,td")[0].element);
|
||||
});
|
||||
it("refreshes DOM order after keyed reordering", async () => {
|
||||
const { w, items } = table();
|
||||
await nextTick();
|
||||
items.value = [3, "", 0, "0"];
|
||||
await nextTick();
|
||||
const rows = w.findAll("tbody tr");
|
||||
(rows[0].element as HTMLElement).focus();
|
||||
await rows[0].trigger("keydown", { key: "ArrowDown" });
|
||||
expect(document.activeElement).toBe(rows[1].element);
|
||||
});
|
||||
it("requests and focuses a previously unmounted logical row", async () => {
|
||||
const visible = ref([0]);
|
||||
const onFocusRow = vi.fn((key: string | number) => {
|
||||
visible.value = [Number(key)];
|
||||
});
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableContent, { rowKeys: [0, 1, 2], onFocusRow }, () =>
|
||||
h(TableBody, () =>
|
||||
visible.value.map((key) =>
|
||||
h(TableRow, { key, rowKey: key }, () =>
|
||||
h(TableCell, () => key),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
await nextTick();
|
||||
(w.get("tbody tr").element as HTMLElement).focus();
|
||||
await w.get("tbody tr").trigger("keydown", { key: "ArrowDown" });
|
||||
await nextTick();
|
||||
expect(onFocusRow).toHaveBeenCalledWith(1, 1);
|
||||
expect(document.activeElement?.textContent).toBe("1");
|
||||
});
|
||||
it("maps row headers and tree-column slot metadata only to their actual columns", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableContent, { treeColumn: "name" }, () => [
|
||||
h(TableHeader, () => [
|
||||
h(TableColumn, { id: "name", isRowHeader: true }, () => "Name"),
|
||||
h(TableColumn, { id: "value" }, () => "Value"),
|
||||
]),
|
||||
h(TableBody, () =>
|
||||
h(TableRow, { rowKey: "a", hasChildItems: true }, () => [
|
||||
h(
|
||||
TableCell,
|
||||
{},
|
||||
{
|
||||
default: ({ isTreeColumn }: { isTreeColumn: boolean }) =>
|
||||
String(isTreeColumn),
|
||||
},
|
||||
),
|
||||
h(
|
||||
TableCell,
|
||||
{},
|
||||
{
|
||||
default: ({ isTreeColumn }: { isTreeColumn: boolean }) =>
|
||||
String(isTreeColumn),
|
||||
},
|
||||
),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await nextTick();
|
||||
expect(w.get("tbody th").attributes("scope")).toBe("row");
|
||||
expect(w.get("tbody th").text()).toBe("true");
|
||||
expect(w.get("tbody td").text()).toBe("false");
|
||||
});
|
||||
it("sorts only the correct column and only once per click", async () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableContent, () =>
|
||||
h(TableHeader, () =>
|
||||
["a", "b"].map((id) =>
|
||||
h(TableColumn, { id, allowsSorting: true }, () =>
|
||||
h(TableSortableColumnHeader, { columnKey: id }, () => id),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.findAll("button")[1].trigger("click");
|
||||
expect(w.findComponent(TableContent).emitted("sortChange")).toEqual([
|
||||
[{ column: "b", direction: "ascending" }],
|
||||
]);
|
||||
expect(
|
||||
w.findAll('[data-slot="table-sortable-column-indicator"]'),
|
||||
).toHaveLength(1);
|
||||
expect(w.findAll("th").map((th) => th.attributes("aria-sort"))).toEqual([
|
||||
"none",
|
||||
"ascending",
|
||||
]);
|
||||
});
|
||||
it("controlled column width proposals respect max, RTL and rejection/retry", async () => {
|
||||
const update = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, { dir: "rtl" }, () =>
|
||||
h(TableContent, () =>
|
||||
h(TableHeader, () =>
|
||||
h(
|
||||
TableColumn,
|
||||
{ width: 100, maxWidth: 110, "onUpdate:width": update },
|
||||
() => h(TableColumnResizer),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
const th = w.get("th").element as HTMLElement;
|
||||
vi.spyOn(th, "getBoundingClientRect").mockReturnValue({
|
||||
width: 100,
|
||||
} as DOMRect);
|
||||
const handle = w.get('[role="separator"]');
|
||||
await handle.trigger("keydown", { key: "ArrowLeft", shiftKey: true });
|
||||
await handle.trigger("keydown", { key: "ArrowLeft", shiftKey: true });
|
||||
expect(update.mock.calls).toEqual([[110], [110]]);
|
||||
expect(th.style.width).toBe("100px");
|
||||
});
|
||||
it("default toast anatomy separates indicator/text/actions; overflow is inert", async () => {
|
||||
const q = new ToastQueue();
|
||||
queues.push(q);
|
||||
for (let i = 0; i < 4; i++)
|
||||
q.add(
|
||||
{
|
||||
title: "Title",
|
||||
description: "Description",
|
||||
actionProps: { label: "Action" },
|
||||
},
|
||||
{ timeout: 0 },
|
||||
);
|
||||
const w = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(w);
|
||||
await nextTick();
|
||||
const card = w.get('[data-slot="toast"]');
|
||||
expect(
|
||||
card.get('[data-slot="toast-content"]').findAll("button"),
|
||||
).toHaveLength(0);
|
||||
expect(card.findAll(':scope > [data-slot="toast-indicator"]')).toHaveLength(
|
||||
1,
|
||||
);
|
||||
expect(w.get('[data-hidden="true"]').attributes("inert")).toBeDefined();
|
||||
});
|
||||
it("Alt+T enters the live stack, close focuses its neighbor, Escape returns to trigger", async () => {
|
||||
const trigger = document.createElement("button");
|
||||
document.body.append(trigger);
|
||||
trigger.focus();
|
||||
const q = new ToastQueue();
|
||||
queues.push(q);
|
||||
const first = q.add({ title: "First" }, { timeout: 0 });
|
||||
const second = q.add({ title: "Second" }, { timeout: 0 });
|
||||
const w = mount(ToastProvider, {
|
||||
props: { queue: q },
|
||||
attachTo: document.body,
|
||||
});
|
||||
wrappers.push(w);
|
||||
await nextTick();
|
||||
document.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { code: "KeyT", altKey: true, bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
expect(document.activeElement?.textContent).toBe("Second");
|
||||
q.close(second);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(document.activeElement?.textContent).toBe("First");
|
||||
document.activeElement?.dispatchEvent(
|
||||
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
|
||||
);
|
||||
await nextTick();
|
||||
expect(document.activeElement).toBe(trigger);
|
||||
});
|
||||
it("one provider unmount cannot release another provider hover pause on the same queue", async () => {
|
||||
vi.useFakeTimers();
|
||||
const q = new ToastQueue({ exitDuration: 0 });
|
||||
queues.push(q);
|
||||
q.add({ title: "Shared" }, { timeout: 1000 });
|
||||
const a = mount(ToastProvider, { props: { queue: q } }),
|
||||
b = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(b);
|
||||
a.element.dispatchEvent(new MouseEvent("pointerenter"));
|
||||
b.element.dispatchEvent(new MouseEvent("pointerenter"));
|
||||
a.unmount();
|
||||
vi.advanceTimersByTime(2000);
|
||||
expect(q.visibleToasts).toHaveLength(1);
|
||||
});
|
||||
it("default toast action stopImmediatePropagation vetoes automatic close", async () => {
|
||||
const queue = new ToastQueue();
|
||||
queues.push(queue);
|
||||
queue.add(
|
||||
{
|
||||
title: "Action",
|
||||
actionProps: {
|
||||
label: "Keep",
|
||||
onClick: (event) => event.stopImmediatePropagation(),
|
||||
},
|
||||
},
|
||||
{ timeout: 0 },
|
||||
);
|
||||
const w = mount(ToastProvider, { props: { queue } });
|
||||
wrappers.push(w);
|
||||
await w.get('[data-slot="toast-action-button"]').trigger("click");
|
||||
expect(queue.getExitingKeys().size).toBe(0);
|
||||
expect(queue.visibleToasts).toHaveLength(1);
|
||||
});
|
||||
it("load-more observes its scroll container and deduplicates an in-flight request", async () => {
|
||||
const observers: {
|
||||
callback: IntersectionObserverCallback;
|
||||
options?: IntersectionObserverInit;
|
||||
disconnect: ReturnType<typeof vi.fn>;
|
||||
}[] = [];
|
||||
vi.stubGlobal(
|
||||
"IntersectionObserver",
|
||||
class {
|
||||
disconnect = vi.fn();
|
||||
observe = vi.fn();
|
||||
unobserve = vi.fn();
|
||||
constructor(
|
||||
callback: IntersectionObserverCallback,
|
||||
options?: IntersectionObserverInit,
|
||||
) {
|
||||
observers.push({ callback, options, disconnect: this.disconnect });
|
||||
}
|
||||
},
|
||||
);
|
||||
const { TableLoadMoreItem, TableLoadMoreContent, TableScrollContainer } =
|
||||
await import("../../../packages/vue/src");
|
||||
const loading = ref(false);
|
||||
const load = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, () =>
|
||||
h(TableScrollContainer, () =>
|
||||
h(TableContent, () =>
|
||||
h(TableBody, () =>
|
||||
h(
|
||||
TableLoadMoreItem,
|
||||
{ isLoading: loading.value, onLoadMore: load },
|
||||
() => h(TableLoadMoreContent, () => "Loading"),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
const observer = observers.at(-1)!;
|
||||
expect(observer.options?.root).toBe(
|
||||
w.get('[data-slot="table-scroll-container"]').element,
|
||||
);
|
||||
expect(
|
||||
w.find('tbody > tr > td > [data-slot="table-load-more-content"]').exists(),
|
||||
).toBe(true);
|
||||
const entry = [{ isIntersecting: true }] as IntersectionObserverEntry[];
|
||||
observer.callback(entry, {} as IntersectionObserver);
|
||||
observer.callback(entry, {} as IntersectionObserver);
|
||||
expect(load).toHaveBeenCalledTimes(1);
|
||||
loading.value = true;
|
||||
await nextTick();
|
||||
loading.value = false;
|
||||
await nextTick();
|
||||
expect(load).toHaveBeenCalledTimes(2);
|
||||
w.unmount();
|
||||
wrappers.pop();
|
||||
observer.callback(entry, {} as IntersectionObserver);
|
||||
expect(load).toHaveBeenCalledTimes(2);
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
@@ -0,0 +1,145 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h } from "vue";
|
||||
import {
|
||||
ToastQueue,
|
||||
ToastProvider,
|
||||
createToastFunction,
|
||||
Table,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableBody,
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableSortableColumnHeader,
|
||||
} from "../../../packages/vue/src";
|
||||
const queues: ToastQueue[] = [];
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => {
|
||||
wrappers.splice(0).forEach((w) => w.unmount());
|
||||
queues.splice(0).forEach((q) => q.destroy());
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
function queue() {
|
||||
const q = new ToastQueue({ exitDuration: 0 });
|
||||
queues.push(q);
|
||||
return q;
|
||||
}
|
||||
it("toasts added while paused start their countdown on resume", () => {
|
||||
vi.useFakeTimers();
|
||||
const q = queue();
|
||||
q.pauseAll();
|
||||
q.add({ title: "paused-add" }, { timeout: 100 });
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(q.visibleToasts).toHaveLength(1);
|
||||
q.resumeAll();
|
||||
vi.advanceTimersByTime(101);
|
||||
expect(q.visibleToasts).toHaveLength(0);
|
||||
});
|
||||
it("a repeated resume does not leave a stale countdown after a later pause", () => {
|
||||
vi.useFakeTimers();
|
||||
const q = queue();
|
||||
q.add({ title: "x" }, { timeout: 1000 });
|
||||
vi.advanceTimersByTime(100);
|
||||
q.resumeAll();
|
||||
vi.advanceTimersByTime(100);
|
||||
q.pauseAll();
|
||||
vi.advanceTimersByTime(2000);
|
||||
expect(q.visibleToasts).toHaveLength(1);
|
||||
});
|
||||
it("promise resolving after early close does not create a replacement notification", async () => {
|
||||
const q = queue();
|
||||
const toast = createToastFunction(q);
|
||||
let resolve!: (n: number) => void;
|
||||
const id = toast.promise(
|
||||
new Promise<number>((r) => {
|
||||
resolve = r;
|
||||
}),
|
||||
{ loading: "loading", success: "done", error: "error" },
|
||||
);
|
||||
toast.close(id);
|
||||
expect(q.visibleToasts).toHaveLength(0);
|
||||
resolve(1);
|
||||
await flushPromises();
|
||||
expect(q.visibleToasts).toHaveLength(0);
|
||||
});
|
||||
it("variant demo renders actual Vue labels rather than JSX-like literals", async () => {
|
||||
const mod = await import("../../../apps/docs/demos/toast-variants.vue");
|
||||
const w = mount(mod.default);
|
||||
wrappers.push(w);
|
||||
expect(w.findAll("button").map((b) => b.text())).toEqual([
|
||||
"默认 Toast",
|
||||
"强调 Toast",
|
||||
"成功 Toast",
|
||||
"警告 Toast",
|
||||
"危险 Toast",
|
||||
]);
|
||||
});
|
||||
it("a row action does not select the enclosing row", async () => {
|
||||
const onSelection = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(
|
||||
Table,
|
||||
{ selectionMode: "multiple", onSelectionChange: onSelection },
|
||||
() =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableBody, {}, () =>
|
||||
h(TableRow, { rowKey: "a" }, () =>
|
||||
h(TableCell, {}, () =>
|
||||
h("button", { type: "button" }, "Edit"),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("button").trigger("click");
|
||||
expect(onSelection).not.toHaveBeenCalled();
|
||||
});
|
||||
it("sortable header invokes a user click listener only once", async () => {
|
||||
const onClick = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableHeader, {}, () =>
|
||||
h(TableRow, { rowKey: "head" }, () =>
|
||||
h(
|
||||
TableSortableColumnHeader,
|
||||
{ columnKey: "a", onClick },
|
||||
() => "A",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("button").trigger("click");
|
||||
expect(onClick).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("hovering the notification region pauses its timeout", async () => {
|
||||
vi.useFakeTimers();
|
||||
const q = queue();
|
||||
q.add({ title: "hover" }, { timeout: 100 });
|
||||
const w = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(w);
|
||||
await w.get('[data-slot="toast-region"]').trigger("pointerenter");
|
||||
vi.advanceTimersByTime(101);
|
||||
expect(q.visibleToasts).toHaveLength(1);
|
||||
});
|
||||
it("default provider renders queued content without requiring a custom renderer", () => {
|
||||
const q = queue();
|
||||
q.add({ title: "default content" }, { timeout: 0 });
|
||||
const w = mount(ToastProvider, { props: { queue: q } });
|
||||
wrappers.push(w);
|
||||
expect(w.text()).toContain("default content");
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
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<typeof mount>[] = [];
|
||||
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);
|
||||
});
|
||||
@@ -0,0 +1,106 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h } from "vue";
|
||||
import {
|
||||
ToastQueue,
|
||||
ToastProvider,
|
||||
Table,
|
||||
TableContent,
|
||||
TableBody,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableColumn,
|
||||
} 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());
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
it("after queue swap the visible toast close action closes the new queue", 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);
|
||||
await w.setProps({ queue: b });
|
||||
b.add({ title: "new queue toast" }, { timeout: 0 });
|
||||
await flushPromises();
|
||||
expect(w.text()).toContain("new queue toast");
|
||||
await w.get('button[data-slot="toast-close"]').trigger("click");
|
||||
await flushPromises();
|
||||
expect(b.visibleToasts).toHaveLength(0);
|
||||
});
|
||||
it("row event arrays respect stopImmediatePropagation before other handlers and selection", async () => {
|
||||
const second = vi.fn(),
|
||||
select = vi.fn();
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, {}, () =>
|
||||
h(
|
||||
TableContent,
|
||||
{ selectionMode: "multiple", onSelectionChange: select },
|
||||
() =>
|
||||
h(TableBody, {}, () =>
|
||||
h(
|
||||
TableRow,
|
||||
{
|
||||
rowKey: "one",
|
||||
onClick: [
|
||||
(e: MouseEvent) => e.stopImmediatePropagation(),
|
||||
second,
|
||||
],
|
||||
},
|
||||
() => h(TableCell, {}, () => "One"),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("tr").trigger("click");
|
||||
expect(second).not.toHaveBeenCalled();
|
||||
expect(select).not.toHaveBeenCalled();
|
||||
});
|
||||
it("a sortable column forwards user click and honors its cancellation", async () => {
|
||||
const sort = vi.fn(),
|
||||
click = vi.fn((e: MouseEvent) => e.preventDefault());
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
render: () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, { onSortChange: sort }, () =>
|
||||
h(TableHeader, {}, () =>
|
||||
h(
|
||||
TableColumn,
|
||||
{ id: "name", allowsSorting: true, onClick: click },
|
||||
() => "Name",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
await w.get("th").trigger("click");
|
||||
expect(click).toHaveBeenCalledTimes(1);
|
||||
expect(sort).not.toHaveBeenCalled();
|
||||
});
|
||||
it("baseline table header creates a header row around its columns", async () => {
|
||||
const mod = await import("../../../apps/docs/demos/table-sorting.vue");
|
||||
const w = mount(mod.default);
|
||||
wrappers.push(w);
|
||||
expect(w.find("thead > tr > th").exists()).toBe(true);
|
||||
});
|
||||
it("baseline sorting exposes the current direction on the real column header", async () => {
|
||||
const mod = await import("../../../apps/docs/demos/table-sorting.vue");
|
||||
const w = mount(mod.default);
|
||||
wrappers.push(w);
|
||||
const column = w.findAll("th")[1];
|
||||
await column.trigger("click");
|
||||
expect(column.attributes("aria-sort")).toBe("ascending");
|
||||
});
|
||||
@@ -0,0 +1,444 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { mount, flushPromises } from "@vue/test-utils";
|
||||
import { defineComponent, h, ref, nextTick, computed } from "vue";
|
||||
import {
|
||||
Checkbox,
|
||||
CheckboxContent,
|
||||
CheckboxControl,
|
||||
CheckboxIndicator,
|
||||
EmptyState,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableColumn,
|
||||
TableCollection,
|
||||
TableContent,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
ToastActionButton,
|
||||
ToastCloseButton,
|
||||
ToastContent,
|
||||
ToastDescription,
|
||||
ToastIndicator,
|
||||
ToastProvider,
|
||||
ToastQueue,
|
||||
ToastTitle,
|
||||
createToastFunction,
|
||||
} 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 = "";
|
||||
});
|
||||
|
||||
function mountDemo(setup: () => unknown, render?: () => unknown) {
|
||||
const w = mount(
|
||||
defineComponent(
|
||||
render
|
||||
? { setup: setup as never, render: render as never }
|
||||
: { setup: setup as never },
|
||||
),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
return w;
|
||||
}
|
||||
|
||||
it("TableBody #empty renders EmptyState without a data row", () => {
|
||||
const w = mountDemo(
|
||||
() => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, { "aria-label": "空表格" }, () => [
|
||||
h(TableHeader, {}, () => h(TableColumn, {}, () => "姓名")),
|
||||
h(
|
||||
TableBody,
|
||||
{ colspan: 1 },
|
||||
{
|
||||
empty: () =>
|
||||
h(EmptyState, {}, () =>
|
||||
h("span", { class: "msg" }, "未找到结果"),
|
||||
),
|
||||
},
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
const body = w.get("tbody");
|
||||
expect(body.attributes("data-empty")).toBeDefined();
|
||||
expect(w.findAll("tr[data-slot='table-row']").length).toBe(0);
|
||||
expect(w.find(".msg").text()).toBe("未找到结果");
|
||||
expect(w.find("td[data-slot='table-empty-cell']").attributes("colspan")).toBe(
|
||||
"1",
|
||||
);
|
||||
// Presentation row must not look like a selectable data row.
|
||||
expect(w.find("tr[data-slot='table-empty-row']").attributes("role")).toBe(
|
||||
"presentation",
|
||||
);
|
||||
expect(
|
||||
w.find("tr[data-slot='table-empty-row']").attributes("aria-selected"),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("TableBody keeps data rows when present and ignores #empty", () => {
|
||||
const w = mountDemo(
|
||||
() => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () => [
|
||||
h(
|
||||
TableBody,
|
||||
{},
|
||||
{
|
||||
default: () =>
|
||||
h(TableRow, { rowKey: "1" }, () => h(TableCell, {}, () => "A")),
|
||||
empty: () => h("span", { class: "msg" }, "未找到结果"),
|
||||
},
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
expect(w.find(".msg").exists()).toBe(false);
|
||||
expect(w.findAll("tr[data-slot='table-row']").length).toBe(1);
|
||||
expect(w.get("tbody").attributes("data-empty")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("select-all checkbox supports indeterminate and shows 全部 when complete", async () => {
|
||||
const users = [
|
||||
{ id: "1", name: "Kate" },
|
||||
{ id: "2", name: "John" },
|
||||
{ id: "3", name: "Sara" },
|
||||
];
|
||||
const selectedKeys = ref<string[]>([]);
|
||||
const w = mountDemo(
|
||||
() => {
|
||||
const allChecked = computed(
|
||||
() => users.length > 0 && selectedKeys.value.length === users.length,
|
||||
);
|
||||
const someChecked = computed(
|
||||
() => selectedKeys.value.length > 0 && !allChecked.value,
|
||||
);
|
||||
return {
|
||||
selectedKeys,
|
||||
allChecked,
|
||||
someChecked,
|
||||
users,
|
||||
};
|
||||
},
|
||||
function (this: {
|
||||
selectedKeys: string[];
|
||||
allChecked: boolean;
|
||||
someChecked: boolean;
|
||||
users: typeof users;
|
||||
}) {
|
||||
return h("div", [
|
||||
h(Checkbox, {
|
||||
ariaLabel: "全选",
|
||||
isSelected: this.allChecked,
|
||||
isIndeterminate: this.someChecked,
|
||||
"onUpdate:checked": () => {
|
||||
this.selectedKeys = this.allChecked
|
||||
? []
|
||||
: this.users.map((u) => u.id);
|
||||
},
|
||||
}),
|
||||
h(
|
||||
"p",
|
||||
{ class: "summary" },
|
||||
this.allChecked
|
||||
? "全部"
|
||||
: this.selectedKeys.length
|
||||
? this.selectedKeys.join(", ")
|
||||
: "无",
|
||||
),
|
||||
]);
|
||||
},
|
||||
);
|
||||
expect(w.find(".summary").text()).toBe("无");
|
||||
const input = w.get("input");
|
||||
expect(input.element.indeterminate).toBe(false);
|
||||
|
||||
selectedKeys.value = ["1"];
|
||||
await nextTick();
|
||||
expect(w.find(".summary").text()).toBe("1");
|
||||
expect(w.get("input").element.indeterminate).toBe(true);
|
||||
|
||||
selectedKeys.value = ["1", "2", "3"];
|
||||
await nextTick();
|
||||
expect(w.find(".summary").text()).toBe("全部");
|
||||
expect(w.get("input").element.indeterminate).toBe(false);
|
||||
expect(w.get("input").element.checked).toBe(true);
|
||||
});
|
||||
|
||||
it("row checkbox click does not also toggle row selection", async () => {
|
||||
const selectionChanges: unknown[] = [];
|
||||
const w = mountDemo(
|
||||
() => ({ selectionChanges }),
|
||||
function (this: { selectionChanges: unknown[] }) {
|
||||
return h(Table, {}, () =>
|
||||
h(
|
||||
TableContent,
|
||||
{
|
||||
selectionMode: "multiple",
|
||||
onSelectionChange: (keys: unknown) =>
|
||||
this.selectionChanges.push(keys),
|
||||
},
|
||||
() =>
|
||||
h(TableBody, {}, () =>
|
||||
h(
|
||||
TableRow,
|
||||
{ rowKey: "1" },
|
||||
{
|
||||
default: () => [
|
||||
h(TableCell, {}, () =>
|
||||
h(
|
||||
Checkbox,
|
||||
{},
|
||||
{
|
||||
default: () => [
|
||||
h(
|
||||
CheckboxContent,
|
||||
{},
|
||||
{
|
||||
default: () => [
|
||||
h(CheckboxControl, {}, () =>
|
||||
h(CheckboxIndicator),
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
),
|
||||
h(TableCell, {}, () => "Kate"),
|
||||
],
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
const label = w.get("label[data-slot='checkbox-content']");
|
||||
await label.trigger("click");
|
||||
await nextTick();
|
||||
// Clicking the choice control must not bubble into row selection.
|
||||
expect(
|
||||
(w.vm as unknown as { selectionChanges: unknown[] }).selectionChanges,
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("toast root is alertdialog with title/description ids and content role=alert", async () => {
|
||||
const queue = new ToastQueue();
|
||||
queues.push(queue);
|
||||
const toast = createToastFunction(queue);
|
||||
const w = mountDemo(
|
||||
() => ({ queue }),
|
||||
() =>
|
||||
h(
|
||||
ToastProvider,
|
||||
{ queue: queue as never, to: "body" },
|
||||
{
|
||||
default: ({
|
||||
toast: item,
|
||||
close,
|
||||
}: {
|
||||
toast: { title: string; description: string };
|
||||
close: () => void;
|
||||
}) =>
|
||||
h(ToastContent, {}, () => [
|
||||
h(ToastTitle, () => item.title),
|
||||
h(ToastDescription, () => item.description),
|
||||
h(ToastActionButton, { onClick: () => close() }, () => "忽略"),
|
||||
h(ToastCloseButton, { onClick: () => close() }),
|
||||
]),
|
||||
},
|
||||
),
|
||||
);
|
||||
toast("您已被邀请加入团队", {
|
||||
description: "Bob 邀请您加入 HeroUI 团队",
|
||||
timeout: 0,
|
||||
});
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
|
||||
const root = document.querySelector('[data-slot="toast"]') as HTMLElement;
|
||||
expect(root).toBeTruthy();
|
||||
expect(root.getAttribute("role")).toBe("alertdialog");
|
||||
expect(root.getAttribute("aria-modal")).toBe("false");
|
||||
const title = root.querySelector('[data-slot="toast-title"]') as HTMLElement;
|
||||
const desc = root.querySelector(
|
||||
'[data-slot="toast-description"]',
|
||||
) as HTMLElement;
|
||||
expect(title.id).toBeTruthy();
|
||||
expect(desc.id).toBeTruthy();
|
||||
expect(root.getAttribute("aria-labelledby")).toBe(title.id);
|
||||
expect(root.getAttribute("aria-describedby")).toBe(desc.id);
|
||||
|
||||
const content = root.querySelector(
|
||||
'[data-slot="toast-content"]',
|
||||
) as HTMLElement;
|
||||
expect(content.getAttribute("role")).toBe("alert");
|
||||
expect(content.getAttribute("aria-atomic")).toBe("true");
|
||||
// After mount the alert payload is visible so it can be announced.
|
||||
await nextTick();
|
||||
expect(content.getAttribute("aria-hidden")).not.toBe("true");
|
||||
|
||||
// Close button keeps an accessible name.
|
||||
const close = root.querySelector("[data-slot='toast-close']");
|
||||
expect(close?.getAttribute("aria-label")).toBeTruthy();
|
||||
void w;
|
||||
});
|
||||
|
||||
it("toast indicator sets data-swapped only after the first paint", async () => {
|
||||
const queue = new ToastQueue();
|
||||
queues.push(queue);
|
||||
const toast = createToastFunction(queue);
|
||||
mountDemo(
|
||||
() => ({ queue }),
|
||||
() =>
|
||||
h(
|
||||
ToastProvider,
|
||||
{ queue: queue as never, to: "body" },
|
||||
{
|
||||
default: ({
|
||||
toast: item,
|
||||
}: {
|
||||
toast: { title: string; isLoading?: boolean; variant?: string };
|
||||
}) =>
|
||||
h(ToastContent, {}, () => [
|
||||
h(ToastTitle, () => item.title),
|
||||
h(
|
||||
ToastIndicator,
|
||||
{ variant: (item.variant ?? "default") as never },
|
||||
item.isLoading
|
||||
? () => h("span", { "data-slot": "spinner" })
|
||||
: undefined,
|
||||
),
|
||||
]),
|
||||
},
|
||||
),
|
||||
);
|
||||
const loadingId = toast("正在上传文件…", { isLoading: true, timeout: 0 });
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
|
||||
const indicator = () =>
|
||||
document.querySelector('[data-slot="toast-indicator"]') as HTMLElement;
|
||||
expect(indicator().getAttribute("data-swapped")).toBeNull();
|
||||
|
||||
toast.update(loadingId, "文件已上传", {
|
||||
isLoading: false,
|
||||
variant: "success",
|
||||
timeout: 0,
|
||||
});
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
expect(indicator().getAttribute("data-swapped")).toBe("true");
|
||||
});
|
||||
|
||||
it("column defaultWidth 1fr does not write an invalid CSS width", () => {
|
||||
const w = mountDemo(
|
||||
() => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(TableHeader, {}, () =>
|
||||
h(TableColumn, { id: "name", defaultWidth: "1fr" }, () => "姓名"),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
const th = w.get("th");
|
||||
expect((th.element as HTMLElement).style.width).toBe("");
|
||||
expect(th.attributes("data-default-width")).toBe("1fr");
|
||||
});
|
||||
|
||||
it("tree column cell gets data-tree-column and row level variable", () => {
|
||||
const w = mountDemo(
|
||||
() => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, { treeColumn: "name" }, () => [
|
||||
h(TableHeader, {}, () =>
|
||||
h(TableColumn, { id: "name" }, () => "姓名"),
|
||||
),
|
||||
h(TableBody, {}, () =>
|
||||
h(TableRow, { rowKey: "1", level: 2, hasChildItems: true }, () =>
|
||||
h(TableCell, {}, () => "文档"),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
const tr = w.get("tr[data-slot='table-row']");
|
||||
expect(tr.attributes("data-level")).toBe("2");
|
||||
expect(
|
||||
(tr.element as HTMLElement).style.getPropertyValue("--table-row-level"),
|
||||
).toBe("2");
|
||||
const td = w.get("td[data-slot='table-cell']");
|
||||
expect(td.attributes("data-tree-column")).toBeDefined();
|
||||
});
|
||||
|
||||
it("TableCollection empty ↔ non-empty toggles the empty slot", async () => {
|
||||
const items = ref<string[]>([]);
|
||||
const w = mountDemo(
|
||||
() => () =>
|
||||
h(Table, {}, () =>
|
||||
h(TableContent, {}, () =>
|
||||
h(
|
||||
TableBody,
|
||||
{ colspan: 1 },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
TableCollection,
|
||||
{ items: items.value },
|
||||
{
|
||||
default: ({ item }: { item: string }) =>
|
||||
h(TableRow, { rowKey: item }, () =>
|
||||
h(TableCell, {}, () => item),
|
||||
),
|
||||
},
|
||||
),
|
||||
empty: () => h("span", { "data-test": "empty" }, "Empty"),
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
expect(w.findAll('[data-slot="table-row"]').length).toBe(0);
|
||||
|
||||
items.value = ["A", "B"];
|
||||
await nextTick();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(false);
|
||||
expect(w.findAll('[data-slot="table-row"]').length).toBe(2);
|
||||
|
||||
items.value = [];
|
||||
await nextTick();
|
||||
expect(w.find('[data-test="empty"]').exists()).toBe(true);
|
||||
expect(w.findAll('[data-slot="table-row"]').length).toBe(0);
|
||||
});
|
||||
|
||||
it("title-only toast omits aria-describedby when no description part exists", async () => {
|
||||
const queue = new ToastQueue();
|
||||
queues.push(queue);
|
||||
const toast = createToastFunction(queue);
|
||||
mountDemo(
|
||||
() => ({ queue }),
|
||||
() => h(ToastProvider, { queue: queue as never, to: "body" }),
|
||||
);
|
||||
toast("标题独占", { timeout: 0 });
|
||||
await flushPromises();
|
||||
await nextTick();
|
||||
const root = document.querySelector('[data-slot="toast"]') as HTMLElement;
|
||||
const refs = (root.getAttribute("aria-describedby") ?? "")
|
||||
.split(/\s+/)
|
||||
.filter(Boolean);
|
||||
expect(refs.length).toBe(0);
|
||||
const labelled = root.getAttribute("aria-labelledby");
|
||||
expect(labelled && document.getElementById(labelled)).toBeTruthy();
|
||||
});
|
||||
Reference in New Issue
Block a user