146 lines
4.2 KiB
TypeScript
146 lines
4.2 KiB
TypeScript
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");
|
|
});
|