Files
heroui-vue/tests/toast.test.ts
T

250 lines
7.5 KiB
TypeScript

import { describe, expect, it, vi, afterEach } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, nextTick } from "vue";
import {
ToastQueue,
createToastFunction,
ToastProvider,
ToastContent,
ToastTitle,
ToastDescription,
ToastIndicator,
ToastCloseButton,
} from "../packages/vue/src";
afterEach(async () => {
await flushPromises();
document.body.innerHTML = "";
});
describe("ToastQueue", () => {
it("add returns stable key and default timeout auto-closes", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
const key = q.add({ title: "hi" });
expect(key).toBeTruthy();
expect(q.visibleToasts).toHaveLength(1);
vi.advanceTimersByTime(4000);
// exit duration 300ms keeps node conceptually; queue removes after exit
vi.advanceTimersByTime(300);
expect(q.visibleToasts).toHaveLength(0);
vi.useRealTimers();
});
it("timeout 0 stays open and update preserves key/position", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
const a = q.add({ title: "a" }, { timeout: 0 });
const b = q.add({ title: "b" }, { timeout: 0 });
expect(q.update(a, { title: "a2" })).toBe(true);
expect(q.visibleToasts.map((t) => t.key)).toEqual([b, a]); // newest first
expect((q.visibleToasts[1].content as { title: string }).title).toBe("a2");
vi.advanceTimersByTime(10000);
expect(q.visibleToasts).toHaveLength(2);
vi.useRealTimers();
});
it("pause/resume keeps remaining time", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
q.add({ title: "x" }, { timeout: 1000 });
vi.advanceTimersByTime(400);
q.pauseAll();
vi.advanceTimersByTime(5000);
expect(q.visibleToasts).toHaveLength(1);
q.resumeAll();
vi.advanceTimersByTime(600);
expect(q.visibleToasts).toHaveLength(1);
vi.advanceTimersByTime(300);
expect(q.visibleToasts).toHaveLength(0);
vi.useRealTimers();
});
it("toast helper variants and promise settle in place", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
const toast = createToastFunction(q);
const id = toast.success("ok");
expect(q.visibleToasts).toHaveLength(1);
const pid = toast.promise(Promise.resolve(1), {
loading: "loading",
success: (n: number) => `done ${n}`,
error: "fail",
});
expect(q.visibleToasts).toHaveLength(2);
await Promise.resolve();
await Promise.resolve();
await flushPromises();
expect(q.visibleToasts.find((t) => t.key === pid)?.content).toMatchObject({
variant: "success",
});
toast.close(id);
vi.advanceTimersByTime(400);
expect(q.visibleToasts.some((t) => t.key === id)).toBe(false);
vi.useRealTimers();
});
});
describe("ToastProvider UI", () => {
it("renders toasts from queue with title/description/indicator", async () => {
const q = new ToastQueue();
q.add(
{ title: "Hello", description: "World", variant: "success" },
{ timeout: 0 },
);
const w = mount(
defineComponent({
components: {
ToastProvider,
ToastContent,
ToastTitle,
ToastDescription,
ToastIndicator,
ToastCloseButton,
},
setup: () => ({ q }),
template: `
<ToastProvider :queue="q">
<template #default="{ toast, close }">
<ToastContent>
<ToastIndicator :variant="toast.variant" />
<ToastTitle>{{ toast.title }}</ToastTitle>
<ToastDescription>{{ toast.description }}</ToastDescription>
<ToastCloseButton @click="close()" />
</ToastContent>
</template>
</ToastProvider>
`,
}),
{ attachTo: document.body },
);
await nextTick();
expect(w.text()).toContain("Hello");
expect(w.text()).toContain("World");
expect(w.find('[data-slot="toast-region"]').exists()).toBe(true);
expect(w.find('[data-slot="toast"]').exists()).toBe(true);
w.unmount();
q.destroy();
});
});
describe("ToastQueue lifecycle edges", () => {
it("close fires onClose at most once and clear closes all", async () => {
vi.useFakeTimers();
const q = new ToastQueue({ exitDuration: 100 });
const onClose = vi.fn();
const key = q.add({ title: "a" }, { timeout: 0, onClose });
q.close(key);
q.close(key);
expect(onClose).toHaveBeenCalledTimes(1);
q.add({ title: "b" }, { timeout: 0 });
q.add({ title: "c" }, { timeout: 0 });
q.clear();
vi.advanceTimersByTime(200);
expect(q.visibleToasts).toHaveLength(0);
vi.useRealTimers();
});
it("custom queue instances are isolated", async () => {
const q1 = new ToastQueue();
const q2 = new ToastQueue();
q1.add({ title: "1" }, { timeout: 0 });
expect(q1.visibleToasts).toHaveLength(1);
expect(q2.visibleToasts).toHaveLength(0);
q1.destroy();
q2.destroy();
});
it("promise after early close does not resurrect the toast", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
const toast = createToastFunction(q);
let resolve!: (v: number) => void;
const p = new Promise<number>((r) => {
resolve = r;
});
const id = toast.promise(p, {
loading: "l",
success: "s",
error: "e",
});
toast.close(id);
vi.advanceTimersByTime(500);
resolve(1);
await flushPromises();
expect(q.visibleToasts.some((t) => t.key === id)).toBe(false);
vi.useRealTimers();
});
it("destroy clears timers and subscriptions", async () => {
vi.useFakeTimers();
const q = new ToastQueue();
const fn = vi.fn();
q.subscribe(fn);
q.add({ title: "x" }, { timeout: 50 });
q.destroy();
vi.advanceTimersByTime(500);
expect(q.visibleToasts).toHaveLength(0);
vi.useRealTimers();
});
});
describe("Toast demos", () => {
it("mounts all 11 baseline demos without Vue warnings", async () => {
const names = [
"default",
"variants",
"placements",
"expanded",
"simple",
"custom-indicator",
"custom-toast",
"promise",
"callbacks",
"custom-queue",
"custom-styles",
];
expect(names).toHaveLength(11);
const warnings: string[] = [];
const spy = vi.spyOn(console, "warn").mockImplementation((...args) => {
warnings.push(args.map(String).join(" "));
});
for (const name of names) {
const mod = await import(`../apps/docs/demos/toast-${name}.vue`);
const w = mount(mod.default, { attachTo: document.body });
await nextTick();
expect(w.html().length).toBeGreaterThan(0);
w.unmount();
}
spy.mockRestore();
expect(warnings).toEqual([]);
});
});
it("re-renders when a toast is added after mount", async () => {
const q = new ToastQueue();
const w = mount(
defineComponent({
components: { ToastProvider, ToastContent, ToastTitle },
setup: () => ({ q }),
template: `
<ToastProvider :queue="q">
<template #default="{ toast: item }">
<ToastContent><ToastTitle>{{ item.title }}</ToastTitle></ToastContent>
</template>
</ToastProvider>
`,
}),
{ attachTo: document.body },
);
await nextTick();
expect(w.find('[data-slot="toast"]').exists()).toBe(false);
q.add({ title: "after-mount" }, { timeout: 0 });
await nextTick();
await flushPromises();
expect(w.find('[data-slot="toast"]').exists()).toBe(true);
expect(w.text()).toContain("after-mount");
w.unmount();
q.destroy();
});