Skip to content

Commit 60eb597

Browse files
Sheraffclaude
andcommitted
test: pin 24 community-reported issues fixed by the match-loading work, fix pendingMs-0 blank frame
Issue sweep across open issues/PRs overlapping this branch's scope. Each issue got a repro test asserting the desired behavior: - Fixed by this work (tests pass): #7602, #7457, #7367, #7635, #4684, #6221, #7379, #4696, #4444, #6107, #5106, #7638, #4572, #3179, #4112, #2980, #2905, #2182, #3928, #6371 — plus test ports pinning the intent of external PRs #7003, #7051, #6645, #7114 (via #7110 repro) and #4614. - Fixed here: #4759 — a pendingMs-0 pending publication deferred to a macrotask painted one blank frame on bare initial loads; publication is now synchronous when nothing is rendered yet (the timer path stays for displayed content, whose later snapshot is fresher). - Known gap kept as an expected failure: #5778 — preloads borrowing an active ancestor derive context from its committed snapshot, not live router.options.context; fixing means changing the read-only borrow protocol (dedicated follow-up). The documented invalidate() workaround is pinned. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent eb9a47f commit 60eb597

28 files changed

Lines changed: 2718 additions & 2 deletions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as React from 'react'
2+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
3+
import { act, cleanup, render, screen } from '@testing-library/react'
4+
import {
5+
RouterProvider,
6+
createBrowserHistory,
7+
createRootRoute,
8+
createRoute,
9+
createRouter,
10+
} from '../src'
11+
import type { RouterHistory } from '../src'
12+
13+
let history: RouterHistory
14+
15+
beforeEach(() => {
16+
history = createBrowserHistory()
17+
expect(window.location.pathname).toBe('/')
18+
})
19+
20+
afterEach(() => {
21+
history.destroy()
22+
window.history.replaceState(null, 'root', '/')
23+
vi.resetAllMocks()
24+
cleanup()
25+
})
26+
27+
// Repro for https://github.com/TanStack/router/issues/4759
28+
//
29+
// With pendingMs: 0 the pending fallback must be visible from the very first
30+
// paint of the initial load. Deferring pending publication to a macrotask
31+
// (setTimeout) leaves at least one committed render where the router outputs
32+
// nothing, which flashes the app shell background (the "red frame") before
33+
// the pending UI appears.
34+
describe('issue #4759: no blank frame before pending UI when pendingMs is 0', () => {
35+
test('pending fallback is committed on mount without waiting for a macrotask', async () => {
36+
let resolveLoader!: (value: string) => void
37+
const loaderPromise = new Promise<string>((resolve) => {
38+
resolveLoader = resolve
39+
})
40+
41+
const rootRoute = createRootRoute()
42+
const indexRoute = createRoute({
43+
getParentRoute: () => rootRoute,
44+
path: '/',
45+
pendingMs: 0,
46+
pendingMinMs: 0,
47+
pendingComponent: () => <div data-testid="pending">pending...</div>,
48+
loader: () => loaderPromise,
49+
component: () => <div data-testid="loaded">loaded</div>,
50+
})
51+
52+
const router = createRouter({
53+
routeTree: rootRoute.addChildren([indexRoute]),
54+
history,
55+
})
56+
57+
render(<RouterProvider router={router} />)
58+
59+
// Flush microtasks only — deliberately no timer/macrotask turn. Any
60+
// implementation that defers pending publication to setTimeout cannot
61+
// have published yet, which is precisely the blank painted frame from
62+
// the issue. The pending fallback must already be in the DOM here.
63+
await act(async () => {})
64+
expect(screen.getByTestId('pending')).toBeInTheDocument()
65+
66+
// Sanity: the load still completes normally afterwards.
67+
resolveLoader('done')
68+
expect(await screen.findByTestId('loaded')).toBeInTheDocument()
69+
expect(screen.queryByTestId('pending')).not.toBeInTheDocument()
70+
})
71+
})
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { act } from 'react'
2+
import { afterEach, expect, test, vi } from 'vitest'
3+
import { cleanup, render, screen } from '@testing-library/react'
4+
import {
5+
Outlet,
6+
RouterProvider,
7+
createMemoryHistory,
8+
createRootRoute,
9+
createRoute,
10+
createRouter,
11+
} from '../src'
12+
13+
afterEach(() => {
14+
vi.clearAllMocks()
15+
cleanup()
16+
})
17+
18+
// Ported from PR #7051. On old main, invalidate({ forcePending: true }) set the
19+
// active match back to status 'pending' while its loadPromise was already
20+
// settled/cleared, so MatchInner threw `undefined` and React rendered the error
21+
// boundary instead of the pending fallback. On this branch the pending throw
22+
// falls back to router.latestLoadPromise (Match.tsx getLoadPromise), so the
23+
// suspense/pending fallback must keep rendering until the reload commits.
24+
test('invalidate({ forcePending: true }) keeps rendering the pending fallback instead of the error boundary', async () => {
25+
const history = createMemoryHistory({
26+
initialEntries: ['/force-pending'],
27+
})
28+
const errorComponentRendered = vi.fn()
29+
let shouldSuspendReload = false
30+
let resolveReload: (() => void) | undefined
31+
32+
const rootRoute = createRootRoute({
33+
component: () => <Outlet />,
34+
})
35+
36+
const forcePendingRoute = createRoute({
37+
getParentRoute: () => rootRoute,
38+
path: '/force-pending',
39+
pendingMs: 0,
40+
pendingMinMs: 10,
41+
loader: async () => {
42+
if (shouldSuspendReload) {
43+
await new Promise<void>((resolve) => {
44+
resolveReload = resolve
45+
})
46+
}
47+
48+
return 'done'
49+
},
50+
component: () => (
51+
<div data-testid="force-pending-route">
52+
{forcePendingRoute.useLoaderData()}
53+
</div>
54+
),
55+
pendingComponent: () => (
56+
<div data-testid="force-pending-fallback">Pending...</div>
57+
),
58+
errorComponent: ({ error }) => {
59+
errorComponentRendered(error)
60+
return <div data-testid="force-pending-error">{String(error)}</div>
61+
},
62+
})
63+
64+
const router = createRouter({
65+
routeTree: rootRoute.addChildren([forcePendingRoute]),
66+
history,
67+
})
68+
69+
render(<RouterProvider router={router} />)
70+
71+
await act(() => router.load())
72+
expect(await screen.findByTestId('force-pending-route')).toHaveTextContent(
73+
'done',
74+
)
75+
76+
shouldSuspendReload = true
77+
act(() => {
78+
void router.invalidate({ forcePending: true })
79+
})
80+
81+
expect(
82+
await screen.findByTestId('force-pending-fallback'),
83+
).toBeInTheDocument()
84+
expect(errorComponentRendered).not.toHaveBeenCalled()
85+
expect(screen.queryByTestId('force-pending-error')).not.toBeInTheDocument()
86+
87+
act(() => {
88+
resolveReload?.()
89+
})
90+
91+
await act(() => router.latestLoadPromise)
92+
expect(await screen.findByTestId('force-pending-route')).toHaveTextContent(
93+
'done',
94+
)
95+
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as React from 'react'
2+
import { cleanup, render, screen } from '@testing-library/react'
3+
import { afterEach, expect, test, vi } from 'vitest'
4+
5+
import {
6+
Outlet,
7+
RouterProvider,
8+
createMemoryHistory,
9+
createRootRoute,
10+
createRoute,
11+
createRouter,
12+
redirect,
13+
} from '../src'
14+
import { sleep } from './utils'
15+
16+
afterEach(() => {
17+
vi.restoreAllMocks()
18+
cleanup()
19+
})
20+
21+
// https://github.com/TanStack/router/issues/7367
22+
// Root route shows a spinner immediately (pendingMs: 0) while beforeLoad
23+
// decides where to send the user, keeps it up for pendingMinMs, and then
24+
// redirects. This used to crash in MatchInnerImpl (white screen) because the
25+
// redirected match was rendered/thrown after its loadPromise was cleared.
26+
test('immediate pending spinner (pendingMs: 0 + pendingMinMs) with root beforeLoad redirect renders the target without render errors', async () => {
27+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
28+
let hasRedirected = false
29+
30+
const rootRoute = createRootRoute({
31+
component: () => <Outlet />,
32+
pendingMs: 0,
33+
pendingMinMs: 100,
34+
pendingComponent: () => <div data-testid="pending">loading</div>,
35+
errorComponent: ({ error }) => (
36+
<pre data-testid="root-error">{String(error)}</pre>
37+
),
38+
beforeLoad: async () => {
39+
await sleep(50)
40+
if (!hasRedirected) {
41+
hasRedirected = true
42+
throw redirect({ to: '/welcome', replace: true })
43+
}
44+
},
45+
})
46+
47+
const indexRoute = createRoute({
48+
getParentRoute: () => rootRoute,
49+
path: '/',
50+
component: () => <div data-testid="index-page">Index</div>,
51+
})
52+
53+
const welcomeRoute = createRoute({
54+
getParentRoute: () => rootRoute,
55+
path: '/welcome',
56+
component: () => <div data-testid="welcome-page">Welcome</div>,
57+
})
58+
59+
const router = createRouter({
60+
routeTree: rootRoute.addChildren([indexRoute, welcomeRoute]),
61+
history: createMemoryHistory({ initialEntries: ['/'] }),
62+
})
63+
64+
render(<RouterProvider router={router} />)
65+
66+
// pendingMs: 0 — the spinner must show right away.
67+
expect(await screen.findByTestId('pending')).toBeInTheDocument()
68+
69+
// The redirect must complete: the target renders, no error boundary output
70+
// and no render crash.
71+
expect(
72+
await screen.findByTestId('welcome-page', undefined, { timeout: 5_000 }),
73+
).toBeInTheDocument()
74+
expect(screen.queryByTestId('pending')).not.toBeInTheDocument()
75+
expect(screen.queryByTestId('root-error')).not.toBeInTheDocument()
76+
expect(router.state.location.pathname).toBe('/welcome')
77+
await vi.waitFor(() => expect(router.state.status).toBe('idle'))
78+
expect(consoleError).not.toHaveBeenCalled()
79+
})
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import * as React from 'react'
2+
import { cleanup, render, screen } from '@testing-library/react'
3+
import { afterEach, expect, test, vi } from 'vitest'
4+
5+
import {
6+
Outlet,
7+
RouterProvider,
8+
createMemoryHistory,
9+
createRootRoute,
10+
createRoute,
11+
createRouter,
12+
redirect,
13+
} from '../src'
14+
import { sleep } from './utils'
15+
16+
afterEach(() => {
17+
vi.restoreAllMocks()
18+
cleanup()
19+
})
20+
21+
// https://github.com/TanStack/router/issues/7457
22+
// A chain of async layout beforeLoad redirects during the very first load
23+
// (search-stripping self-redirect -> layout redirect -> child redirect) used
24+
// to leave a match rendering with a nulled loadPromise, crashing
25+
// MatchInnerImpl with an uncaught `undefined`. Pending UI is enabled for
26+
// every match (defaultPendingMs: 0) to force pending publication mid-chain.
27+
test('chained layout beforeLoad redirects on first load render the final target without throwing from MatchInner', async () => {
28+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
29+
30+
const rootRoute = createRootRoute({ component: () => <Outlet /> })
31+
32+
const userLayout = createRoute({
33+
id: 'user',
34+
getParentRoute: () => rootRoute,
35+
validateSearch: (search: Record<string, unknown>): { flag?: boolean } => ({
36+
flag: search.flag === true || search.flag === 'true' ? true : undefined,
37+
}),
38+
beforeLoad: async ({ search }) => {
39+
if (search.flag) {
40+
await sleep(20)
41+
throw redirect({
42+
to: '.',
43+
replace: true,
44+
search: (prev: any) => ({ ...prev, flag: undefined }),
45+
})
46+
}
47+
},
48+
component: () => <Outlet />,
49+
})
50+
51+
const dashboardLayout = createRoute({
52+
id: 'dashboard',
53+
getParentRoute: () => userLayout,
54+
beforeLoad: async () => {
55+
await sleep(30)
56+
throw redirect({ to: '/intro', replace: true })
57+
},
58+
component: () => <Outlet />,
59+
})
60+
61+
const homeRoute = createRoute({
62+
getParentRoute: () => dashboardLayout,
63+
path: '/home',
64+
component: () => <div data-testid="home-page">Home</div>,
65+
})
66+
67+
const introLayout = createRoute({
68+
getParentRoute: () => userLayout,
69+
path: '/intro',
70+
beforeLoad: ({ location }) => {
71+
if (location.pathname !== '/intro/step') {
72+
throw redirect({ to: '/intro/step', replace: true })
73+
}
74+
},
75+
component: () => <Outlet />,
76+
})
77+
78+
const introIndexRoute = createRoute({
79+
getParentRoute: () => introLayout,
80+
path: '/',
81+
component: () => <div data-testid="intro-index">Intro index</div>,
82+
})
83+
84+
const introStepRoute = createRoute({
85+
getParentRoute: () => introLayout,
86+
path: 'step',
87+
component: () => <div data-testid="intro-step">Intro step</div>,
88+
})
89+
90+
const router = createRouter({
91+
routeTree: rootRoute.addChildren([
92+
userLayout.addChildren([
93+
dashboardLayout.addChildren([homeRoute]),
94+
introLayout.addChildren([introIndexRoute, introStepRoute]),
95+
]),
96+
]),
97+
defaultPendingMs: 0,
98+
defaultPendingComponent: () => <div data-testid="pending">loading</div>,
99+
history: createMemoryHistory({ initialEntries: ['/home?flag=true'] }),
100+
})
101+
102+
render(<RouterProvider router={router} />)
103+
104+
expect(
105+
await screen.findByTestId('intro-step', undefined, { timeout: 5_000 }),
106+
).toBeInTheDocument()
107+
expect(router.state.location.pathname).toBe('/intro/step')
108+
await vi.waitFor(() => expect(router.state.status).toBe('idle'))
109+
expect(consoleError).not.toHaveBeenCalled()
110+
})

0 commit comments

Comments
 (0)