Open History to see the last commit
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})