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

not-found.test.tsx

486 lines13.5 KB

1import { afterEach, beforeEach, expect, test } from 'vitest'
2import { cleanup, render, screen } from '@solidjs/testing-library'
3​
4import {
5 Link,
6 Outlet,
7 RouterProvider,
8 createBrowserHistory,
9 createRootRoute,
10 createRoute,
11 createRouter,
12 notFound,
13 rootRouteId,
14} from '../src'
15import type { NotFoundRouteProps, RouterHistory } from '../src'
16​
17let history: RouterHistory
18​
19beforeEach(() => {
20 history = createBrowserHistory()
21 expect(window.location.pathname).toBe('/')
22})
23​
24afterEach(() => {
25 history.destroy()
26 window.history.replaceState(null, 'root', '/')
27 cleanup()
28})
29​
30test.each([
31 {
32 notFoundMode: 'fuzzy' as const,
33 expectedNotFoundComponent: 'settings-not-found',
34 },
35 {
36 notFoundMode: 'root' as const,
37 expectedNotFoundComponent: 'root-not-found',
38 },
39])(
40 'correct notFoundComponent is rendered for mode=%s',
41 async ({ notFoundMode, expectedNotFoundComponent }) => {
42 const rootRoute = createRootRoute({
43 component: () => (
44 <div data-testid="root-component">
45 <h1>Root Component</h1>
46 <div>
47 <Link data-testid="settings-link" to="/settings/">
48 link to settings
49 </Link>{' '}
50 <Link data-testid="non-existing-link" to="/settings/does-not-exist">
51 link to non-existing route
52 </Link>
53 </div>
54 <Outlet />
55 </div>
56 ),
57 notFoundComponent: () => (
58 <span data-testid="root-not-found">Root Not Found Component</span>
59 ),
60 })
61​
62 const indexRoute = createRoute({
63 getParentRoute: () => rootRoute,
64 path: '/',
65 component: () => (
66 <div data-testid="index-component">
67 <h2>Index Page</h2>
68 </div>
69 ),
70 })
71​
72 const settingsRoute = createRoute({
73 getParentRoute: () => rootRoute,
74 path: '/settings',
75 notFoundComponent: () => (
76 <span data-testid="settings-not-found">
77 Settings Not Found Component
78 </span>
79 ),
80 component: () => (
81 <div>
82 <p>Settings Page Layout</p>
83 <Outlet />
84 </div>
85 ),
86 })
87​
88 const settingsIndexRoute = createRoute({
89 getParentRoute: () => settingsRoute,
90 path: '/',
91 component: () => (
92 <div data-testid="settings-index-component">Settings Page</div>
93 ),
94 })
95​
96 const router = createRouter({
97 routeTree: rootRoute.addChildren([
98 indexRoute,
99 settingsRoute.addChildren([settingsIndexRoute]),
100 ]),
101 history,
102 notFoundMode,
103 })
104​
105 render(() => <RouterProvider router={router} />)
106 await router.load()
107 await screen.findByTestId('root-component')
108​
109 const settingsLink = screen.getByTestId('settings-link')
110 settingsLink.click()
111​
112 const settingsIndexComponent = await screen.findByTestId(
113 'settings-index-component',
114 )
115 expect(settingsIndexComponent).toBeInTheDocument()
116​
117 const nonExistingLink = screen.getByTestId('non-existing-link')
118 nonExistingLink.click()
119​
120 const notFoundComponent = await screen.findByTestId(
121 expectedNotFoundComponent,
122 {},
123 { timeout: 1000 },
124 )
125 expect(notFoundComponent).toBeInTheDocument()
126 },
128​
129test('defaultNotFoundComponent and notFoundComponent receives data props via spread operator', async () => {
130 const isCustomData = (data: unknown): data is typeof customData => {
131 return 'message' in (data as typeof customData)
132 }
133​
134 const customData = {
135 message: 'Custom not found message',
136 }
137​
138 const DefaultNotFoundComponentWithProps = (props: NotFoundRouteProps) => (
139 <div data-testid="default-not-found-with-props">
140 <span data-testid="message">
141 {isCustomData(props.data) && <span>{props.data.message}</span>}
142 </span>
143 </div>
144 )
145​
146 const rootRoute = createRootRoute({
147 component: () => (
148 <div data-testid="root-component">
149 <h1>Root Component</h1>
150 <div>
151 <Link
152 data-testid="default-not-found-route-link"
153 to="/default-not-found-route"
154 >
155 link to default not found route
156 </Link>
157 <Link data-testid="not-found-route-link" to="/not-found-route">
158 link to not found route
159 </Link>
160 </div>
161 <Outlet />
162 </div>
163 ),
164 })
165​
166 const indexRoute = createRoute({
167 getParentRoute: () => rootRoute,
168 path: '/',
169 component: () => (
170 <div data-testid="index-component">
171 <h2>Index Page</h2>
172 </div>
173 ),
174 })
175​
176 const defaultNotFoundRoute = createRoute({
177 getParentRoute: () => rootRoute,
178 path: '/default-not-found-route',
179 loader: () => {
180 throw notFound({ data: customData })
181 },
182 component: () => (
183 <div data-testid="default-not-found-route-component">
184 Should not render
185 </div>
186 ),
187 })
188​
189 const notFoundRoute = createRoute({
190 getParentRoute: () => rootRoute,
191 path: '/not-found-route',
192 loader: () => {
193 throw notFound({ data: customData })
194 },
195 component: () => (
196 <div data-testid="not-found-route-component">Should not render</div>
197 ),
198 notFoundComponent: (props) => (
199 <div data-testid="not-found-with-props">
200 <span data-testid="message">
201 {isCustomData(props.data) && <span>{props.data.message}</span>}
202 </span>
203 </div>
204 ),
205 })
206​
207 const router = createRouter({
208 routeTree: rootRoute.addChildren([
209 indexRoute,
210 defaultNotFoundRoute,
211 notFoundRoute,
212 ]),
213 history,
214 defaultNotFoundComponent: DefaultNotFoundComponentWithProps,
215 })
216​
217 render(() => <RouterProvider router={router} />)
218 await router.load()
219 await screen.findByTestId('root-component')
220​
221 const defaultNotFoundRouteLink = screen.getByTestId(
222 'default-not-found-route-link',
223 )
224 defaultNotFoundRouteLink.click()
225​
226 const defaultNotFoundComponent = await screen.findByTestId(
227 'default-not-found-with-props',
228 {},
229 { timeout: 1000 },
230 )
231 expect(defaultNotFoundComponent).toBeInTheDocument()
232​
233 const defaultNotFoundComponentMessage = await screen.findByTestId('message')
234 expect(defaultNotFoundComponentMessage).toHaveTextContent(customData.message)
235​
236 const notFoundRouteLink = screen.getByTestId('not-found-route-link')
237 notFoundRouteLink.click()
238​
239 const notFoundComponent = await screen.findByTestId(
240 'not-found-with-props',
241 {},
242 { timeout: 1000 },
243 )
244 expect(notFoundComponent).toBeInTheDocument()
245​
246 const errorMessageComponent = await screen.findByTestId('message')
247 expect(errorMessageComponent).toHaveTextContent(customData.message)
248})
249​
250test('component-thrown bare notFound renders current route notFoundComponent', async () => {
251 const rootRoute = createRootRoute({
252 component: () => <Outlet />,
253 notFoundComponent: () => <span data-testid="root-not-found">Root</span>,
254 })
255​
256 const parentRoute = createRoute({
257 getParentRoute: () => rootRoute,
258 path: '/parent',
259 component: () => <Outlet />,
260 notFoundComponent: () => (
261 <span data-testid="parent-not-found">Parent not found</span>
262 ),
263 })
264​
265 const childRoute = createRoute({
266 getParentRoute: () => parentRoute,
267 path: '/child',
268 notFoundComponent: () => (
269 <span data-testid="child-not-found">Child not found</span>
270 ),
271 component: () => {
272 throw notFound()
273 },
274 })
275​
276 const router = createRouter({
277 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
278 history,
279 notFoundMode: 'fuzzy',
280 })
281​
282 render(() => <RouterProvider router={router} />)
283 await router.navigate({ to: '/parent/child' })
284​
285 expect(await screen.findByTestId('child-not-found')).toBeInTheDocument()
286 expect(screen.queryByTestId('parent-not-found')).not.toBeInTheDocument()
287 expect(screen.queryByTestId('root-not-found')).not.toBeInTheDocument()
288})
289​
290test('component-thrown bare notFound falls back to nearest ancestor notFoundComponent', async () => {
291 const rootRoute = createRootRoute({
292 component: () => <Outlet />,
293 notFoundComponent: () => <span data-testid="root-not-found">Root</span>,
294 })
295​
296 const parentRoute = createRoute({
297 getParentRoute: () => rootRoute,
298 path: '/parent',
299 component: () => <Outlet />,
300 notFoundComponent: () => (
301 <span data-testid="parent-not-found">Parent not found</span>
302 ),
303 })
304​
305 const childRoute = createRoute({
306 getParentRoute: () => parentRoute,
307 path: '/child',
308 component: () => {
309 throw notFound()
310 },
311 })
312​
313 const router = createRouter({
314 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
315 history,
316 notFoundMode: 'fuzzy',
317 })
318​
319 render(() => <RouterProvider router={router} />)
320 await router.navigate({ to: '/parent/child' })
321​
322 expect(await screen.findByTestId('parent-not-found')).toBeInTheDocument()
323 expect(screen.queryByTestId('root-not-found')).not.toBeInTheDocument()
324})
325​
326test('beforeLoad notFound with routeId targets parent notFoundComponent', async () => {
327 const rootRoute = createRootRoute({
328 component: () => <Outlet />,
329 notFoundComponent: () => (
330 <span data-testid="root-not-found">Root not found</span>
331 ),
332 })
333​
334 const parentRoute = createRoute({
335 getParentRoute: () => rootRoute,
336 path: '/parent',
337 component: () => <Outlet />,
338 notFoundComponent: () => (
339 <span data-testid="parent-not-found">Parent not found</span>
340 ),
341 })
342​
343 const childRoute = createRoute({
344 getParentRoute: () => parentRoute,
345 path: '/child',
346 beforeLoad: () => {
347 throw notFound({ routeId: parentRoute.id })
348 },
349 component: () => <span data-testid="child-component">Child</span>,
350 })
351​
352 const router = createRouter({
353 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
354 history,
355 })
356​
357 render(() => <RouterProvider router={router} />)
358 await router.navigate({ to: '/parent/child' })
359​
360 expect(await screen.findByTestId('parent-not-found')).toBeInTheDocument()
361 expect(screen.queryByTestId('child-component')).not.toBeInTheDocument()
362})
363​
364test('beforeLoad notFound with routeId targets parent boundary and preserves parent loader data', async () => {
365 const rootRoute = createRootRoute({
366 component: () => <Outlet />,
367 notFoundComponent: () => (
368 <span data-testid="root-not-found">Root not found</span>
369 ),
370 })
371​
372 const parentRoute = createRoute({
373 getParentRoute: () => rootRoute,
374 path: '/parent',
375 loader: () => ({ message: 'ready' }),
376 component: () => <Outlet />,
377 notFoundComponent: () => {
378 const loaderData = parentRoute.useLoaderData()
379 return (
380 <span data-testid="parent-not-found-with-loader-data">
381 {loaderData().message}
382 </span>
383 )
384 },
385 })
386​
387 const childRoute = createRoute({
388 getParentRoute: () => parentRoute,
389 path: '/child',
390 beforeLoad: () => {
391 throw notFound({ routeId: parentRoute.id })
392 },
393 component: () => <span data-testid="child-component">Child</span>,
394 })
395​
396 const router = createRouter({
397 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
398 history,
399 })
400​
401 render(() => <RouterProvider router={router} />)
402 await router.navigate({ to: '/parent/child' })
403​
404 expect(
405 await screen.findByTestId('parent-not-found-with-loader-data'),
406 ).toHaveTextContent('ready')
407 expect(screen.queryByTestId('child-component')).not.toBeInTheDocument()
408})
409​
410test('beforeLoad notFound with routeId targets root notFoundComponent', async () => {
411 const rootRoute = createRootRoute({
412 component: () => <Outlet />,
413 notFoundComponent: () => (
414 <span data-testid="root-not-found">Root not found</span>
415 ),
416 })
417​
418 const parentRoute = createRoute({
419 getParentRoute: () => rootRoute,
420 path: '/parent',
421 component: () => <Outlet />,
422 notFoundComponent: () => (
423 <span data-testid="parent-not-found">Parent not found</span>
424 ),
425 })
426​
427 const childRoute = createRoute({
428 getParentRoute: () => parentRoute,
429 path: '/child',
430 beforeLoad: () => {
431 throw notFound({ routeId: rootRouteId })
432 },
433 component: () => <span data-testid="child-component">Child</span>,
434 })
435​
436 const router = createRouter({
437 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
438 history,
439 })
440​
441 render(() => <RouterProvider router={router} />)
442 await router.navigate({ to: '/parent/child' })
443​
444 expect(await screen.findByTestId('root-not-found')).toBeInTheDocument()
445 expect(screen.queryByTestId('child-component')).not.toBeInTheDocument()
446})
447​
448test('beforeLoad notFound with non-exact routeId falls back to root notFoundComponent', async () => {
449 const rootRoute = createRootRoute({
450 component: () => <Outlet />,
451 notFoundComponent: () => (
452 <span data-testid="root-not-found">Root not found</span>
453 ),
454 })
455​
456 const parentRoute = createRoute({
457 getParentRoute: () => rootRoute,
458 path: '/parent',
459 component: () => <Outlet />,
460 notFoundComponent: () => (
461 <span data-testid="parent-not-found">Parent not found</span>
462 ),
463 })
464​
465 const childRoute = createRoute({
466 getParentRoute: () => parentRoute,
467 path: '/child',
468 beforeLoad: () => {
469 throw notFound({ routeId: `${parentRoute.id}/` as never })
470 },
471 component: () => <span data-testid="child-component">Child</span>,
472 })
473​
474 const router = createRouter({
475 routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]),
476 history,
477 notFoundMode: 'fuzzy',
478 })
479​
480 render(() => <RouterProvider router={router} />)
481 await router.navigate({ to: '/parent/child' })
482​
483 expect(await screen.findByTestId('root-not-found')).toBeInTheDocument()
484 expect(screen.queryByTestId('parent-not-found')).not.toBeInTheDocument()
485 expect(screen.queryByTestId('child-component')).not.toBeInTheDocument()
486})