166 lines
4.9 KiB
TypeScript
166 lines
4.9 KiB
TypeScript
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");
|
|
});
|