diff --git a/src/App.tsx b/src/App.tsx index cd79907..7120c9d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,11 +1,12 @@ import { createBrowserRouter, RouterProvider } from 'react-router-dom' -import { MainPage } from 'components/pages' +import { ErrorPage, MainPage } from 'components/pages' const router = createBrowserRouter([ { path: '/', element: , + errorElement: , }, ]) diff --git a/src/components/pages/ErrorPage/ErrorPage.test.tsx b/src/components/pages/ErrorPage/ErrorPage.test.tsx new file mode 100644 index 0000000..d2ca2df --- /dev/null +++ b/src/components/pages/ErrorPage/ErrorPage.test.tsx @@ -0,0 +1,28 @@ +import { describe, it, expect, afterEach } from 'vitest' +import { screen, cleanup, render } from '@testing-library/react' +import { RouterProvider, createMemoryRouter } from 'react-router-dom' + +import ErrorPage from './ErrorPage' + +// Tests +describe('Renders ErrorPage correctly', async () => { + /** + * Resets all renders after each test + */ + afterEach(() => { + cleanup() + }) + + /** + * Passes - shows title correctly + */ + it('Should render the error page correctly', () => { + const router = createMemoryRouter([{ path: '/', element:
, errorElement: }], { initialEntries: ['/some/bad/route'] }) + + render() + + expect(screen.getByText('Oops!')).toBeInTheDocument() + expect(screen.getByText('Sorry, an unexpected error has occurred.')).toBeInTheDocument() + expect(screen.getByText('404 Not Found')).toBeInTheDocument() + }) +}) diff --git a/src/components/pages/ErrorPage/ErrorPage.tsx b/src/components/pages/ErrorPage/ErrorPage.tsx new file mode 100644 index 0000000..8d4184b --- /dev/null +++ b/src/components/pages/ErrorPage/ErrorPage.tsx @@ -0,0 +1,22 @@ +import { isRouteErrorResponse, useRouteError } from 'react-router-dom' + +const ErrorPage: React.FC = () => { + const error: unknown = useRouteError() + if (isRouteErrorResponse(error)) { + return ( +
+

Oops!

+

Sorry, an unexpected error has occurred.

+ {isRouteErrorResponse(error) && ( +

+ + {error.status} {error.statusText} + +

+ )} +
+ ) + } +} + +export default ErrorPage diff --git a/src/components/pages/ErrorPage/index.ts b/src/components/pages/ErrorPage/index.ts new file mode 100644 index 0000000..333b56e --- /dev/null +++ b/src/components/pages/ErrorPage/index.ts @@ -0,0 +1 @@ +export { default } from './ErrorPage' diff --git a/src/components/pages/index.ts b/src/components/pages/index.ts index 13c908d..74a0199 100644 --- a/src/components/pages/index.ts +++ b/src/components/pages/index.ts @@ -1 +1,3 @@ export { default as MainPage } from './MainPage' + +export { default as ErrorPage } from './ErrorPage'