Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-router): loader promise not resolving when error thrown in params.parse #2682

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/react-router/tests/errorComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createRootRoute,
createRoute,
createRouter,
notFound,
} from '../src'
import type { ErrorComponentProps } from '../src'

Expand Down Expand Up @@ -129,3 +130,69 @@ describe.each([{ preload: false }, { preload: 'intent' }] as const)(
})
},
)

describe('notFoundComponent is rendered when an error is thrown in params.parse', () => {
test('displays notFoundComponent when error is thrown in params.parse', async () => {
const rootRoute = createRootRoute({
component: function Root() {
return <div>Root</div>
},
notFoundComponent: function NotFound() {
return <div>Not Found</div>
},
})

const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: function Home() {
return (
<div>
<Link to="/pizza/rotten">link to rotten pizza</Link>
</div>
)
},
})

const pizzaRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/pizza/:pizzaType',
component: function Pizza() {
return <div>Pizza</div>
},
params: {
parse: (p) => {
if (p.pizzaType === 'rotten') {
throw new Error('404 No rotten pizzas')
}
return { pizzaType: p.pizzaType }
},
stringify: (p) => ({ pizzaType: p.pizzaType }),
},
onError: () => {
throw notFound()
},
})

const routeTree = rootRoute.addChildren([indexRoute, pizzaRoute])

const router = createRouter({
routeTree,
})

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

const linkToRottenPizza = await screen.findByRole('link', {
name: 'link to rotten pizza',
})

expect(linkToRottenPizza).toBeInTheDocument()
fireEvent.mouseOver(linkToRottenPizza)
fireEvent.click(linkToRottenPizza)

const notFoundComponent = await screen.findByText('Not Found', undefined, {
timeout: 750,
})
expect(notFoundComponent).toBeInTheDocument()
})
})
Loading