diff --git a/src/App.js b/src/App.js index 8d1a809..2ae2363 100644 --- a/src/App.js +++ b/src/App.js @@ -27,12 +27,14 @@ function App() { }, }}> - - }> -}> - }> - }> - + + + }> + }> + }> + }> + + ); diff --git a/src/App.test.js b/src/App.test.js index b4ef4e2..1772a1e 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -1,8 +1,29 @@ import { render, screen } from '@testing-library/react'; import App from './App'; +import ErrorBoundary from './components/ErrorBoundary'; test('renders landing page call to action', () => { render(); const buttonElement = screen.getByText(/Get Started/i); expect(buttonElement).toBeInTheDocument(); }); + +test('renders the error boundary fallback UI when a child throws', () => { + const Thrower = () => { + throw new Error('Boom'); + }; + + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + + render( + + + + ); + + expect(screen.getByText(/Something went wrong/i)).toBeInTheDocument(); + expect(screen.getByText(/Boom/i)).toBeInTheDocument(); + expect(screen.getByRole('button', { name: /Go Home/i })).toBeInTheDocument(); + + consoleErrorSpy.mockRestore(); +}); diff --git a/src/components/ErrorBoundary.js b/src/components/ErrorBoundary.js index 40315f6..12e71c1 100644 --- a/src/components/ErrorBoundary.js +++ b/src/components/ErrorBoundary.js @@ -14,28 +14,35 @@ class ErrorBoundary extends React.Component { console.error('ErrorBoundary caught:', error, errorInfo); } + handleGoHome = () => { + window.location.href = '/'; + }; + render() { if (this.state.hasError) { return (

Something went wrong

-

+

{this.state.error?.message || 'An unexpected error occurred.'}