|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { ErrorPage } from "./error-page-client"; |
| 5 | + |
| 6 | +describe("ErrorPage", () => { |
| 7 | + it("logs errors to console on mount", () => { |
| 8 | + const consoleError = vi |
| 9 | + .spyOn(console, "error") |
| 10 | + .mockImplementation(() => {}); |
| 11 | + const error = new Error("Test error"); |
| 12 | + |
| 13 | + render(<ErrorPage error={error} reset={vi.fn()} />); |
| 14 | + |
| 15 | + expect(consoleError).toHaveBeenCalledWith(error); |
| 16 | + consoleError.mockRestore(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("displays 'Something went wrong' title", () => { |
| 20 | + render(<ErrorPage error={new Error("Test")} reset={vi.fn()} />); |
| 21 | + |
| 22 | + expect( |
| 23 | + screen.getByRole("heading", { name: /something went wrong/i }), |
| 24 | + ).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("displays error description", () => { |
| 28 | + render(<ErrorPage error={new Error("Test")} reset={vi.fn()} />); |
| 29 | + |
| 30 | + expect( |
| 31 | + screen.getByText(/an unexpected error occurred/i), |
| 32 | + ).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("calls reset function when Try again button is clicked", async () => { |
| 36 | + const user = userEvent.setup(); |
| 37 | + const reset = vi.fn(); |
| 38 | + |
| 39 | + render(<ErrorPage error={new Error("Test")} reset={reset} />); |
| 40 | + |
| 41 | + await user.click(screen.getByRole("button", { name: /try again/i })); |
| 42 | + |
| 43 | + expect(reset).toHaveBeenCalledTimes(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not expose error message to user", () => { |
| 47 | + const error = new Error("Sensitive internal error details"); |
| 48 | + |
| 49 | + render(<ErrorPage error={error} reset={vi.fn()} />); |
| 50 | + |
| 51 | + expect( |
| 52 | + screen.queryByText(/sensitive internal error/i), |
| 53 | + ).not.toBeInTheDocument(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments