Skip to content

Commit

Permalink
tests: reproducer for #2043
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Jul 27, 2024
1 parent 3f6ffa5 commit af57d78
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions packages/react-router/tests/link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,78 @@ describe('Link', () => {
expect(onError).toHaveBeenCalledOnce()
})

test('when navigating away from a route with a loader that errors', async () => {
const postsOnError = vi.fn()
const indexOnError = vi.fn()
const rootRoute = createRootRoute({
component: () => (
<>
<div>
<Link to="/">Index</Link> <Link to="/posts">Posts</Link>
</div>
<hr />
<Outlet />
</>
),
})
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return (
<>
<h1>Index</h1>
</>
)
},
onError: indexOnError,
errorComponent: () => <span>IndexError</span>,
})

const error = new Error('Something went wrong!')

const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'posts',
loaderDeps: (opts) => ({ page: opts.search }),
loader: () => {
throw error
},
onError: postsOnError,
errorComponent: () => <span>PostsError</span>,
component: () => {
return (
<>
<h1>Posts</h1>
</>
)
},
})

const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
})

render(<RouterProvider router={router} />)

const postsLink = await screen.findByRole('link', { name: 'Posts' })

fireEvent.click(postsLink)

const postsErrorText = await screen.findByText('PostsError')
expect(postsErrorText).toBeInTheDocument()

expect(postsOnError).toHaveBeenCalledOnce()
expect(postsOnError).toHaveBeenCalledWith(error)

const indexLink = await screen.findByRole('link', { name: 'Index' })
fireEvent.click(indexLink)

const indexErrorText = await screen.findByText('IndexError')
expect(indexOnError).not.toHaveBeenCalledOnce()
expect(indexErrorText).not.toBeInTheDocument()
})

test('when navigating to /posts with a beforeLoad that redirects', async () => {
const rootRoute = createRootRoute()
const indexRoute = createRoute({
Expand Down

0 comments on commit af57d78

Please sign in to comment.