diff --git a/packages/react-router/tests/loaders.test.tsx b/packages/react-router/tests/loaders.test.tsx index 277dc90949..23ef28d5ea 100644 --- a/packages/react-router/tests/loaders.test.tsx +++ b/packages/react-router/tests/loaders.test.tsx @@ -224,3 +224,97 @@ test('reproducer for #2053', async () => { const fooElement = await screen.findByText('fooId: 3ΚΑΠΠΑ') expect(fooElement).toBeInTheDocument() }) + +test('reproducer for #2198 - throw error from beforeLoad upon initial load', async () => { + const rootRoute = createRootRoute({}) + + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () =>
Index page
, + beforeLoad: () => { + throw new Error('Test!') + }, + errorComponent: () =>
indexErrorComponent
, + }) + + const routeTree = rootRoute.addChildren([indexRoute]) + const router = createRouter({ + routeTree, + defaultErrorComponent: () => { + return
defaultErrorComponent
+ }, + }) + + render() + + const errorElement = await screen.findByText('indexErrorComponent') + expect(errorElement).toBeInTheDocument() +}) + +test('throw error from loader upon initial load', async () => { + const rootRoute = createRootRoute({}) + + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () =>
Index page
, + loader: () => { + throw new Error('Test!') + }, + errorComponent: () =>
indexErrorComponent
, + }) + + const routeTree = rootRoute.addChildren([indexRoute]) + const router = createRouter({ + routeTree, + defaultErrorComponent: () => { + return
defaultErrorComponent
+ }, + }) + + render() + + const errorElement = await screen.findByText('indexErrorComponent') + expect(errorElement).toBeInTheDocument() +}) + +test('throw error from beforeLoad when navigating to route', async () => { + const rootRoute = createRootRoute({}) + + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () =>

Index page

link to foo
, + errorComponent: () =>
indexErrorComponent
, + }) + + const fooRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/foo', + component: () =>
Foo page
, + beforeLoad: () => { + throw new Error('Test!') + }, + errorComponent: () =>
fooErrorComponent
, + }) + + const routeTree = rootRoute.addChildren([indexRoute, fooRoute]) + const router = createRouter({ + routeTree, + defaultErrorComponent: () => { + return
defaultErrorComponent
+ }, + }) + + render() + + const linkToFoo = await screen.findByRole('link', { name: 'link to foo' }) + + expect(linkToFoo).toBeInTheDocument() + + fireEvent.click(linkToFoo) + + const indexElement = await screen.findByText('fooErrorComponent') + expect(indexElement).toBeInTheDocument() +})