|
| 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