Open History to see the last commit
1import { expectTypeOf, test } from 'vitest'
2import {
3 MatchRoute,
4 createRootRoute,
5 createRoute,
6 createRouter,
7 isMatch,
8 useMatchRoute,
9 useMatches,
10} from '../src'
11import type { AnyRouteMatch, RouteMatch } from '@tanstack/router-core'
12import type * as Solid from 'solid-js'
13import type { MakeMatchRouteOptions } from '../src'
14β
15const rootRoute = createRootRoute()
16β
17type RootRoute = typeof rootRoute
18β
19type RootMatch = RouteMatch<
20 RootRoute['id'],
21 RootRoute['fullPath'],
22 RootRoute['types']['allParams'],
23 RootRoute['types']['fullSearchSchema'],
24 RootRoute['types']['loaderData'],
25 RootRoute['types']['allContext'],
26 RootRoute['types']['loaderDeps']
27>
28β
29const indexRoute = createRoute({
30 getParentRoute: () => rootRoute,
31 path: '/',
32})
33β
34type IndexRoute = typeof indexRoute
35β
36type IndexMatch = RouteMatch<
37 IndexRoute['id'],
38 IndexRoute['fullPath'],
39 IndexRoute['types']['allParams'],
40 IndexRoute['types']['fullSearchSchema'],
41 IndexRoute['types']['loaderData'],
42 IndexRoute['types']['allContext'],
43 IndexRoute['types']['loaderDeps']
44>
45β
46const invoicesRoute = createRoute({
47 getParentRoute: () => rootRoute,
48 path: 'invoices',
49 loader: () => [{ id: '1' }, { id: '2' }],
50})
51β
52type InvoiceMatch = RouteMatch<
53 InvoiceRoute['id'],
54 InvoiceRoute['fullPath'],
55 InvoiceRoute['types']['allParams'],
56 InvoiceRoute['types']['fullSearchSchema'],
57 InvoiceRoute['types']['loaderData'],
58 InvoiceRoute['types']['allContext'],
59 InvoiceRoute['types']['loaderDeps']
60>
61β
62type InvoicesRoute = typeof invoicesRoute
63β
64type InvoicesMatch = RouteMatch<
65 InvoicesRoute['id'],
66 InvoicesRoute['fullPath'],
67 InvoicesRoute['types']['allParams'],
68 InvoicesRoute['types']['fullSearchSchema'],
69 InvoicesRoute['types']['loaderData'],
70 InvoicesRoute['types']['allContext'],
71 InvoicesRoute['types']['loaderDeps']
72>
73β
74const invoicesIndexRoute = createRoute({
75 getParentRoute: () => invoicesRoute,
76 path: '/',
77})
78β
79type InvoicesIndexRoute = typeof invoicesIndexRoute
80β
81type InvoicesIndexMatch = RouteMatch<
82 InvoicesIndexRoute['id'],
83 InvoicesIndexRoute['fullPath'],
84 InvoicesIndexRoute['types']['allParams'],
85 InvoicesIndexRoute['types']['fullSearchSchema'],
86 InvoicesIndexRoute['types']['loaderData'],
87 InvoicesIndexRoute['types']['allContext'],
88 InvoicesIndexRoute['types']['loaderDeps']
89>
90β
91const invoiceRoute = createRoute({
92 getParentRoute: () => invoicesRoute,
93 path: '$invoiceId',
94 validateSearch: () => ({ page: 0 }),
95})
96β
97type InvoiceRoute = typeof invoiceRoute
98β
99const layoutRoute = createRoute({
100 getParentRoute: () => rootRoute,
101 id: '_layout',
102})
103β
104type LayoutRoute = typeof layoutRoute
105β
106type LayoutMatch = RouteMatch<
107 LayoutRoute['id'],
108 LayoutRoute['fullPath'],
109 LayoutRoute['types']['allParams'],
110 LayoutRoute['types']['fullSearchSchema'],
111 LayoutRoute['types']['loaderData'],
112 LayoutRoute['types']['allContext'],
113 LayoutRoute['types']['loaderDeps']
114>
115β
116const commentsRoute = createRoute({
117 getParentRoute: () => layoutRoute,
118 path: 'comments/$id',
119 validateSearch: () => ({
120 page: 0,
121 search: '',
122 }),
123 loader: () =>
124 [{ comment: 'one comment' }, { comment: 'two comment' }] as const,
125})
126β
127type CommentsRoute = typeof commentsRoute
128β
129type CommentsMatch = RouteMatch<
130 CommentsRoute['id'],
131 CommentsRoute['fullPath'],
132 CommentsRoute['types']['allParams'],
133 CommentsRoute['types']['fullSearchSchema'],
134 CommentsRoute['types']['loaderData'],
135 CommentsRoute['types']['allContext'],
136 CommentsRoute['types']['loaderDeps']
137>
138β
139const routeTree = rootRoute.addChildren([
140 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
141 indexRoute,
142 layoutRoute.addChildren([commentsRoute]),
143])
144β
145const defaultRouter = createRouter({
146 routeTree,
147})
148β
149type DefaultRouter = typeof defaultRouter
150β
151const useDefaultMatchRoute = useMatchRoute<DefaultRouter>
152β
153test('when matching a route with params', () => {
154 const matchRoute = useDefaultMatchRoute()
155β
156 expectTypeOf(matchRoute<string, '/invoices/$invoiceId'>)
157 .parameter(0)
158 .toHaveProperty('to')
159 .toEqualTypeOf<
160 '/' | '.' | '..' | '/invoices' | '/invoices/$invoiceId' | '/comments/$id'
161 >()
162β
163 expectTypeOf(MatchRoute<DefaultRouter, string, '/invoices/$invoiceId'>)
164 .parameter(0)
165 .toHaveProperty('to')
166 .toEqualTypeOf<
167 '/' | '.' | '..' | '/invoices' | '/invoices/$invoiceId' | '/comments/$id'
168 >()
169β
170 expectTypeOf(
171 matchRoute({
172 to: '/invoices/$invoiceId',
173 }),
174 ).toEqualTypeOf<Solid.Accessor<false | { invoiceId: string }>>()
175})
176β
177test('when matching a route with params underneath a layout route', () => {
178 const matchRoute = useDefaultMatchRoute()
179β
180 expectTypeOf(
181 matchRoute({
182 to: '/comments/$id',
183 }),
184 ).toEqualTypeOf<Solid.Accessor<false | { id: string }>>()
185})
186β
187test('useMatches returns a union of all matches', () => {
188 expectTypeOf(useMatches<DefaultRouter>()()).toEqualTypeOf<
189 Array<
190 | RootMatch
191 | IndexMatch
192 | InvoicesMatch
193 | InvoicesIndexMatch
194 | InvoiceMatch
195 | LayoutMatch
196 | CommentsMatch
197 >
198 >
199})
200β
201test('when filtering useMatches by search', () => {
202 const matches = useMatches<DefaultRouter>()()
203β
204 expectTypeOf(isMatch<(typeof matches)[number], ''>)
205 .parameter(1)
206 .toEqualTypeOf<keyof AnyRouteMatch>()
207β
208 expectTypeOf(isMatch<(typeof matches)[number], 'search.'>).parameter(1)
209 .toEqualTypeOf<'search.page' | 'search.search'>
210β
211 expectTypeOf(
212 matches.filter((match) => isMatch(match, 'search.page')),
213 ).toEqualTypeOf<Array<InvoiceMatch | CommentsMatch>>()
214})
215β
216test('when filtering useMatches by loaderData with an array', () => {
217 const matches = useMatches<DefaultRouter>()()
218β
219 expectTypeOf(isMatch<(typeof matches)[number], ''>)
220 .parameter(1)
221 .toEqualTypeOf<keyof AnyRouteMatch>()
222β
223 expectTypeOf(isMatch<(typeof matches)[number], 'loaderData.'>)
224 .parameter(1)
225 .toEqualTypeOf<'loaderData.0' | 'loaderData.1' | `loaderData.${number}`>()
226β
227 expectTypeOf(isMatch<(typeof matches)[number], 'loaderData.0.'>).parameter(1)
228 .toEqualTypeOf<'loaderData.0.id' | 'loaderData.0.comment'>
229β
230 expectTypeOf(
231 matches.filter((match) => isMatch(match, 'loaderData.5.id')),
232 ).toEqualTypeOf<Array<InvoicesMatch>>()
233β
234 expectTypeOf(
235 matches.filter((match) => isMatch(match, 'loaderData.0.comment')),
236 ).toEqualTypeOf<Array<CommentsMatch>>()
237})
238β
239test('MatchRoute children are typed from resolved params under pathless layouts', () => {
240 type CommentsMatchRouteChildren = MakeMatchRouteOptions<
241 DefaultRouter,
242 string,
243 '/comments/$id'
244 >['children']
245 type CommentsMatchRouteRenderFn = Extract<
246 CommentsMatchRouteChildren,
247 (...args: Array<any>) => any
248 >
249β
250 expectTypeOf<CommentsMatchRouteRenderFn>().toEqualTypeOf<
251 (params?: { id: string } | undefined) => Solid.JSX.Element
252 >()
253})