11import * as React from 'react'
2- import { cleanup , render , screen } from '@testing-library/react'
2+ import { act , cleanup , render , screen } from '@testing-library/react'
3+ import { createControlledPromise } from '@tanstack/router-core'
34import { afterEach , expect , test , vi } from 'vitest'
45
56import {
@@ -16,6 +17,7 @@ import { sleep } from './utils'
1617afterEach ( ( ) => {
1718 vi . restoreAllMocks ( )
1819 cleanup ( )
20+ vi . useRealTimers ( )
1921} )
2022
2123// https://github.com/TanStack/router/issues/7367
@@ -76,3 +78,126 @@ test('immediate pending spinner (pendingMs: 0 + pendingMinMs) with root beforeLo
7678 expect ( router . state . location . pathname ) . toBe ( '/welcome' )
7779 expect ( consoleError ) . not . toHaveBeenCalled ( )
7880} )
81+
82+ test ( 'a compatible SPA redirect preserves the acknowledged pending minimum' , async ( ) => {
83+ vi . useFakeTimers ( )
84+ vi . setSystemTime ( 0 )
85+ const redirectReady = createControlledPromise < void > ( )
86+ let shouldRedirect = true
87+
88+ const rootRoute = createRootRoute ( {
89+ component : Outlet ,
90+ pendingMs : 0 ,
91+ pendingMinMs : 100 ,
92+ pendingComponent : ( ) => < div data-testid = "pending" > loading</ div > ,
93+ beforeLoad : async ( ) => {
94+ if ( shouldRedirect ) {
95+ shouldRedirect = false
96+ await redirectReady
97+ throw redirect ( { to : '/welcome' , replace : true } )
98+ }
99+ } ,
100+ } )
101+ const indexRoute = createRoute ( {
102+ getParentRoute : ( ) => rootRoute ,
103+ path : '/' ,
104+ component : ( ) => < div > Index</ div > ,
105+ } )
106+ const welcomeRoute = createRoute ( {
107+ getParentRoute : ( ) => rootRoute ,
108+ path : '/welcome' ,
109+ component : ( ) => < div data-testid = "welcome-page" > Welcome</ div > ,
110+ } )
111+ const router = createRouter ( {
112+ routeTree : rootRoute . addChildren ( [ indexRoute , welcomeRoute ] ) ,
113+ history : createMemoryHistory ( { initialEntries : [ '/' ] } ) ,
114+ } )
115+
116+ try {
117+ render ( < RouterProvider router = { router } /> )
118+ await act ( async ( ) => {
119+ await vi . advanceTimersByTimeAsync ( 0 )
120+ } )
121+ expect ( screen . getByTestId ( 'pending' ) ) . toBeInTheDocument ( )
122+
123+ await act ( async ( ) => {
124+ await vi . advanceTimersByTimeAsync ( 25 )
125+ redirectReady . resolve ( )
126+ await vi . advanceTimersByTimeAsync ( 74 )
127+ } )
128+ expect ( screen . getByTestId ( 'pending' ) ) . toBeInTheDocument ( )
129+ expect ( screen . queryByTestId ( 'welcome-page' ) ) . not . toBeInTheDocument ( )
130+
131+ await act ( async ( ) => {
132+ await vi . advanceTimersByTimeAsync ( 5 )
133+ } )
134+ expect ( screen . getByTestId ( 'welcome-page' ) ) . toBeInTheDocument ( )
135+ } finally {
136+ redirectReady . resolve ( )
137+ await act ( async ( ) => {
138+ await vi . advanceTimersByTimeAsync ( 1_000 )
139+ } )
140+ vi . useRealTimers ( )
141+ }
142+ } )
143+
144+ test ( 'an incompatible SPA redirect does not inherit the pending minimum' , async ( ) => {
145+ const redirectReady = createControlledPromise < void > ( )
146+ const rootRoute = createRootRoute ( { component : Outlet } )
147+ const indexRoute = createRoute ( {
148+ getParentRoute : ( ) => rootRoute ,
149+ path : '/' ,
150+ component : ( ) => < div data-testid = "index-page" > Index</ div > ,
151+ } )
152+ const sourceRoute = createRoute ( {
153+ getParentRoute : ( ) => rootRoute ,
154+ path : '/source' ,
155+ pendingMs : 0 ,
156+ pendingMinMs : 100 ,
157+ pendingComponent : ( ) => < div data-testid = "pending" > loading</ div > ,
158+ beforeLoad : async ( ) => {
159+ await redirectReady
160+ throw redirect ( { to : '/welcome' , replace : true } )
161+ } ,
162+ } )
163+ const welcomeRoute = createRoute ( {
164+ getParentRoute : ( ) => rootRoute ,
165+ path : '/welcome' ,
166+ component : ( ) => < div data-testid = "welcome-page" > Welcome</ div > ,
167+ } )
168+ const router = createRouter ( {
169+ routeTree : rootRoute . addChildren ( [ indexRoute , sourceRoute , welcomeRoute ] ) ,
170+ history : createMemoryHistory ( { initialEntries : [ '/' ] } ) ,
171+ } )
172+
173+ render ( < RouterProvider router = { router } /> )
174+ expect ( await screen . findByTestId ( 'index-page' ) ) . toBeVisible ( )
175+ vi . useFakeTimers ( )
176+ vi . setSystemTime ( 0 )
177+
178+ const navigation = router . navigate ( { to : '/source' } )
179+ try {
180+ await act ( async ( ) => {
181+ await vi . advanceTimersByTimeAsync ( 0 )
182+ } )
183+ expect ( screen . getByTestId ( 'pending' ) ) . toBeVisible ( )
184+
185+ await act ( async ( ) => {
186+ await vi . advanceTimersByTimeAsync ( 25 )
187+ redirectReady . resolve ( )
188+ await vi . advanceTimersByTimeAsync ( 5 )
189+ await navigation
190+ } )
191+
192+ expect ( Date . now ( ) ) . toBeLessThan ( 100 )
193+ expect ( screen . getByTestId ( 'welcome-page' ) ) . toBeVisible ( )
194+ expect ( screen . queryByTestId ( 'index-page' ) ) . not . toBeInTheDocument ( )
195+ expect ( screen . queryByTestId ( 'pending' ) ) . not . toBeInTheDocument ( )
196+ } finally {
197+ redirectReady . resolve ( )
198+ await act ( async ( ) => {
199+ await vi . advanceTimersByTimeAsync ( 1_000 )
200+ await navigation
201+ } )
202+ }
203+ } )
0 commit comments