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

useMatch.test.tsx

121 lines3.3 KB

1import { afterEach, describe, expect, test, vi } from 'vitest'
2import { cleanup, render, screen, waitFor } from '@testing-library/react'
3import {
4 Link,
5 Outlet,
6 RouterProvider,
7 createMemoryHistory,
8 createRootRoute,
9 createRoute,
10 createRouter,
11 useMatch,
12} from '../src'
13import type { RouteComponent, RouterHistory } from '../src'
14​
15afterEach(() => {
16 window.history.replaceState(null, 'root', '/')
17 cleanup()
18})
19​
20describe('useMatch', () => {
21 function setup({
22 RootComponent,
23 history,
24 }: {
25 RootComponent: RouteComponent
26 history?: RouterHistory
27 }) {
28 const rootRoute = createRootRoute({
29 component: RootComponent,
30 })
31 const indexRoute = createRoute({
32 getParentRoute: () => rootRoute,
33 path: '/',
34 component: () => (
35 <>
36 <h1>IndexTitle</h1>
37 <Link to="/posts">Posts</Link>
38 </>
39 ),
40 })
41​
42 history = history || createMemoryHistory({ initialEntries: ['/'] })
43​
44 const postsRoute = createRoute({
45 getParentRoute: () => rootRoute,
46 path: '/posts',
47 component: () => <h1>PostsTitle</h1>,
48 })
49​
50 const router = createRouter({
51 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
52 history,
53 })
54​
55 return render(<RouterProvider router={router} />)
56 }
57​
58 describe('when match is found', () => {
59 test.each([true, false, undefined])(
60 'returns the match if shouldThrow = %s',
61 async (shouldThrow) => {
62 function RootComponent() {
63 const match = useMatch({ from: '/posts', shouldThrow })
64 expect(match).toBeDefined()
65 expect(match!.routeId).toBe('/posts')
66 return <Outlet />
67 }
68​
69 setup({
70 RootComponent,
71 history: createMemoryHistory({ initialEntries: ['/posts'] }),
72 })
73 const postsTitle = await screen.findByText('PostsTitle')
74 expect(postsTitle).toBeInTheDocument()
75 },
76 )
77 })
78​
79 describe('when match is not found', () => {
80 test.each([undefined, true])(
81 'throws if shouldThrow = %s',
82 async (shouldThrow) => {
83 function RootComponent() {
84 useMatch({ from: '/posts', shouldThrow })
85 return <Outlet />
86 }
87 setup({ RootComponent })
88 const postsError = await screen.findByText(
89 'Invariant failed: Could not find an active match from "/posts"',
90 )
91 expect(postsError).toBeInTheDocument()
92 },
93 )
94​
95 describe('returns undefined if shouldThrow = false', () => {
96 test('without select function', async () => {
97 function RootComponent() {
98 const match = useMatch({ from: 'posts', shouldThrow: false })
99 expect(match).toBeUndefined()
100 return <Outlet />
101 }
102 setup({ RootComponent })
103 expect(
104 await waitFor(() => screen.findByText('IndexTitle')),
105 ).toBeInTheDocument()
106 })
107 test('with select function', async () => {
108 const select = vi.fn()
109 function RootComponent() {
110 const match = useMatch({ from: 'posts', shouldThrow: false, select })
111 expect(match).toBeUndefined()
112 return <Outlet />
113 }
114 setup({ RootComponent })
115 const indexTitle = await screen.findByText('IndexTitle')
116 expect(indexTitle).toBeInTheDocument()
117 expect(select).not.toHaveBeenCalled()
118 })
119 })
120 })
121})