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

useMatch.test-d.tsx

83 lines2.2 KB

1import { describe, expectTypeOf, test } from 'vitest'
2import { createRootRoute, createRoute, createRouter, useMatch } from '../src'
3import type { MakeRouteMatch, MakeRouteMatchUnion } from '@tanstack/router-core'
4import type * as Solid from 'solid-js'
5​
6const rootRoute = createRootRoute()
7​
8const indexRoute = createRoute({
9 getParentRoute: () => rootRoute,
10 path: '/',
11})
12​
13const invoicesRoute = createRoute({
14 getParentRoute: () => rootRoute,
15 path: 'invoices',
16})
17​
18const routeTree = rootRoute.addChildren([invoicesRoute, indexRoute])
19​
20const defaultRouter = createRouter({ routeTree })
21​
22type DefaultRouter = typeof defaultRouter
23​
24type TRouteMatch = MakeRouteMatch<DefaultRouter['routeTree']>
25​
26describe('useMatch', () => {
27 describe('shouldThrow', () => {
28 const from = '/invoices'
29 test('return type is `RouteMatch` when shouldThrow = true', () => {
30 const shouldThrow = true
31 const match = useMatch<
32 DefaultRouter,
33 typeof from,
34 true, // TStrict
35 typeof shouldThrow,
36 TRouteMatch
37 >({ from, shouldThrow })
38​
39 expectTypeOf(match).toEqualTypeOf<Solid.Accessor<TRouteMatch>>()
40 })
41​
42 test('return type is `RouteMatch | undefined` when shouldThrow = false', () => {
43 const shouldThrow = false
44 const match = useMatch<
45 DefaultRouter,
46 typeof from,
47 true, // TStrict
48 typeof shouldThrow,
49 TRouteMatch
50 >({ from, shouldThrow })
51​
52 expectTypeOf(match).toEqualTypeOf<
53 Solid.Accessor<TRouteMatch | undefined>
54 >()
55 })
56 })
57​
58 test('return type is union of matches when strict = false', () => {
59 const strict = false as const
60 const match = useMatch<DefaultRouter, typeof undefined, typeof strict>({
61 strict,
62 })
63​
64 expectTypeOf(match).toEqualTypeOf<
65 Solid.Accessor<MakeRouteMatchUnion<DefaultRouter>>
66 >()
67 })
68​
69 test('shouldThrow must be false when strict is false', () => {
70 const strict = false as const
71 const shouldThrow = true as const
72 useMatch<
73 DefaultRouter,
74 typeof undefined,
75 typeof strict,
76 typeof shouldThrow
77 >({
78 strict,
79 // @ts-expect-error shouldThrow must be false when strict is false
80 shouldThrow,
81 })
82 })
83})