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

not-found.test.tsx

125 lines3.1 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} from '../src'
13import type { RouterHistory } from '../src'
14​
15let history: RouterHistory
16​
17beforeEach(() => {
18 history = createBrowserHistory()
19 expect(window.location.pathname).toBe('/')
20})
21​
22afterEach(() => {
23 history.destroy()
24 window.history.replaceState(null, 'root', '/')
25 cleanup()
26})
27​
28test.each([
29 {
30 notFoundMode: 'fuzzy' as const,
31 expectedNotFoundComponent: 'settings-not-found',
32 },
33 {
34 notFoundMode: 'root' as const,
35 expectedNotFoundComponent: 'root-not-found',
36 },
37])(
38 'correct notFoundComponent is rendered for mode=%s',
39 async ({ notFoundMode, expectedNotFoundComponent }) => {
40 const rootRoute = createRootRoute({
41 component: () => (
42 <div data-testid="root-component">
43 <h1>Root Component</h1>
44 <div>
45 <Link data-testid="settings-link" to="/settings/">
46 link to settings
47 </Link>{' '}
48 <Link data-testid="non-existing-link" to="/settings/does-not-exist">
49 link to non-existing route
50 </Link>
51 </div>
52 <Outlet />
53 </div>
54 ),
55 notFoundComponent: () => (
56 <span data-testid="root-not-found">Root Not Found Component</span>
57 ),
58 })
59​
60 const indexRoute = createRoute({
61 getParentRoute: () => rootRoute,
62 path: '/',
63 component: () => (
64 <div data-testid="index-component">
65 <h2>Index Page</h2>
66 </div>
67 ),
68 })
69​
70 const settingsRoute = createRoute({
71 getParentRoute: () => rootRoute,
72 path: '/settings',
73 notFoundComponent: () => (
74 <span data-testid="settings-not-found">
75 Settings Not Found Component
76 </span>
77 ),
78 component: () => (
79 <div>
80 <p>Settings Page Layout</p>
81 <Outlet />
82 </div>
83 ),
84 })
85​
86 const settingsIndexRoute = createRoute({
87 getParentRoute: () => settingsRoute,
88 path: '/',
89 component: () => (
90 <div data-testid="settings-index-component">Settings Page</div>
91 ),
92 })
93​
94 const router = createRouter({
95 routeTree: rootRoute.addChildren([
96 indexRoute,
97 settingsRoute.addChildren([settingsIndexRoute]),
98 ]),
99 history,
100 notFoundMode,
101 })
102​
103 render(() => <RouterProvider router={router} />)
104 await router.load()
105 await screen.findByTestId('root-component')
106​
107 const settingsLink = screen.getByTestId('settings-link')
108 settingsLink.click()
109​
110 const settingsIndexComponent = await screen.findByTestId(
111 'settings-index-component',
112 )
113 expect(settingsIndexComponent).toBeInTheDocument()
114​
115 const nonExistingLink = screen.getByTestId('non-existing-link')
116 nonExistingLink.click()
117​
118 const notFoundComponent = await screen.findByTestId(
119 expectedNotFoundComponent,
120 {},
121 { timeout: 1000 },
122 )
123 expect(notFoundComponent).toBeInTheDocument()
124 },