@@ -2,6 +2,7 @@ import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library'
22import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
33import { z } from 'zod'
44
5+ import { createEffect , onMount } from 'solid-js'
56import {
67 Link ,
78 Outlet ,
@@ -2392,6 +2393,64 @@ describe('useRouteContext in the component', () => {
23922393 expect ( content ) . toBeInTheDocument ( )
23932394 } )
23942395
2396+ // Note: This test passes in solid-router but fails in react-router, even
2397+ // though in issue (GitHub #6040), the bug manifests in both routers.
2398+ test ( 'route context, (sleep in beforeLoad), with immediate navigation' , async ( ) => {
2399+ const contextValues : Array < { data : string } > = [ ]
2400+
2401+ const rootRoute = createRootRoute ( {
2402+ beforeLoad : async ( ) => {
2403+ await sleep ( WAIT_TIME )
2404+ return { data : 'context-from-beforeLoad' }
2405+ } ,
2406+ shellComponent : ( ) => {
2407+ const context = rootRoute . useRouteContext ( )
2408+
2409+ // Track context value at render time
2410+ createEffect ( ( ) => {
2411+ const contextValue : { data : string } = context ( )
2412+ contextValues . push ( contextValue )
2413+ } )
2414+
2415+ return < Outlet />
2416+ } ,
2417+ } )
2418+
2419+ const indexRoute = createRoute ( {
2420+ getParentRoute : ( ) => rootRoute ,
2421+ path : '/' ,
2422+ component : ( ) => {
2423+ const navigate = indexRoute . useNavigate ( )
2424+
2425+ // Navigate away immediately on mount
2426+ onMount ( ( ) => {
2427+ navigate ( { to : '/other' } )
2428+ } )
2429+
2430+ return < div > Index page</ div >
2431+ } ,
2432+ } )
2433+
2434+ const otherRoute = createRoute ( {
2435+ getParentRoute : ( ) => rootRoute ,
2436+ path : '/other' ,
2437+ component : ( ) => < div > Other page</ div > ,
2438+ } )
2439+
2440+ const routeTree = rootRoute . addChildren ( [ indexRoute , otherRoute ] )
2441+ const router = createRouter ( { routeTree, history } )
2442+
2443+ render ( ( ) => < RouterProvider router = { router } /> )
2444+
2445+ // Wait for navigation to complete
2446+ await screen . findByText ( 'Other page' )
2447+
2448+ const allContextsValid = contextValues . every (
2449+ ( c ) => c . data === 'context-from-beforeLoad' ,
2450+ )
2451+ expect ( allContextsValid ) . toBe ( true )
2452+ } )
2453+
23952454 test ( 'route context (sleep in loader), present root route' , async ( ) => {
23962455 const rootRoute = createRootRoute ( {
23972456 loader : async ( ) => {
0 commit comments