import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import React from "react";
import { ErrorBoundary } from "./error-boundary";
afterEach(cleanup);
describe("ErrorBoundary Component", () => {
    const ThrowError = ({ message }) => {
        throw new Error(message);
    };
    it("should render children when no error occurs", () => {
        render(<ErrorBoundary>
                <div>Safe Content</div>
            </ErrorBoundary>);
        expect(screen.getByText("Safe Content")).toBeTruthy();
    });
    it("should catch error and render fallback UI", () => {
        // Suppress console.error
        const consoleErrorMock = vi.spyOn(console, "error").mockImplementation(() => { });
        render(<ErrorBoundary>
                <ThrowError message="Boom!"/>
            </ErrorBoundary>);
        expect(screen.getByText("Sorry.. there was an error: Error")).toBeTruthy();
        expect(screen.getByText("Boom!")).toBeTruthy();
        // Verify console.error was called
        expect(consoleErrorMock).toHaveBeenCalled();
        consoleErrorMock.mockRestore();
    });
});
