import "@testing-library/jest-dom"; import { act, fireEvent, render, screen } from "@testing-library/react"; import Toast from "./Toast"; describe("Toast", () => { test("renders the message when visible", () => { render( {}} />, ); expect(screen.getByText("Saved successfully")).toBeInTheDocument(); }); test("renders nothing when not visible", () => { const { container } = render( {}} />, ); expect(container.firstChild).toBeNull(); }); test("renders the title when provided", () => { render( {}} />, ); expect(screen.getByText("Success")).toBeInTheDocument(); }); test("does not render a title element when title is omitted", () => { const { container } = render( {}} />, ); expect(container.querySelector(".toast-title")).not.toBeInTheDocument(); }); test("calls onClose when the close button is clicked", () => { const handleClose = vi.fn(); render(); fireEvent.click(screen.getByRole("button", { name: /close/i })); expect(handleClose).toHaveBeenCalledTimes(1); }); test("auto-dismisses after the specified duration", () => { vi.useFakeTimers(); const handleClose = vi.fn(); render( , ); act(() => vi.advanceTimersByTime(3000)); expect(handleClose).toHaveBeenCalledTimes(1); vi.useRealTimers(); }); test("does not auto-dismiss when duration is 0", () => { vi.useFakeTimers(); const handleClose = vi.fn(); render( , ); act(() => vi.advanceTimersByTime(10000)); expect(handleClose).not.toHaveBeenCalled(); vi.useRealTimers(); }); test("does not start the auto-dismiss timer when not visible", () => { vi.useFakeTimers(); const handleClose = vi.fn(); render( , ); act(() => vi.advanceTimersByTime(3000)); expect(handleClose).not.toHaveBeenCalled(); vi.useRealTimers(); }); test("applies the correct variant class", () => { const { container } = render( {}} />, ); expect(container.firstChild).toHaveClass("toast-error"); }); test("applies the correct position class", () => { const { container } = render( {}} />, ); expect(container.firstChild).toHaveClass("toast-bottom-left"); }); test("has role alert for accessibility", () => { render( {}} />); expect(screen.getByRole("alert")).toBeInTheDocument(); }); test("has an accessible label on the close button", () => { render( {}} />); expect(screen.getByRole("button", { name: /close/i })).toBeInTheDocument(); }); test("defaults to the info variant", () => { const { container } = render( {}} />, ); expect(container.firstChild).toHaveClass("toast-info"); }); test("defaults to the top-right position", () => { const { container } = render( {}} />, ); expect(container.firstChild).toHaveClass("toast-top-right"); }); });