Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Outlet.test.tsx

146 lines4.4 KB

1import { afterEach, expect, test, vi } from 'vitest'
2import { cleanup, render, screen } from '@solidjs/testing-library'
3import { createMemoryHistory } from '@tanstack/history'
4import { createControlledPromise, notFound } from '@tanstack/router-core'
5import {
6 Outlet,
7 RouterProvider,
8 createRootRoute,
9 createRoute,
10 createRouter,
11} from '../src'
12​
13const outletWarning = (
14 component: 'pendingComponent' | 'errorComponent' | 'notFoundComponent',
15) =>
16 `Warning: An <Outlet /> was rendered inside a ${component}. <Outlet /> should only be rendered inside a route component.`
17​
18afterEach(() => {
19 cleanup()
20 vi.restoreAllMocks()
21})
22​
23test('does not warn when Outlet is rendered inside a route component', async () => {
24 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
25 const rootRoute = createRootRoute({
26 component: () => (
27 <>
28 <span>Root route</span>
29 <Outlet />
30 </>
31 ),
32 })
33 const indexRoute = createRoute({
34 getParentRoute: () => rootRoute,
35 path: '/',
36 component: () => <span>Index route</span>,
37 })
38 const router = createRouter({
39 routeTree: rootRoute.addChildren([indexRoute]),
40 history: createMemoryHistory({ initialEntries: ['/'] }),
41 })
42​
43 render(() => <RouterProvider router={router} />)
44​
45 expect(await screen.findByText('Index route')).toBeInTheDocument()
46 expect(warn).not.toHaveBeenCalled()
47})
48​
49test('warns when Outlet is rendered inside a pendingComponent', async () => {
50 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
51 const pending = createControlledPromise<void>()
52 const rootRoute = createRootRoute({ component: Outlet })
53 const indexRoute = createRoute({
54 getParentRoute: () => rootRoute,
55 path: '/',
56 component: () => <span>Index route</span>,
57 })
58 const pendingRoute = createRoute({
59 getParentRoute: () => rootRoute,
60 path: '/pending',
61 loader: () => pending,
62 pendingMs: 0,
63 pendingComponent: () => (
64 <>
65 <span>Pending route</span>
66 <Outlet />
67 </>
68 ),
69 component: () => <span>Resolved route</span>,
70 })
71 const router = createRouter({
72 routeTree: rootRoute.addChildren([indexRoute, pendingRoute]),
73 history: createMemoryHistory({ initialEntries: ['/'] }),
74 })
75​
76 render(() => <RouterProvider router={router} />)
77 await screen.findByText('Index route')
78​
79 const navigation = router.navigate({ to: '/pending' })
80 expect(await screen.findByText('Pending route')).toBeInTheDocument()
81 pending.resolve()
82 await navigation
83​
84 expect(warn).toHaveBeenCalledWith(outletWarning('pendingComponent'))
85})
86​
87test('warns when Outlet is rendered inside an errorComponent', async () => {
88 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
89 const rootRoute = createRootRoute({ component: Outlet })
90 const indexRoute = createRoute({
91 getParentRoute: () => rootRoute,
92 path: '/',
93 loader: () => {
94 throw new Error('Loader failed')
95 },
96 errorComponent: () => (
97 <>
98 <span>Error route</span>
99 <Outlet />
100 </>
101 ),
102 })
103 const router = createRouter({
104 routeTree: rootRoute.addChildren([indexRoute]),
105 history: createMemoryHistory({ initialEntries: ['/'] }),
106 })
107​
108 render(() => <RouterProvider router={router} />)
109​
110 expect(await screen.findByText('Error route')).toBeInTheDocument()
111 expect(warn).toHaveBeenCalledWith(outletWarning('errorComponent'))
112})
113​
114test('warns when Outlet is rendered inside a notFoundComponent', async () => {
115 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
116 const rootRoute = createRootRoute({ component: Outlet })
117 const indexRoute = createRoute({
118 getParentRoute: () => rootRoute,
119 path: '/',
120 component: () => <span>Index route</span>,
121 })
122 const notFoundRoute = createRoute({
123 getParentRoute: () => rootRoute,
124 path: '/not-found',
125 component: () => {
126 throw notFound()
127 },
128 notFoundComponent: () => (
129 <>
130 <span>Not found route</span>
131 <Outlet />
132 </>
133 ),
134 })
135 const router = createRouter({
136 routeTree: rootRoute.addChildren([indexRoute, notFoundRoute]),
137 history: createMemoryHistory({ initialEntries: ['/'] }),
138 })
139​
140 render(() => <RouterProvider router={router} />)
141 await screen.findByText('Index route')
142 await router.navigate({ to: '/not-found' })
143​
144 expect(await screen.findByText('Not found route')).toBeInTheDocument()
145 expect(warn).toHaveBeenCalledWith(outletWarning('notFoundComponent'))
146})