From 010733956032830ecd855312a41ec948d61a0adc Mon Sep 17 00:00:00 2001 From: Igor Golovin Date: Thu, 24 Oct 2024 14:57:46 -0500 Subject: [PATCH 1/2] tests: reproducer for #2547 --- packages/react-router/tests/link.test.tsx | 117 +++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx index 4d2e1d2dae..519be0c426 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -31,7 +31,7 @@ import { useParams, useRouteContext, useRouterState, - useSearch, + useSearch, notFound, } from '../src' import { getIntersectionObserverMock, @@ -1194,6 +1194,121 @@ describe('Link', () => { expect(notFoundComponent).not.toBeCalled() }) + + test('404 thrown in child will bubble up to the root without async loader', async () => { + const rootRoute = createRootRoute() + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + notFoundComponent: () => ( +
+ Root notFoundComponent +
+ ), + component: () => { + return ( + <> +

Index

+ + Non existing post + + + ) + }, + }) + + const postRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '$postId', + params: { + parse: (p) => { + if (p.postId === 'nonExistingPost') { + throw new Error('Post not exists'); + } + return { + postId: p.postId, + }; + }, + stringify: (p) => ({ postId: p.postId }), + }, + onError: () => { + throw notFound(); + }, + component: () =>
Existing Post
, + }) + + const router = createRouter({ + routeTree: rootRoute.addChildren([indexRoute, postRoute]), + }) + + render() + + const postsLink = await screen.findByRole('link', { name: 'Non existing post' }) + fireEvent.click(postsLink) + + const errorText = await screen.findByText('Root notFoundComponent') + expect(errorText).toBeInTheDocument() + }) + + test('404 thrown in child will bubble up to the root with async loader', async () => { + const rootRoute = createRootRoute() + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + notFoundComponent: () => ( +
+ Root notFoundComponent +
+ ), + loader: async () => { + await sleep(0); + return { ok: true }; + }, + component: () => { + return ( + <> +

Index

+ + Non existing post + + + ) + }, + }) + + const postRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '$postId', + params: { + parse: (p) => { + if (p.postId === 'nonExistingPost') { + throw new Error('Post not exists'); + } + return { + postId: p.postId, + }; + }, + stringify: (p) => ({ postId: p.postId }), + }, + onError: () => { + throw notFound(); + }, + component: () =>
Existing Post
, + }) + + const router = createRouter({ + routeTree: rootRoute.addChildren([indexRoute, postRoute]), + }) + + render() + + const postsLink = await screen.findByRole('link', { name: 'Non existing post' }) + fireEvent.click(postsLink) + + const errorText = await screen.findByText('Root notFoundComponent') + expect(errorText).toBeInTheDocument() + }) + test('when navigating to /posts with params', async () => { const rootRoute = createRootRoute() const indexRoute = createRoute({ From 15230e851b6d868e750ab08eeeefb2a8bf9770af Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 26 Oct 2024 00:51:17 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- packages/react-router/tests/link.test.tsx | 60 ++++++++++------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx index 519be0c426..f4c990b6d4 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -31,7 +31,8 @@ import { useParams, useRouteContext, useRouterState, - useSearch, notFound, + useSearch, + notFound, } from '../src' import { getIntersectionObserverMock, @@ -1194,25 +1195,18 @@ describe('Link', () => { expect(notFoundComponent).not.toBeCalled() }) - test('404 thrown in child will bubble up to the root without async loader', async () => { const rootRoute = createRootRoute() const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', - notFoundComponent: () => ( -
- Root notFoundComponent -
- ), + notFoundComponent: () =>
Root notFoundComponent
, component: () => { return ( - <> -

Index

- - Non existing post - - + <> +

Index

+ Non existing post + ) }, }) @@ -1223,16 +1217,16 @@ describe('Link', () => { params: { parse: (p) => { if (p.postId === 'nonExistingPost') { - throw new Error('Post not exists'); + throw new Error('Post not exists') } return { postId: p.postId, - }; + } }, stringify: (p) => ({ postId: p.postId }), }, onError: () => { - throw notFound(); + throw notFound() }, component: () =>
Existing Post
, }) @@ -1243,7 +1237,9 @@ describe('Link', () => { render() - const postsLink = await screen.findByRole('link', { name: 'Non existing post' }) + const postsLink = await screen.findByRole('link', { + name: 'Non existing post', + }) fireEvent.click(postsLink) const errorText = await screen.findByText('Root notFoundComponent') @@ -1255,23 +1251,17 @@ describe('Link', () => { const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', - notFoundComponent: () => ( -
- Root notFoundComponent -
- ), + notFoundComponent: () =>
Root notFoundComponent
, loader: async () => { - await sleep(0); - return { ok: true }; + await sleep(0) + return { ok: true } }, component: () => { return ( - <> -

Index

- - Non existing post - - + <> +

Index

+ Non existing post + ) }, }) @@ -1282,16 +1272,16 @@ describe('Link', () => { params: { parse: (p) => { if (p.postId === 'nonExistingPost') { - throw new Error('Post not exists'); + throw new Error('Post not exists') } return { postId: p.postId, - }; + } }, stringify: (p) => ({ postId: p.postId }), }, onError: () => { - throw notFound(); + throw notFound() }, component: () =>
Existing Post
, }) @@ -1302,7 +1292,9 @@ describe('Link', () => { render() - const postsLink = await screen.findByRole('link', { name: 'Non existing post' }) + const postsLink = await screen.findByRole('link', { + name: 'Non existing post', + }) fireEvent.click(postsLink) const errorText = await screen.findByText('Root notFoundComponent')