|
| 1 | +import { addToast } from '@heroui/toast' |
| 2 | +import * as Sentry from '@sentry/nextjs' |
| 3 | +import { screen, fireEvent } from '@testing-library/react' |
| 4 | +import { useRouter } from 'next/navigation' |
| 5 | +import { render } from 'wrappers/testUtil' |
| 6 | +import GlobalError, { |
| 7 | + AppError, |
| 8 | + ERROR_CONFIGS, |
| 9 | + ErrorDisplay, |
| 10 | + ErrorWrapper, |
| 11 | + handleAppError, |
| 12 | +} from 'app/global-error' |
| 13 | + |
| 14 | +// Mocks |
| 15 | +jest.mock('next/navigation', () => ({ |
| 16 | + useRouter: jest.fn(), |
| 17 | +})) |
| 18 | +jest.mock('@sentry/nextjs', () => ({ |
| 19 | + captureException: jest.fn(), |
| 20 | + ErrorBoundary: ({ _fallback, children }) => <>{children}</>, |
| 21 | +})) |
| 22 | +jest.mock('@heroui/toast', () => ({ |
| 23 | + addToast: jest.fn(), |
| 24 | +})) |
| 25 | + |
| 26 | +describe('ErrorDisplay Component', () => { |
| 27 | + test('renders correct error details', () => { |
| 28 | + render(<ErrorDisplay statusCode={404} title="Not Found" message="Page not found" />) |
| 29 | + |
| 30 | + expect(screen.getByText('404')).toBeInTheDocument() |
| 31 | + expect(screen.getByText('Not Found')).toBeInTheDocument() |
| 32 | + expect(screen.getByText('Page not found')).toBeInTheDocument() |
| 33 | + }) |
| 34 | + |
| 35 | + test('navigates to home on button press', () => { |
| 36 | + const pushMock = jest.fn() |
| 37 | + ;(useRouter as jest.Mock).mockReturnValue({ push: pushMock }) |
| 38 | + |
| 39 | + render(<ErrorDisplay {...ERROR_CONFIGS['404']} />) |
| 40 | + |
| 41 | + fireEvent.click(screen.getByText('Return To Home')) |
| 42 | + expect(pushMock).toHaveBeenCalledWith('/') |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('handleAppError Function', () => { |
| 47 | + beforeEach(() => { |
| 48 | + jest.clearAllMocks() |
| 49 | + }) |
| 50 | + |
| 51 | + test('handles AppError without Sentry for 404', () => { |
| 52 | + const error = new AppError(404, 'Page not found') |
| 53 | + handleAppError(error) |
| 54 | + |
| 55 | + expect(Sentry.captureException).not.toHaveBeenCalled() |
| 56 | + expect(addToast).toHaveBeenCalledWith( |
| 57 | + expect.objectContaining({ |
| 58 | + title: 'Page Not Found', |
| 59 | + description: 'Page not found', |
| 60 | + }) |
| 61 | + ) |
| 62 | + }) |
| 63 | + |
| 64 | + test('handles unknown error and calls Sentry for 500+', () => { |
| 65 | + handleAppError('Unknown crash') |
| 66 | + |
| 67 | + expect(Sentry.captureException).toHaveBeenCalled() |
| 68 | + expect(addToast).toHaveBeenCalledWith( |
| 69 | + expect.objectContaining({ |
| 70 | + description: 'An unexpected server error occurred.', |
| 71 | + shouldShowTimeoutProgress: true, |
| 72 | + timeout: 5000, |
| 73 | + title: 'Server Error', |
| 74 | + }) |
| 75 | + ) |
| 76 | + }) |
| 77 | + |
| 78 | + test('handles normal Error instance', () => { |
| 79 | + const err = new Error('Oops') |
| 80 | + handleAppError(err) |
| 81 | + |
| 82 | + expect(Sentry.captureException).toHaveBeenCalledWith(err) |
| 83 | + expect(addToast).toHaveBeenCalledWith( |
| 84 | + expect.objectContaining({ |
| 85 | + description: 'Oops', |
| 86 | + }) |
| 87 | + ) |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +describe('AppError class', () => { |
| 92 | + test('should extend Error properly', () => { |
| 93 | + const err = new AppError(403, 'Forbidden') |
| 94 | + expect(err).toBeInstanceOf(Error) |
| 95 | + expect(err.name).toBe('AppError') |
| 96 | + expect(err.message).toBe('Forbidden') |
| 97 | + expect(err.statusCode).toBe(403) |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +describe('GlobalError component', () => { |
| 102 | + test('renders 500 fallback and calls Sentry', () => { |
| 103 | + const error = new Error('Critical failure') |
| 104 | + render(<GlobalError error={error} />) |
| 105 | + |
| 106 | + expect(Sentry.captureException).toHaveBeenCalledWith(error) |
| 107 | + expect(screen.getByText('Server Error')).toBeInTheDocument() |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe('ErrorWrapper component', () => { |
| 112 | + test('renders children without crashing', () => { |
| 113 | + render( |
| 114 | + <ErrorWrapper> |
| 115 | + <div>App Content</div> |
| 116 | + </ErrorWrapper> |
| 117 | + ) |
| 118 | + |
| 119 | + expect(screen.getByText('App Content')).toBeInTheDocument() |
| 120 | + }) |
| 121 | +}) |
0 commit comments