Open History to see the last commit
1import { describe, expectTypeOf, test } from 'vitest'
2import { createRootRoute, createRoute, createRouter, useParams } from '../src'
3β
4describe('useParams', () => {
5 test('when there are no params', () => {
6 const rootRoute = createRootRoute()
7β
8 const indexRoute = createRoute({
9 getParentRoute: () => rootRoute,
10 path: '/',
11 })
12β
13 const invoicesRoute = createRoute({
14 getParentRoute: () => rootRoute,
15 path: 'invoices',
16 })
17β
18 const invoicesIndexRoute = createRoute({
19 getParentRoute: () => invoicesRoute,
20 path: '/',
21 })
22β
23 const routeTree = rootRoute.addChildren([
24 invoicesRoute.addChildren([invoicesIndexRoute]),
25 indexRoute,
26 ])
27β
28 // eslint-disable-next-line unused-imports/no-unused-vars
29 const defaultRouter = createRouter({
30 routeTree,
31 })
32β
33 type DefaultRouter = typeof defaultRouter
34β
35 expectTypeOf(useParams<DefaultRouter>)
36 .parameter(0)
37 .toHaveProperty('from')
38 .toEqualTypeOf<'/invoices' | '__root__' | '/invoices/' | '/'>()
39β
40 expectTypeOf(useParams<DefaultRouter>)
41 .parameter(0)
42 .toHaveProperty('strict')
43 .toEqualTypeOf<true | undefined>()
44β
45 expectTypeOf(useParams<DefaultRouter, '/'>)
46 .parameter(0)
47 .toHaveProperty('select')
48 .parameter(0)
49 .toEqualTypeOf<{}>()
50β
51 expectTypeOf(useParams<DefaultRouter, '/'>)
52 .parameter(0)
53 .toHaveProperty('select')
54 .returns.toEqualTypeOf<unknown>()
55β
56 expectTypeOf(useParams<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
57β
58 expectTypeOf(
59 useParams<DefaultRouter, '/', false>({
60 strict: false,
61 }),
62 ).toEqualTypeOf<{}>()
63 })
64β
65 test('when there is one param', () => {
66 const rootRoute = createRootRoute()
67β
68 const indexRoute = createRoute({
69 getParentRoute: () => rootRoute,
70 path: '/',
71 })
72β
73 const invoicesRoute = createRoute({
74 getParentRoute: () => rootRoute,
75 path: 'invoices',
76 })
77β
78 const invoicesIndexRoute = createRoute({
79 getParentRoute: () => invoicesRoute,
80 path: '/',
81 })
82β
83 const invoiceRoute = createRoute({
84 getParentRoute: () => invoicesRoute,
85 path: '$invoiceId',
86 })
87β
88 const routeTree = rootRoute.addChildren([
89 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
90 indexRoute,
91 ])
92β
93 // eslint-disable-next-line unused-imports/no-unused-vars
94 const defaultRouter = createRouter({
95 routeTree,
96 })
97β
98 type DefaultRouter = typeof defaultRouter
99β
100 expectTypeOf(useParams<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
101β
102 expectTypeOf(
103 useParams<DefaultRouter, '/invoices/$invoiceId'>,
104 ).returns.toEqualTypeOf<{ invoiceId: string }>()
105β
106 expectTypeOf(useParams<DefaultRouter, '/invoices/$invoiceId'>)
107 .parameter(0)
108 .toHaveProperty('select')
109 .toEqualTypeOf<((search: { invoiceId: string }) => unknown) | undefined>()
110β
111 expectTypeOf(
112 useParams<DefaultRouter, '/invoices', false>,
113 ).returns.toEqualTypeOf<{ invoiceId?: string }>()
114β
115 expectTypeOf(useParams<DefaultRouter, '/invoices', false>)
116 .parameter(0)
117 .toHaveProperty('select')
118 .toEqualTypeOf<
119 ((search: { invoiceId?: string }) => unknown) | undefined
120 >()
121β
122 expectTypeOf(
123 useParams<
124 DefaultRouter,
125 '/invoices',
126 /* strict */ false,
127 /* shouldThrow */ true,
128 number
129 >,
130 ).returns.toEqualTypeOf<number>()
131 })
132β
133 test('when there are multiple params', () => {
134 const rootRoute = createRootRoute()
135β
136 const indexRoute = createRoute({
137 getParentRoute: () => rootRoute,
138 path: '/',
139 })
140β
141 const invoicesRoute = createRoute({
142 getParentRoute: () => rootRoute,
143 path: 'invoices',
144 })
145β
146 const invoicesIndexRoute = createRoute({
147 getParentRoute: () => invoicesRoute,
148 path: '/',
149 })
150β
151 const invoiceRoute = createRoute({
152 getParentRoute: () => invoicesRoute,
153 path: '$invoiceId',
154 })
155β
156 const postsRoute = createRoute({
157 getParentRoute: () => rootRoute,
158 path: 'posts',
159 })
160β
161 const postRoute = createRoute({
162 getParentRoute: () => postsRoute,
163 path: '$postId',
164 })
165β
166 const routeTree = rootRoute.addChildren([
167 invoicesRoute.addChildren([
168 invoicesIndexRoute,
169 invoiceRoute,
170 postsRoute.addChildren([postRoute]),
171 ]),
172 indexRoute,
173 ])
174β
175 // eslint-disable-next-line unused-imports/no-unused-vars
176 const defaultRouter = createRouter({
177 routeTree,
178 })
179β
180 type DefaultRouter = typeof defaultRouter
181β
182 expectTypeOf(useParams<DefaultRouter, '/'>).returns.toEqualTypeOf<{}>()
183β
184 expectTypeOf(
185 useParams<DefaultRouter, '/invoices/$invoiceId'>,
186 ).returns.toEqualTypeOf<{ invoiceId: string }>()
187β
188 expectTypeOf(useParams<DefaultRouter, '/invoices/$invoiceId'>)
189 .parameter(0)
190 .toHaveProperty('select')
191 .toEqualTypeOf<((search: { invoiceId: string }) => unknown) | undefined>()
192β
193 expectTypeOf(
194 useParams<DefaultRouter, '/invoices', false>,
195 ).returns.toEqualTypeOf<{ invoiceId?: string; postId?: string }>()
196β
197 expectTypeOf(useParams<DefaultRouter, '/invoices', false>)
198 .parameter(0)
199 .toHaveProperty('select')
200 .toEqualTypeOf<
201 | ((search: { invoiceId?: string; postId?: string }) => unknown)
202 | undefined
203 >()
204β
205 expectTypeOf(
206 useParams<
207 DefaultRouter,
208 '/invoices',
209 /* strict */ true,
210 /* shouldThrow */ true,
211 { func: () => void }
212 >,
213 )
214 .parameter(0)
215 .exclude<undefined>()
216 .toHaveProperty('select')
217 .toEqualTypeOf<
218 | ((search: {}) => {
219 func: () => void
220 })
221 | undefined
222 >()
223β
224 expectTypeOf(
225 useParams<
226 DefaultRouter,
227 '/invoices',
228 /* strict */ true,
229 /* shouldThrow */ true,
230 { func: () => void }
231 >,
232 )
233 .parameter(0)
234 .exclude<undefined>()
235 .toHaveProperty('structuralSharing')
236 .toEqualTypeOf<false | undefined>()
237β
238 expectTypeOf(
239 useParams<
240 DefaultRouter,
241 '/invoices',
242 /* strict */ true,
243 /* shouldThrow */ true,
244 { func: () => void },
245 true
246 >,
247 )
248 .parameter(0)
249 .exclude<undefined>()
250 .toHaveProperty('select')
251 .toEqualTypeOf<
252 | ((search: {}) => {
253 func: 'Function is not serializable'
254 })
255 | undefined
256 >()
257β
258 expectTypeOf(
259 useParams<
260 DefaultRouter,
261 '/invoices',
262 /* strict */ true,
263 /* shouldThrow */ true,
264 { func: () => void },
265 true
266 >,
267 )
268 .parameter(0)
269 .exclude<undefined>()
270 .toHaveProperty('structuralSharing')
271 .toEqualTypeOf<false | undefined>()
272β
273 // eslint-disable-next-line unused-imports/no-unused-vars
274 const routerWithStructuralSharing = createRouter({
275 routeTree,
276 defaultStructuralSharing: true,
277 })
278β
279 expectTypeOf(
280 useParams<
281 typeof routerWithStructuralSharing,
282 '/invoices',
283 /* strict */ true,
284 /* shouldThrow */ true,
285 { func: () => void },
286 true
287 >,
288 )
289 .parameter(0)
290 .exclude<undefined>()
291 .toHaveProperty('select')
292 .toEqualTypeOf<
293 | ((search: {}) => {
294 func: 'Function is not serializable'
295 })
296 | undefined
297 >()
298β
299 expectTypeOf(
300 useParams<
301 typeof routerWithStructuralSharing,
302 '/invoices',
303 /* strict */ true,
304 /* shouldThrow */ true,
305 { func: () => void },
306 true
307 >,
308 )
309 .parameter(0)
310 .exclude<undefined>()
311 .toHaveProperty('structuralSharing')
312 .toEqualTypeOf<false>()
313 })
314β
315 describe('shouldThrow', () => {
316 const rootRoute = createRootRoute()
317β
318 const indexRoute = createRoute({
319 getParentRoute: () => rootRoute,
320 path: '/',
321 })
322β
323 const invoicesRoute = createRoute({
324 getParentRoute: () => rootRoute,
325 path: 'invoices',
326 validateSearch: () => ({ page: 0 }),
327 })
328 const invoiceRoute = createRoute({
329 getParentRoute: () => invoicesRoute,
330 path: '$id',
331 })
332β
333 const routeTree = rootRoute.addChildren([
334 invoicesRoute.addChildren([invoiceRoute]),
335 indexRoute,
336 ])
337β
338 // eslint-disable-next-line unused-imports/no-unused-vars
339 const router = createRouter({ routeTree })
340β
341 const from = '/invoices/$id'
342 test('return type is `{ id: string }` when shouldThrow = true', () => {
343 const shouldThrow = true
344 const params = useParams<
345 typeof router,
346 typeof from,
347 /* strict */ true,
348 typeof shouldThrow
349 >({ from, shouldThrow })
350β
351 expectTypeOf(params).toEqualTypeOf<{ id: string }>()
352 })
353β
354 test('return type is `{page: string} | undefined` when shouldThrow = false', () => {
355 const shouldThrow = false
356 const params = useParams<
357 typeof router,
358 typeof from,
359 /* strict */ true,
360 typeof shouldThrow
361 >({ from, shouldThrow })
362β
363 expectTypeOf(params).toEqualTypeOf<{ id: string } | undefined>()
364 })
365 })
366})