import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { Component } from "react"; import { LinkButton } from "."; interface ErrorBoundaryProps { children?: React.ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; } class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, }; } static getDerivedStateFromError(error: Error) { return { hasError: true, error }; } render() { const { hasError, error } = this.state; const { children } = this.props; if (hasError && error) { // You can render any custom fallback UI return
{error.message}
; } return children; } } describe("Test for LinkButton", () => { it("renders without error", () => { const view = render( Link, ); expect(view).toBeTruthy(); }); it("should call onClick callback if supplied", async () => { const onClick = vi.fn(); render( Link , ); await userEvent.click(screen.getByText("Link")); expect(onClick).toHaveBeenCalled(); }); it("throws when href is empty", () => { const spy = vi.spyOn(console, "error"); spy.mockImplementation(() => {}); render( Link , ); expect(screen.getByTestId("error")).toHaveTextContent( "LinkButton requires a href prop", ); spy.mockRestore(); }); it("uses the elementType prop", () => { function CustomComponent(props: any) { return ; } render( Test , ); expect(screen.getByTestId("custom-component")).toBeInTheDocument(); }); describe("given elementType", () => { it("is not applied as element attribute", () => { const { container } = render( Test , ); expect(container.firstChild).not.toHaveAttribute("elementtype", "span"); }); }); });