Skip to content

Commit 9e5200d

Browse files
committed
test: rewrite PR 7744 internals assertions
1 parent 89c4321 commit 9e5200d

8 files changed

Lines changed: 146 additions & 49 deletions

‎e2e/react-start/basic/src/routes/hydration-capped-assets/child.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export const Route = createFileRoute('/hydration-capped-assets/child')({
1414
}),
1515
scripts: ({ loaderData }) => [
1616
{
17-
children: `window.__CAPPED_ASSETS_CHILD_SCRIPT = ${JSON.stringify(
18-
loaderData?.title ?? 'missing-loader-data',
19-
)}`,
17+
children: `document.documentElement.setAttribute('data-capped-assets-child-script', ${JSON.stringify(loaderData?.title ?? 'missing-loader-data')})`,
2018
},
2119
],
2220
component: () => (

‎e2e/react-start/basic/tests/hydration-capped-assets.spec.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ test.describe('SSR hydration capped route assets', () => {
4242
await expect(
4343
page.locator('meta[name="capped-assets-child-head"]'),
4444
).toHaveCount(0)
45-
expect(
46-
await page.evaluate(() =>
47-
Boolean((window as any).__CAPPED_ASSETS_CHILD_SCRIPT),
48-
),
49-
).toBe(false)
45+
await expect(
46+
page.locator('html[data-capped-assets-child-script]'),
47+
).toHaveCount(0)
5048
})
5149
})

‎packages/router-core/tests/fatal-load-rejection.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ describe('fatal load rejection', () => {
4040
})
4141
},
4242
})
43+
const safeLoader = vi.fn(() => 'safe data')
44+
const safeRoute = new BaseRoute({
45+
getParentRoute: () => rootRoute,
46+
path: '/safe',
47+
loader: safeLoader,
48+
})
4349

4450
const router = createTestRouter({
45-
routeTree: rootRoute.addChildren([badRoute]),
51+
routeTree: rootRoute.addChildren([badRoute, safeRoute]),
4652
history: createMemoryHistory({ initialEntries: ['/bad'] }),
4753
})
4854

