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