Open History to see the last commit
1import { expect, test } from 'vitest'
2import { fireEvent, render, screen } from '@testing-library/react'
3import {
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10 isMatch,
11 useMatches,
12} from '../src'
13β
14const rootRoute = createRootRoute()
15β
16const indexRoute = createRoute({
17 getParentRoute: () => rootRoute,
18 path: '/',
19 component: () => {
20 return <Link to="/invoices/">To Invoices</Link>
21 },
22})
23β
24const invoicesRoute = createRoute({
25 getParentRoute: () => rootRoute,
26 path: 'invoices',
27 loader: () => [{ id: '1' }, { id: '2' }],
28 component: () => <Outlet />,
29})
30β
31const InvoicesIndex = () => {
32 const matches = useMatches<DefaultRouter>()
33β
34 const loaderDataMatches = matches.filter((match) =>
35 isMatch(match, 'loaderData.0.id'),
36 )
37β
38 const contextMatches = matches.filter((match) =>
39 isMatch(match, 'context.permissions'),
40 )
41β
42 const incorrectMatches = matches.filter((match) =>
43 isMatch(match, 'loaderData.6.id'),
44 )
45β
46 return (
47 <div>
48 <section>
49 Loader Matches -{' '}
50 {loaderDataMatches.map((match) => match.fullPath).join(',')}
51 </section>
52 <section>
53 Context Matches -{' '}
54 {contextMatches.map((match) => match.fullPath).join(',')}
55 </section>
56 <section>
57 Incorrect Matches -{' '}
58 {incorrectMatches.map((match) => match.fullPath).join(',')}
59 </section>
60 </div>
61 )
62}
63β
64const invoicesIndexRoute = createRoute({
65 getParentRoute: () => invoicesRoute,
66 path: '/',
67 component: InvoicesIndex,
68 context: () => ({
69 permissions: 'permission',
70 }),
71})
72β
73const invoiceRoute = createRoute({
74 getParentRoute: () => invoicesRoute,
75 path: '$invoiceId',
76 validateSearch: () => ({ page: 0 }),
77})
78β
79const layoutRoute = createRoute({
80 getParentRoute: () => rootRoute,
81 id: '_layout',
82})
83β
84const commentsRoute = createRoute({
85 getParentRoute: () => layoutRoute,
86 path: 'comments/$id',
87 validateSearch: () => ({
88 page: 0,
89 search: '',
90 }),
91 loader: () =>
92 [{ comment: 'one comment' }, { comment: 'two comment' }] as const,
93})
94β
95const routeTree = rootRoute.addChildren([
96 invoicesRoute.addChildren([invoicesIndexRoute, invoiceRoute]),
97 indexRoute,
98 layoutRoute.addChildren([commentsRoute]),
99])
100β
101const defaultRouter = createRouter({
102 routeTree,
103})
104β
105type DefaultRouter = typeof defaultRouter
106β
107test('when filtering useMatches by loaderData', async () => {
108 render(<RouterProvider router={defaultRouter} />)
109β
110 const searchLink = await screen.findByRole('link', { name: 'To Invoices' })
111β
112 fireEvent.click(searchLink)
113β
114 expect(
115 await screen.findByText('Loader Matches - /invoices'),
116 ).toBeInTheDocument()
117β
118 expect(
119 await screen.findByText('Context Matches - /invoices/'),
120 ).toBeInTheDocument()
121β
122 expect(await screen.findByText('Incorrect Matches -')).toBeInTheDocument()
123})
124β
125test('should show pendingComponent of root route', async () => {
126 const root = createRootRoute({
127 pendingComponent: () => <div data-testId="root-pending" />,
128 loader: async () => {
129 await new Promise((r) => setTimeout(r, 50))
130 },
131 component: () => <div data-testId="root-content" />,
132 })
133 const router = createRouter({
134 routeTree: root,
135 defaultPendingMs: 0,
136 defaultPendingComponent: () => <div>default pending...</div>,
137 })
138β
139 const rendered = render(<RouterProvider router={router} />)
140β
141 expect(await rendered.findByTestId('root-pending')).toBeInTheDocument()
142 expect(await rendered.findByTestId('root-content')).toBeInTheDocument()
143})