Open History to see the last commit
1import { afterEach, describe, expect, test, vi } from 'vitest'
2import { cleanup, render, screen, waitFor } from '@solidjs/testing-library'
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 const postsRoute = createRoute({
43 getParentRoute: () => rootRoute,
44 path: '/posts',
45 component: () => <h1>PostsTitle</h1>,
46 })
47β
48 const router = createRouter({
49 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
50 history,
51 })
52β
53 return render(() => <RouterProvider router={router} />)
54 }
55β
56 describe('when match is found', () => {
57 test.each([true, false, undefined])(
58 'returns the match if shouldThrow = %s',
59 async (shouldThrow) => {
60 function RootComponent() {
61 const match = useMatch({ from: '/posts', shouldThrow })
62 expect(match()).toBeDefined()
63 expect(match()!.routeId).toBe('/posts')
64 return <Outlet />
65 }
66β
67 setup({
68 RootComponent,
69 history: createMemoryHistory({ initialEntries: ['/posts'] }),
70 })
71 const postsTitle = await screen.findByText('PostsTitle')
72 expect(postsTitle).toBeInTheDocument()
73 },
74 )
75 })
76β
77 describe('when match is not found', () => {
78 test.each([undefined, true])(
79 'throws if shouldThrow = %s',
80 async (shouldThrow) => {
81 function RootComponent() {
82 useMatch({ from: '/posts', shouldThrow })
83 return <Outlet />
84 }
85 setup({ RootComponent })
86 const postsError = await screen.findByText(
87 'Invariant failed: Could not find an active match from "/posts"',
88 )
89 expect(postsError).toBeInTheDocument()
90 },
91 )
92β
93 describe('returns undefined if shouldThrow = false', () => {
94 test('without select function', async () => {
95 function RootComponent() {
96 const match = useMatch({ from: 'posts', shouldThrow: false })
97 expect(match()).toBeUndefined()
98 return <Outlet />
99 }
100 setup({ RootComponent })
101 expect(
102 await waitFor(() => screen.findByText('IndexTitle')),
103 ).toBeInTheDocument()
104 })
105 test('with select function', async () => {
106 const select = vi.fn()
107 function RootComponent() {
108 const match = useMatch({ from: 'posts', shouldThrow: false, select })
109 expect(match()).toBeUndefined()
110 return <Outlet />
111 }
112 setup({ RootComponent })
113 const indexTitle = await screen.findByText('IndexTitle')
114 expect(indexTitle).toBeInTheDocument()
115 expect(select).not.toHaveBeenCalled()
116 })
117 })
118 })
119})