/** * @file toast.test.ts * @description The toast queue: at most four on screen, and an error — which * stays until dismissed — is never the one pushed out by newer toasts. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, afterEach } from "vitest"; import { toast, subscribeToasts, dismissToast, type ToastItem } from "../toast"; let current: ToastItem[] = []; const unsubscribe = subscribeToasts((items) => (current = items)); afterEach(() => { for (const t of current) dismissToast(t.id); }); describe("toast queue", () => { it("keeps an unread error when newer toasts overflow the queue", () => { toast.error("save failed"); for (let i = 0; i < 5; i++) toast.success(`copied ${i}`); expect(current).toHaveLength(4); expect(current.map((t) => t.message)).toContain("save failed"); expect(current[current.length - 1]?.message).toBe("copied 4"); }); it("still shows the newest toast when only errors are queued", () => { for (let i = 0; i < 4; i++) toast.error(`error ${i}`); toast.info("hello"); expect(current).toHaveLength(4); expect(current[current.length - 1]?.message).toBe("hello"); }); }); void unsubscribe;