Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import {
3 cleanup,
4 configure,
5 fireEvent,
6 render,
7 screen,
8} from '@testing-library/react'
9import {
10 Link,
11 RouterProvider,
12 createBrowserHistory,
13 createMemoryHistory,
14 createRootRoute,
15 createRoute,
16 createRouter,
17} from '../src'
18import type { RouterHistory } from '../src'
19β
20afterEach(() => {
21 vi.resetAllMocks()
22 cleanup()
23})
24β
25function createTestRouter(initialHistory?: RouterHistory) {
26 const history =
27 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
28β
29 const rootRoute = createRootRoute({})
30 const indexRoute = createRoute({
31 getParentRoute: () => rootRoute,
32 path: '/',
33 component: () => (
34 <div>
35 <p>Index Route</p>
36 <Link to="/heavy">Link to heavy</Link>
37 </div>
38 ),
39 })
40β
41 const heavyRoute = createRoute({
42 getParentRoute: () => rootRoute,
43 path: '/heavy',
44 }).lazy(() => import('./lazy/heavy').then((d) => d.default('/heavy')))
45β
46 const routeTree = rootRoute.addChildren([indexRoute, heavyRoute])
47β
48 const router = createRouter({ routeTree, history })
49β
50 return {
51 router,
52 routes: { indexRoute, heavyRoute },
53 }
54}
55β
56describe('preload: matched routes', { timeout: 20000 }, () => {
57 configure({ reactStrictMode: true })
58β
59 it('should wait for lazy options to be streamed in before ', async () => {
60 const { router } = createTestRouter(
61 createMemoryHistory({ initialEntries: ['/'] }),
62 )
63β
64 await router.load()
65β
66 // Preload the route and navigate to it
67 router.preloadRoute({ to: '/heavy' })
68 await router.navigate({ to: '/heavy' })
69β
70 await router.invalidate()
71β
72 expect(router.state.location.pathname).toBe('/heavy')
73β
74 const lazyRoute = router.routesByPath['/heavy']
75β
76 expect(lazyRoute.options.component).toBeDefined()
77 })
78β
79 it('should render the heavy/lazy component', async () => {
80 const { router } = createTestRouter(createBrowserHistory())
81β
82 render(<RouterProvider router={router} />)
83β
84 const linkToHeavy = await screen.findByText('Link to heavy')
85 expect(linkToHeavy).toBeInTheDocument()
86β
87 expect(router.state.location.pathname).toBe('/')
88 expect(window.location.pathname).toBe('/')
89β
90 // click the link to navigate to the heavy route
91 fireEvent.click(linkToHeavy)
92β
93 const heavyElement = await screen.findByText('I am sooo heavy')
94β
95 expect(heavyElement).toBeInTheDocument()
96β
97 expect(router.state.location.pathname).toBe('/heavy')
98 expect(window.location.pathname).toBe('/heavy')
99β
100 const lazyRoute = router.routesByPath['/heavy']
101 expect(lazyRoute.options.component).toBeDefined()
102 })
103β
104 it('should render a lazy route errorComponent when the loader throws on first load', async () => {
105 const history = createMemoryHistory({ initialEntries: ['/lazy-error'] })
106β
107 const rootRoute = createRootRoute()
108 const lazyErrorRoute = createRoute({
109 getParentRoute: () => rootRoute,
110 path: '/lazy-error',
111 loader: () => {
112 throw new Error('lazy error')
113 },
114 }).lazy(() => import('./lazy/error').then((d) => d.Route('/lazy-error')))
115β
116 const router = createRouter({
117 routeTree: rootRoute.addChildren([lazyErrorRoute]),
118 history,
119 })
120β
121 render(<RouterProvider router={router} />)
122β
123 expect(
124 await screen.findByText('Lazy Error: lazy error'),
125 ).toBeInTheDocument()
126 expect(screen.queryByText('About route content')).not.toBeInTheDocument()
127 })
128})