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

Matches.test-d.tsx

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