@@ -52,11 +58,23 @@ describe('fatal load rejection', () => {
5258
await new Promise((resolve) => setTimeout(resolve, 0))
5359
expect(unhandledRejection).toHaveBeenCalledWith(boom, expect.anything())
5460

55-
// No committed match may keep a pending loadPromise (hung Suspense).
56-
expect(router.state.matches.length).toBeGreaterThan(0)
57-
for (const match of router.state.matches) {
58-
expect(match._.loadPromise?.status ?? 'settled').not.toBe('pending')
59-
}
61+
// A fatal machinery failure is not a route-level error that can be
62+
// rendered by the matched route. Once router.load() has returned, the
63+
// public lane must be settled and the router must remain usable.
64+
expect(router.state.isLoading).toBe(false)
65+
66+
// A dangling readiness owner would strand framework consumers on the
67+
// failed lane. Prove liveness through a normal follow-up navigation
68+
// instead of inspecting match-owned promise bookkeeping.
69+
await router.navigate({ to: '/safe' })
70+
expect(router.state.location.pathname).toBe('/safe')
71+
expect(router.state.isLoading).toBe(false)
72+
expect(router.state.matches.at(-1)).toMatchObject({
73+
routeId: safeRoute.id,
74+
status: 'success',
75+
loaderData: 'safe data',
76+
})
77+
expect(safeLoader).toHaveBeenCalledTimes(1)
6078
} finally {
6179
process.off('unhandledRejection', unhandledRejection)
6280
}

‎packages/router-core/tests/issue-2182-root-loader-pending.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe('issue #2182: root loader pending UI on initial page visit', () => {
4545

4646
const load = router.load()
4747
load.catch(() => {})
48+
let loadSettled = false
49+
void load.finally(() => {
50+
loadSettled = true
51+
})
4852

4953
// Let the loader start; before pendingMs nothing is published.
5054
await vi.advanceTimersByTimeAsync(999)
@@ -60,11 +64,12 @@ describe('issue #2182: root loader pending UI on initial page visit', () => {
6064
expect(rootMatch).toBeDefined()
6165
expect(rootMatch?.status).toBe('pending')
6266
expect(rootMatch?.isFetching).toBe('loader')
63-
expect(rootMatch?._.loadPromise?.status).toBe('pending')
67+
expect(loadSettled).toBe(false)
6468

6569
// Resolving the loader completes the final commit with loader data.
6670
loaderGate.resolve({ user: 'flo' })
6771
await load
72+
expect(loadSettled).toBe(true)
6873

6974
const settledRoot = router.state.matches.find(
7075
(match) => match.routeId === rootRouteId,

‎packages/router-core/tests/issue-2905-root-beforeload-pending.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ describe('issue #2905: root beforeLoad pending UI on initial load', () => {
4444

4545
const load = router.load()
4646
load.catch(() => {})
47+
let loadSettled = false
48+
void load.finally(() => {
49+
loadSettled = true
50+
})
4751

4852
// Before the default pendingMs (1000ms) elapses, nothing is published.
4953
await vi.advanceTimersByTimeAsync(999)
@@ -57,11 +61,12 @@ describe('issue #2905: root beforeLoad pending UI on initial load', () => {
5761
)
5862
expect(rootMatch).toBeDefined()
5963
expect(rootMatch?.status).toBe('pending')
60-
expect(rootMatch?._.loadPromise?.status).toBe('pending')
64+
expect(loadSettled).toBe(false)
6165

6266
// Publication is presentation only: the load must still settle normally.
6367
beforeLoadGate.resolve({ auth: 'ok' })
6468
await load
69+
expect(loadSettled).toBe(true)
6570

6671
const settledRoot = router.state.matches.find(
6772
(match) => match.routeId === rootRouteId,

‎packages/router-core/tests/issue-5106-hydrated-notfound-boundary.test.ts‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ describe('issue #5106 - hydrating a server-committed notFound boundary', () => {
4747
component: () => 'Post',
4848
notFoundComponent: () => 'Post not found',
4949
})
50+
const safeLoader = vi.fn(() => 'safe-data')
51+
const safeRoute = new BaseRoute({
52+
getParentRoute: () => rootRoute,
53+
path: '/safe',
54+
loader: safeLoader,
55+
})
5056

5157
const router = createTestRouter({
52-
routeTree: rootRoute.addChildren([postsRoute.addChildren([postRoute])]),
58+
routeTree: rootRoute.addChildren([
59+
postsRoute.addChildren([postRoute]),
60+
safeRoute,
61+
]),
5362
history,
5463
isServer: false,
5564
})
@@ -112,16 +121,24 @@ describe('issue #5106 - hydrating a server-committed notFound boundary', () => {
112121
expect(postsLoader).not.toHaveBeenCalled()
113122
expect(postLoader).not.toHaveBeenCalled()
114123

115-
// Every match settled: an unsettled loadPromise here is the #5106 freeze -
116-
// the framework Match suspends on it and the parent never hydrates.
117-
for (const match of stateMatches) {
118-
expect(match._.dehydrated).toBeUndefined()
119-
expect(match._.loadPromise).toBeUndefined()
120-
}
121-
122124
// Hydration finished without needing a follow-up router.load().
123125
expect(router.stores.resolvedLocation.get()).toBeDefined()
124126
expect(router.stores.isLoading.get()).toBe(false)
127+
128+
// Prove the hydrated lane did not freeze the router by leaving it able to
129+
// complete an ordinary client navigation. This observes the consequence
130+
// that matters without reading hydration/readiness bookkeeping.
131+
await router.navigate({ to: '/safe' })
132+
expect(router.state.location.pathname).toBe('/safe')
133+
expect(router.state.isLoading).toBe(false)
134+
expect(router.state.matches.at(-1)).toMatchObject({
135+
routeId: safeRoute.id,
136+
status: 'success',
137+
loaderData: 'safe-data',
138+
})
139+
expect(safeLoader).toHaveBeenCalledTimes(1)
140+
expect(postsLoader).not.toHaveBeenCalled()
141+
expect(postLoader).not.toHaveBeenCalled()
125142
})
126143

127144
it('ancestor boundary replays the dehydrated notFound in the follow-up load instead of loading the omitted child', async () => {
@@ -147,9 +164,18 @@ describe('issue #5106 - hydrating a server-committed notFound boundary', () => {
147164
loader: postLoader,
148165
component: () => 'Post',
149166
})
167+
const safeLoader = vi.fn(() => 'safe-data')
168+
const safeRoute = new BaseRoute({
169+
getParentRoute: () => rootRoute,
170+
path: '/safe',
171+
loader: safeLoader,
172+
})
150173

151174
const router = createTestRouter({
152-
routeTree: rootRoute.addChildren([postsRoute.addChildren([postRoute])]),
175+
routeTree: rootRoute.addChildren([
176+
postsRoute.addChildren([postRoute]),
177+
safeRoute,
178+
]),
153179
history,
154180
isServer: false,
155181
})
@@ -210,11 +236,17 @@ describe('issue #5106 - hydrating a server-committed notFound boundary', () => {
210236
expect(postLoader).not.toHaveBeenCalled()
211237
expect(postsLoader).not.toHaveBeenCalled()
212238

213-
// The committed lane is fully settled - nothing left for React to
214-
// suspend on (the #5106 freeze).
215-
for (const match of stateMatches) {
216-
expect(match._.dehydrated).toBeUndefined()
217-
expect(match._.loadPromise).toBeUndefined()
218-
}
239+
// The replayed boundary must not strand subsequent client work.
240+
await router.navigate({ to: '/safe' })
241+
expect(router.state.location.pathname).toBe('/safe')
242+
expect(router.state.isLoading).toBe(false)
243+
expect(router.state.matches.at(-1)).toMatchObject({
244+
routeId: safeRoute.id,
245+
status: 'success',
246+
loaderData: 'safe-data',
247+
})
248+
expect(safeLoader).toHaveBeenCalledTimes(1)
249+
expect(postLoader).not.toHaveBeenCalled()
250+
expect(postsLoader).not.toHaveBeenCalled()
219251
})
220252
})

‎packages/router-core/tests/issue-6645-load-route-chunk-rejection.test.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ describe('loadRouteChunk lazy chunk rejection (PR #6645 port)', () => {
5252
await new Promise((resolve) => setTimeout(resolve, 20))
5353
expect(unhandledRejection).not.toHaveBeenCalled()
5454

55-
// The failed chunk is evicted, not poisoned forever.
56-
expect((fooRoute as any)._lazyPromise).toBeUndefined()
57-
expect((fooRoute as any)._lazyLoaded).toBeFalsy()
58-
expect((fooRoute as any)._componentsLoaded).toBeFalsy()
59-
6055
// A later pass retries the import and succeeds.
6156
await router.loadRouteChunk(fooRoute as any)
6257
expect(lazyCalls).toBe(2)
63-
expect((fooRoute as any)._lazyLoaded).toBe(true)
64-
expect((fooRoute as any)._componentsLoaded).toBe(true)
6558
expect(fooRoute.options.component).toBe(fooComponent)
6659
expect(unhandledRejection).not.toHaveBeenCalled()
60+
61+
// The successful retry is cached, so an additional consumer does not
62+
// invoke the lazy importer again.
63+
await router.loadRouteChunk(fooRoute as any)
64+
expect(lazyCalls).toBe(2)
6765
} finally {
6866
process.off('unhandledRejection', unhandledRejection)
6967
}

‎packages/vue-router/tests/pending-match-settled-promise.test.tsx‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { afterEach, expect, test } from 'vitest'
1+
import { afterEach, expect, test, vi } from 'vitest'
22
import { cleanup, render, screen } from '@testing-library/vue'
33
import { createMemoryHistory } from '@tanstack/history'
4+
import { createControlledPromise } from '@tanstack/router-core'
5+
import { hydrate as hydrateRouter } from '@tanstack/router-core/ssr/client'
46
import {
57
Outlet,
68
RouterProvider,
@@ -19,6 +21,7 @@ import {
1921

2022
afterEach(() => {
2123
cleanup()
24+
window.$_TSR = undefined
2225
})
2326

2427
function setup() {
@@ -49,27 +52,67 @@ test('pending match with a settled loadPromise renders the pending component wit
4952
.get()
5053
.find((m) => m.routeId === slowRoute.id)!
5154
expect(match.status).toBe('pending')
52-
// Simulate hydrate()/cancelMatches() settling the match-local promise while
53-
// the match stays pending.
54-
match._.loadPromise = undefined
55+
// Exercise the real path that settles active match readiness while leaving
56+
// this published snapshot pending.
57+
router.cancelMatches()
5558

5659
render(<RouterProvider router={router} />)
5760

5861
expect(await screen.findByTestId('pending-ui')).toBeTruthy()
62+
expect(screen.queryByTestId('slow-content')).toBeNull()
5963
})
6064

6165
test('hydration display match keeps rendering pending UI after mount', async () => {
62-
const { router, slowRoute } = setup()
63-
router.stores.setMatches(router.matchRoutes(router.latestLocation))
66+
const loaderGate = createControlledPromise<void>()
67+
const rootRoute = createRootRoute({
68+
component: () => <Outlet />,
69+
})
70+
const slowRoute = createRoute({
71+
getParentRoute: () => rootRoute,
72+
path: '/slow',
73+
ssr: false,
74+
loader: () => loaderGate,
75+
pendingComponent: () => <div data-testid="pending-ui">Pending</div>,
76+
component: () => <div data-testid="slow-content">Slow content</div>,
77+
})
78+
const router = createRouter({
79+
routeTree: rootRoute.addChildren([slowRoute]),
80+
history: createMemoryHistory({ initialEntries: ['/slow'] }),
81+
})
82+
const matches = router.matchRoutes(router.latestLocation)
6483

65-
const match = router.stores.matches
66-
.get()
67-
.find((m) => m.routeId === slowRoute.id)!
68-
match._displayPending = true
69-
match._.loadPromise = undefined
84+
window.$_TSR = {
85+
router: {
86+
manifest: { routes: {} },
87+
dehydratedData: {},
88+
lastMatchId: matches[0]!.id,
89+
matches: [
90+
{
91+
i: matches[0]!.id,
92+
s: 'success',
93+
ssr: true,
94+
u: Date.now(),
95+
},
96+
],
97+
},
98+
h: vi.fn(),
99+
e: vi.fn(),
100+
c: vi.fn(),
101+
p: vi.fn(),
102+
buffer: [],
103+
initialized: false,
104+
}
105+
106+
await hydrateRouter(router)
70107

71108
render(<RouterProvider router={router} />)
72109

73110
expect(await screen.findByTestId('pending-ui')).toBeTruthy()
74111
expect(screen.queryByTestId('slow-content')).toBeNull()
112+
113+
loaderGate.resolve()
114+
await router.latestLoadPromise
115+
116+
expect(await screen.findByTestId('slow-content')).toBeTruthy()
117+
expect(screen.queryByTestId('pending-ui')).toBeNull()
75118
})

0 commit comments

Comments
 (0)