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

Matches.test-d.tsx

235 lines6.0 KB

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']
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']
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']
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']
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']
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']
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']
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})