toastUpdate.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // @vitest-environment jsdom
  2. import { cleanup, waitFor } from "@testing-library/react";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { toast, updateToast } from "@/components/ui/toast.js";
  5. afterEach(() => {
  6. cleanup();
  7. document.body.replaceChildren();
  8. vi.useRealTimers();
  9. });
  10. describe("toast updates", () => {
  11. it("updates an existing toast in place instead of stacking a second item", async () => {
  12. let id = toast("导入状态初始化", { durationMs: 0, dedupeKey: "share-import" });
  13. // Toast 容器可能还没有完成首帧挂载,更新也必须保留并合并到即将挂载的同一个 item。
  14. updateToast(id, { message: "正在下载分享文件:0/1", variant: "info" });
  15. await waitFor(() => {
  16. expect(document.querySelector("#zcode-toast-host")?.textContent).toContain(
  17. "正在下载分享文件:0/1",
  18. );
  19. });
  20. // 同一操作的替代提示仍须去重,同时保留紧接着到达的进度更新。
  21. id = toast("正在完成导入", { durationMs: 0, dedupeKey: "share-import" });
  22. updateToast(id, { message: "已从分享导入:示例会话", durationMs: 10 });
  23. await waitFor(() => {
  24. const hostText = document.querySelector("#zcode-toast-host")?.textContent ?? "";
  25. expect(hostText).toContain("已从分享导入:示例会话");
  26. expect(hostText).not.toContain("正在下载分享文件:0/1");
  27. expect(document.querySelectorAll("#zcode-toast-host .rounded-2xl")).toHaveLength(1);
  28. });
  29. await waitFor(
  30. () => expect(document.querySelectorAll("#zcode-toast-host .rounded-2xl")).toHaveLength(0),
  31. { timeout: 1000 },
  32. );
  33. });
  34. });