|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 3 | +import NotFound from '@/app/not-found' |
| 4 | +import ErrorPage from '@/components/ErrorPage' |
| 5 | + |
| 6 | +// Mock PageLayout to avoid layout dependencies in unit tests |
| 7 | +vi.mock('@/components/PageLayout', () => ({ |
| 8 | + default: ({ title, description, children }: any) => ( |
| 9 | + <div> |
| 10 | + {title && <h1>{title}</h1>} |
| 11 | + {description && <p>{description}</p>} |
| 12 | + <div>{children}</div> |
| 13 | + </div> |
| 14 | + ) |
| 15 | +})) |
| 16 | + |
| 17 | +describe('Custom NotFound page', () => { |
| 18 | + it('renders title, description and home link', () => { |
| 19 | + render(<NotFound />) |
| 20 | + expect(screen.getByRole('heading', { name: 'Page not found' })).toBeInTheDocument() |
| 21 | + expect(screen.getByText(/couldn't find the page/i)).toBeInTheDocument() |
| 22 | + const back = screen.getByRole('link', { name: /back to homepage/i }) |
| 23 | + expect(back).toHaveAttribute('href', '/') |
| 24 | + }) |
| 25 | +}) |
| 26 | + |
| 27 | +describe('Custom Error page', () => { |
| 28 | + it('renders message and allows retry via reset()', () => { |
| 29 | + const reset = vi.fn() |
| 30 | + const error = new Error('Boom!') |
| 31 | + render(<ErrorPage error={error} reset={reset} />) |
| 32 | + expect(screen.getByRole('heading', { name: 'Something went wrong' })).toBeInTheDocument() |
| 33 | + // In test env NODE_ENV is usually 'test', details should be visible |
| 34 | + expect(screen.getByText(/details:/i)).toBeInTheDocument() |
| 35 | + expect(screen.getByText(/boom!/i)).toBeInTheDocument() |
| 36 | + const btn = screen.getByRole('button', { name: /try again/i }) |
| 37 | + fireEvent.click(btn) |
| 38 | + expect(reset).toHaveBeenCalled() |
| 39 | + }) |
| 40 | +}) |
0 commit comments