Open History to see the last commit
1import { expectTypeOf, test } from 'vitest'
2import {
3 createMemoryHistory,
4 createRootRoute,
5 createRootRouteWithContext,
6 createRoute,
7 createRouter,
8} from '../src'
9import type { RouterHistory } from '../src'
10β
11test('when creating a router without context', () => {
12 const rootRoute = createRootRoute()
13β
14 type RouteTree = typeof rootRoute
15β
16 expectTypeOf(createRouter<RouteTree, 'never'>)
17 .parameter(0)
18 .toHaveProperty('routeTree')
19 .toEqualTypeOf<RouteTree | undefined>()
20β
21 expectTypeOf(createRouter<RouteTree, 'never'>)
22 .parameter(0)
23 .toHaveProperty('context')
24 .toEqualTypeOf<{} | undefined>()
25β
26 expectTypeOf(createRouter<RouteTree, 'never'>)
27 .parameter(0)
28 .not.toMatchTypeOf<{
29 context: {}
30 }>()
31})
32β
33test('when navigating using router', () => {
34 const rootRoute = createRootRoute()
35β
36 const indexRoute = createRoute({
37 getParentRoute: () => rootRoute,
38 path: '/',
39 })
40β
41 const postsRoute = createRoute({
42 getParentRoute: () => rootRoute,
43 path: 'posts',
44 validateSearch: () => ({
45 page: 0,
46 }),
47 })
48β
49 const routeTree = rootRoute.addChildren([indexRoute, postsRoute])
50β
51 const router = createRouter({
52 routeTree,
53 })
54β
55 expectTypeOf(router.navigate<typeof router, '/posts'>)
56 .parameter(0)
57 .toHaveProperty('to')
58 .toEqualTypeOf<'/posts' | '/' | '.' | '..'>()
59β
60 expectTypeOf(router.navigate<typeof router, '/posts'>)
61 .parameter(0)
62 .toHaveProperty('search')
63 .exclude<(...args: Array<any>) => any>()
64 .toEqualTypeOf<{ page: number }>()
65})
66β
67test('when building location using router', () => {
68 const rootRoute = createRootRoute()
69β
70 const indexRoute = createRoute({
71 getParentRoute: () => rootRoute,
72 path: '/',
73 })
74β
75 const postsRoute = createRoute({
76 getParentRoute: () => rootRoute,
77 path: 'posts',
78 validateSearch: () => ({
79 page: 0,
80 }),
81 })
82β
83 const routeTree = rootRoute.addChildren([indexRoute, postsRoute])
84β
85 const router = createRouter({
86 routeTree,
87 })
88β
89 expectTypeOf(router.buildLocation<typeof router, '/posts'>)
90 .parameter(0)
91 .toHaveProperty('to')
92 .toEqualTypeOf<'/posts' | '/' | '.' | '..'>()
93β
94 expectTypeOf(router.buildLocation<typeof router, '/posts'>)
95 .parameter(0)
96 .toHaveProperty('search')
97 .exclude<(...args: Array<any>) => any>()
98 .toEqualTypeOf<{ page: number }>()
99})
100β
101test('when creating a router with context', () => {
102 const rootRoute = createRootRouteWithContext<{ userId: string }>()()
103β
104 type RouteTree = typeof rootRoute
105β
106 expectTypeOf(createRouter<RouteTree, 'never'>)
107 .parameter(0)
108 .toHaveProperty('routeTree')
109 .toEqualTypeOf<RouteTree | undefined>()
110β
111 expectTypeOf(createRouter<RouteTree, 'never'>)
112 .parameter(0)
113 .toHaveProperty('context')
114 .toEqualTypeOf<{ userId: string }>()
115β
116 expectTypeOf(createRouter<RouteTree, 'never'>)
117 .parameter(0)
118 .toMatchTypeOf<{
119 context: { userId: string }
120 }>()
121})
122β
123test('when creating a router with context and children', () => {
124 const rootRoute = createRootRouteWithContext<{ userId: string }>()()
125β
126 const indexRoute = createRoute({
127 getParentRoute: () => rootRoute,
128 path: '/',
129 })
130β
131 const routeTree = rootRoute.addChildren([indexRoute])
132β
133 type RouteTree = typeof routeTree
134β
135 expectTypeOf(createRouter<RouteTree, 'never'>)
136 .parameter(0)
137 .toHaveProperty('routeTree')
138 .toEqualTypeOf<RouteTree | undefined>()
139β
140 expectTypeOf(createRouter<RouteTree, 'never'>)
141 .parameter(0)
142 .toHaveProperty('context')
143 .toEqualTypeOf<{ userId: string }>()
144β
145 expectTypeOf(createRouter<RouteTree, 'never'>)
146 .parameter(0)
147 .toMatchTypeOf<{
148 context: { userId: string }
149 }>()
150})
151β
152test('invalidate and clearCache narrowing in filter', () => {
153 const rootRoute = createRootRoute()
154β
155 const invoicesRoute = createRoute({
156 path: 'invoices',
157 getParentRoute: () => rootRoute,
158 validateSearch: () => ({ page: 0 }),
159 beforeLoad: () => ({ invoicePermissions: ['view'] as const }),
160 })
161β
162 const invoiceRoute = createRoute({
163 path: '$invoiceId',
164 getParentRoute: () => invoicesRoute,
165 })
166β
167 const detailsRoute = createRoute({
168 path: 'details',
169 getParentRoute: () => invoiceRoute,
170 validateSearch: () => ({ detailPage: 0 }),
171 beforeLoad: () => ({ detailsPermissions: ['view'] as const }),
172 })
173β
174 const detailRoute = createRoute({
175 path: '$detailId',
176 getParentRoute: () => detailsRoute,
177 })
178β
179 const routeTree = rootRoute.addChildren([
180 invoicesRoute.addChildren([
181 invoiceRoute.addChildren([detailsRoute.addChildren([detailRoute])]),
182 ]),
183 ])
184β
185 const router = createRouter({
186 routeTree,
187 context: { userId: 'userId' },
188 })
189β
190 type Router = typeof router
191β
192 router.invalidate({
193 filter: (route) => {
194 expectTypeOf(route.routeId).toEqualTypeOf<
195 | '__root__'
196 | '/invoices'
197 | '/invoices/$invoiceId'
198 | '/invoices/$invoiceId/details'
199 | '/invoices/$invoiceId/details/$detailId'
200 >()
201β
202 if (route.routeId === '/invoices/$invoiceId/details/$detailId') {
203 expectTypeOf(route.params).branded.toEqualTypeOf<{
204 invoiceId: string
205 detailId: string
206 }>()
207 }
208 return true
209 },
210 })
211β
212 router.clearCache({
213 filter: (route) => {
214 expectTypeOf(route.routeId).toEqualTypeOf<
215 | '__root__'
216 | '/invoices'
217 | '/invoices/$invoiceId'
218 | '/invoices/$invoiceId/details'
219 | '/invoices/$invoiceId/details/$detailId'
220 >()
221β
222 if (route.routeId === '/invoices/$invoiceId/details/$detailId') {
223 expectTypeOf(route.params).branded.toEqualTypeOf<{
224 invoiceId: string
225 detailId: string
226 }>()
227 }
228 return true
229 },
230 })
231})
232β
233test('when creating a router with default router history', () => {
234 const router = createRouter({ routeTree: createRootRoute() })
235β
236 expectTypeOf(router.history).toEqualTypeOf<RouterHistory>()
237})
238β
239test('when creating a router with custom router history', () => {
240 const customRouterHistory = {
241 ...createMemoryHistory(),
242 _isCustomRouterHistory: true,
243 }
244β
245 const router = createRouter({
246 routeTree: createRootRoute(),
247 history: customRouterHistory,
248 })
249β
250 expectTypeOf(router.history).toMatchTypeOf<RouterHistory>()
251 expectTypeOf(router.history).toEqualTypeOf<typeof customRouterHistory>()
252})