393 lines
13 KiB
TypeScript
393 lines
13 KiB
TypeScript
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();
|
|
});
|