Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library'
3import {
4 Link,
5 RouterProvider,
6 createBrowserHistory,
7 createMemoryHistory,
8 createRootRoute,
9 createRoute,
10 createRouter,
11} from '../src'
12import type { RouterHistory } from '../src'
13β
14afterEach(() => {
15 vi.resetAllMocks()
16 cleanup()
17})
18β
19function createTestRouter(initialHistory?: RouterHistory) {
20 const history =
21 initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })
22β
23 const rootRoute = createRootRoute({})
24 const indexRoute = createRoute({
25 getParentRoute: () => rootRoute,
26 path: '/',
27 component: () => (
28 <div>
29 <p>Index Route</p>
30 <Link to="/heavy">Link to heavy</Link>
31 </div>
32 ),
33 })
34β
35 const heavyRoute = createRoute({
36 getParentRoute: () => rootRoute,
37 path: '/heavy',
38 }).lazy(() => import('./lazy/heavy').then((d) => d.default('/heavy')))
39β
40 const routeTree = rootRoute.addChildren([indexRoute, heavyRoute])
41β
42 const router = createRouter({ routeTree, history })
43β
44 return {
45 router,
46 routes: { indexRoute, heavyRoute },
47 }
48}
49β
50describe('preload: matched routes', { timeout: 20000 }, () => {
51 it('should wait for lazy options to be streamed in before ', async () => {
52 const { router } = createTestRouter(
53 createMemoryHistory({ initialEntries: ['/'] }),
54 )
55β
56 await router.load()
57β
58 // Preload the route and navigate to it
59 router.preloadRoute({ to: '/heavy' })
60 await router.navigate({ to: '/heavy' })
61β
62 await router.invalidate()
63β
64 expect(router.state.location.pathname).toBe('/heavy')
65β
66 const lazyRoute = router.routesByPath['/heavy']
67β
68 expect(lazyRoute.options.component).toBeDefined()
69 })
70β
71 it('should render the heavy/lazy component', async () => {
72 const { router } = createTestRouter(createBrowserHistory())
73β
74 render(() => <RouterProvider router={router} />)
75β
76 const linkToHeavy = await screen.findByText('Link to heavy')
77 expect(linkToHeavy).toBeInTheDocument()
78β
79 expect(router.state.location.pathname).toBe('/')
80 expect(window.location.pathname).toBe('/')
81β
82 // click the link to navigate to the heavy route
83 fireEvent.click(linkToHeavy)
84β
85 const heavyElement = await screen.findByText('I am sooo heavy')
86β
87 expect(heavyElement).toBeInTheDocument()
88β
89 expect(router.state.location.pathname).toBe('/heavy')
90 expect(window.location.pathname).toBe('/heavy')
91β
92 const lazyRoute = router.routesByPath['/heavy']
93 expect(lazyRoute.options.component).toBeDefined()
94 })
95})