@@ -27,6 +27,7 @@ import {
2727 createRouteMask ,
2828 createRouter ,
2929 getRouteApi ,
30+ notFound ,
3031 redirect ,
3132 retainSearchParams ,
3233 stripSearchParams ,
@@ -1987,6 +1988,115 @@ describe('Link', () => {
19871988 expect ( notFoundComponent ) . not . toBeCalled ( )
19881989 } )
19891990
1991+ test ( '404 thrown in child will bubble up to the root with beforeLoad' , async ( ) => {
1992+ const rootRoute = createRootRoute ( {
1993+ notFoundComponent : ( ) => < div > Root notFoundComponent</ div > ,
1994+ beforeLoad : ( ) => ( { ok : true } ) ,
1995+ } )
1996+ const indexRoute = createRoute ( {
1997+ getParentRoute : ( ) => rootRoute ,
1998+ path : '/' ,
1999+ component : ( ) => {
2000+ return (
2001+ < >
2002+ < h1 > Index</ h1 >
2003+ < Link to = "/nonExistingPost" > Non existing post</ Link >
2004+ </ >
2005+ )
2006+ } ,
2007+ } )
2008+
2009+ const postRoute = createRoute ( {
2010+ getParentRoute : ( ) => rootRoute ,
2011+ path : '$postId' ,
2012+ params : {
2013+ parse : ( p ) => {
2014+ if ( p . postId === 'nonExistingPost' ) {
2015+ throw new Error ( 'Post not exists' )
2016+ }
2017+ return {
2018+ postId : p . postId ,
2019+ }
2020+ } ,
2021+ stringify : ( p ) => ( { postId : p . postId } ) ,
2022+ } ,
2023+ onError : ( ) => {
2024+ throw notFound ( )
2025+ } ,
2026+ component : ( ) => < div > Existing Post</ div > ,
2027+ } )
2028+
2029+ const router = createRouter ( {
2030+ routeTree : rootRoute . addChildren ( [ indexRoute , postRoute ] ) ,
2031+ } )
2032+
2033+ render ( < RouterProvider router = { router } /> )
2034+
2035+ const postsLink = await screen . findByRole ( 'link' , {
2036+ name : 'Non existing post' ,
2037+ } )
2038+ await act ( ( ) => fireEvent . click ( postsLink ) )
2039+
2040+ const errorText = await screen . findByText ( 'Root notFoundComponent' )
2041+ expect ( errorText ) . toBeInTheDocument ( )
2042+ } )
2043+
2044+ test ( '404 thrown in child will bubble up to the root with async loader' , async ( ) => {
2045+ const rootRoute = createRootRoute ( {
2046+ notFoundComponent : ( ) => < div > Root notFoundComponent</ div > ,
2047+ loader : async ( ) => {
2048+ await sleep ( 0 )
2049+ return { ok : true }
2050+ } ,
2051+ } )
2052+ const indexRoute = createRoute ( {
2053+ getParentRoute : ( ) => rootRoute ,
2054+ path : '/' ,
2055+ component : ( ) => {
2056+ return (
2057+ < >
2058+ < h1 > Index</ h1 >
2059+ < Link to = "/nonExistingPost" > Non existing post</ Link >
2060+ </ >
2061+ )
2062+ } ,
2063+ } )
2064+
2065+ const postRoute = createRoute ( {
2066+ getParentRoute : ( ) => rootRoute ,
2067+ path : '$postId' ,
2068+ params : {
2069+ parse : ( p ) => {
2070+ if ( p . postId === 'nonExistingPost' ) {
2071+ throw new Error ( 'Post not exists' )
2072+ }
2073+ return {
2074+ postId : p . postId ,
2075+ }
2076+ } ,
2077+ stringify : ( p ) => ( { postId : p . postId } ) ,
2078+ } ,
2079+ onError : ( ) => {
2080+ throw notFound ( )
2081+ } ,
2082+ component : ( ) => < div > Existing Post</ div > ,
2083+ } )
2084+
2085+ const router = createRouter ( {
2086+ routeTree : rootRoute . addChildren ( [ indexRoute , postRoute ] ) ,
2087+ } )
2088+
2089+ render ( < RouterProvider router = { router } /> )
2090+
2091+ const postsLink = await screen . findByRole ( 'link' , {
2092+ name : 'Non existing post' ,
2093+ } )
2094+ await act ( ( ) => fireEvent . click ( postsLink ) )
2095+
2096+ const errorText = await screen . findByText ( 'Root notFoundComponent' )
2097+ expect ( errorText ) . toBeInTheDocument ( )
2098+ } )
2099+
19902100 test ( 'when navigating to /posts with params' , async ( ) => {
19912101 const rootRoute = createRootRoute ( )
19922102 const indexRoute = createRoute ( {
0 commit comments