Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

2.0 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import { act, renderHook } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { toast, useToast } from "@/components/ui/use-toast";

describe("toast once", () => {
  beforeEach(() => {
    vi.useFakeTimers();
  });

  afterEach(() => {
    const { result } = renderHook(() => useToast());
    act(() => {
      result.current.dismiss();
      vi.runAllTimers();
    });
    vi.useRealTimers();
  });

  it("shows a once-toast a single time per session, even after removal", () => {
    const { result } = renderHook(() => useToast());

    act(() => {
      toast({ id: "static-notebook", once: true, title: "Static notebook" });
    });
    expect(result.current.toasts).toHaveLength(1);

    act(() => {
      result.current.dismiss("static-notebook");
      vi.advanceTimersByTime(10_000);
    });
    expect(result.current.toasts).toHaveLength(0);

    act(() => {
      toast({ id: "static-notebook", once: true, title: "Static notebook" });
    });
    expect(result.current.toasts).toHaveLength(0);
  });

  it("does not suppress normal (non-once) toasts", () => {
    const { result } = renderHook(() => useToast());

    act(() => {
      toast({ id: "normal", title: "First" });
    });
    act(() => {
      result.current.dismiss("normal");
      vi.advanceTimersByTime(10_000);
    });
    expect(result.current.toasts).toHaveLength(0);

    act(() => {
      toast({ id: "normal", title: "First" });
    });
    expect(result.current.toasts).toHaveLength(1);
  });

  it("does not dedupe a once-toast without a stable id", () => {
    const { result } = renderHook(() => useToast());

    act(() => {
      toast({ once: true, title: "No id" });
    });
    act(() => {
      result.current.dismiss();
      vi.advanceTimersByTime(10_000);
    });
    expect(result.current.toasts).toHaveLength(0);

    act(() => {
      toast({ once: true, title: "No id" });
    });
    expect(result.current.toasts).toHaveLength(1);
  });
});