Open History to see the last commit
1import { expect, test, vi } from 'vitest'
2import { act, fireEvent, render, screen } from '@testing-library/react'
3import {
4 Link,
5 Outlet,
6 RouterProvider,
7 createRootRoute,
8 createRoute,
9 createRouter,
10 useParams,
11} from '../src'
12β
13test('useParams must return parsed result if applicable.', async () => {
14 const posts = [
15 {
16 id: 1,
17 title: 'First Post',
18 category: 'one',
19 },
20 {
21 id: 2,
22 title: 'Second Post',
23 category: 'two',
24 },
25 ]
26β
27 const mockedfn = vi.fn()
28 const rootRoute = createRootRoute()
29β
30 const postsRoute = createRoute({
31 getParentRoute: () => rootRoute,
32 path: 'posts',
33 component: PostsComponent,
34 })
35β
36 const postCategoryRoute = createRoute({
37 getParentRoute: () => postsRoute,
38 path: 'category_{$category}',
39 component: PostCategoryComponent,
40 params: {
41 parse: (params) => {
42 return {
43 ...params,
44 category:
45 params.category === 'first'
46 ? 'one'
47 : params.category === 'second'
48 ? 'two'
49 : params.category,
50 }
51 },
52 stringify: (params) => {
53 return {
54 category:
55 params.category === 'one'
56 ? 'first'
57 : params.category === 'two'
58 ? 'second'
59 : params.category,
60 }
61 },
62 },
63 loader: ({ params }) => ({
64 posts:
65 params.category === 'all'
66 ? posts
67 : posts.filter((post) => post.category === params.category),
68 }),
69 })
70β
71 const postRoute = createRoute({
72 getParentRoute: () => postCategoryRoute,
73 path: '$postId',
74 loader: ({ params }) => {
75 return { post: posts.find((post) => post.id === parseInt(params.postId)) }
76 },
77 params: {
78 parse: (params) => {
79 mockedfn()
80 return {
81 ...params,
82 postId: params.postId === 'one' ? '1' : '2',
83 }
84 },
85 },
86 component: PostComponent,
87 })
88β
89 function PostsComponent() {
90 return (
91 <div>
92 <h1 data-testid="posts-heading">Posts</h1>
93 <Link
94 data-testid="all-category-link"
95 to={postCategoryRoute.fullPath}
96 params={{ category: 'all' }}
97 >
98 All Categories
99 </Link>
100 <Link
101 data-testid="first-category-link"
102 to={postCategoryRoute.fullPath}
103 params={{ category: 'first' }}
104 >
105 First Category
106 </Link>
107 <Outlet />
108 </div>
109 )
110 }
111β
112 function PostCategoryComponent() {
113 const data = postCategoryRoute.useLoaderData()
114β
115 return (
116 <div>
117 <h1 data-testid="post-category-heading">Post Categories</h1>
118 {data.posts.map((post: (typeof posts)[number]) => {
119 const id = post.id === 1 ? 'one' : 'two'
120 return (
121 <Link
122 key={id}
123 from={postCategoryRoute.fullPath}
124 to="./$postId"
125 params={{ postId: id }}
126 data-testid={`post-${id}-link`}
127 >
128 {post.title}
129 </Link>
130 )
131 })}
132 <Outlet />
133 </div>
134 )
135 }
136β
137 function PostComponent() {
138 const params = useParams({ from: postRoute.fullPath })
139β
140 const data = postRoute.useLoaderData()
141β
142 return (
143 <div>
144 <h1 data-testid="post-heading">Post Route</h1>
145 <div>
146 Category_Param:{' '}
147 <span data-testid="param_category_value">{params.category}</span>
148 </div>
149 <div>
150 PostId_Param:{' '}
151 <span data-testid="param_postId_value">{params.postId}</span>
152 </div>
153 <div>
154 PostId: <span data-testid="post_id_value">{data.post.id}</span>
155 </div>
156 <div>
157 Title: <span data-testid="post_title_value">{data.post.title}</span>
158 </div>
159 <div>
160 Category:{' '}
161 <span data-testid="post_category_value">{data.post.category}</span>
162 </div>
163 </div>
164 )
165 }
166β
167 window.history.replaceState({}, '', '/posts')
168β
169 const router = createRouter({
170 routeTree: rootRoute.addChildren([
171 postsRoute.addChildren([postCategoryRoute.addChildren([postRoute])]),
172 ]),
173 })
174β
175 render(<RouterProvider router={router} />)
176β
177 await act(() => router.load())
178β
179 expect(await screen.findByTestId('posts-heading')).toBeInTheDocument()
180β
181 const firstCategoryLink = await screen.findByTestId('first-category-link')
182β
183 expect(firstCategoryLink).toBeInTheDocument()
184β
185 mockedfn.mockClear()
186 await act(() => fireEvent.click(firstCategoryLink))
187β
188 const firstPostLink = await screen.findByTestId('post-one-link')
189β
190 expect(window.location.pathname).toBe('/posts/category_first')
191 expect(await screen.findByTestId('post-category-heading')).toBeInTheDocument()
192 expect(mockedfn).toHaveBeenCalledTimes(1)
193β
194 mockedfn.mockClear()
195 await act(() => fireEvent.click(firstPostLink))
196β
197 const allCategoryLink = await screen.findByTestId('all-category-link')
198 let paramCategoryValue = await screen.findByTestId('param_category_value')
199 let paramPostIdValue = await screen.findByTestId('param_postId_value')
200 let postCategory = await screen.findByTestId('post_category_value')
201 let postTitleValue = await screen.findByTestId('post_title_value')
202 let postIdValue = await screen.findByTestId('post_id_value')
203β
204 let renderedPost = {
205 id: parseInt(postIdValue.textContent),
206 title: postTitleValue.textContent,
207 category: postCategory.textContent,
208 }
209β
210 expect(window.location.pathname).toBe('/posts/category_first/one')
211 expect(await screen.findByTestId('post-heading')).toBeInTheDocument()
212 expect(renderedPost).toEqual(posts[0])
213 expect(renderedPost.category).toBe('one')
214 expect(paramCategoryValue.textContent).toBe('one')
215 expect(paramPostIdValue.textContent).toBe('1')
216 expect(mockedfn).toHaveBeenCalledTimes(2)
217 expect(allCategoryLink).toBeInTheDocument()
218β
219 mockedfn.mockClear()
220 await act(() => fireEvent.click(allCategoryLink))
221β
222 const secondPostLink = await screen.findByTestId('post-two-link')
223β
224 expect(window.location.pathname).toBe('/posts/category_all')
225 expect(await screen.findByTestId('post-category-heading')).toBeInTheDocument()
226 expect(secondPostLink).toBeInTheDocument()
227 expect(mockedfn).toHaveBeenCalledTimes(2)
228β
229 mockedfn.mockClear()
230 await act(() => fireEvent.click(secondPostLink))
231β
232 paramCategoryValue = await screen.findByTestId('param_category_value')
233 paramPostIdValue = await screen.findByTestId('param_postId_value')
234 postCategory = await screen.findByTestId('post_category_value')
235 postTitleValue = await screen.findByTestId('post_title_value')
236 postIdValue = await screen.findByTestId('post_id_value')
237 renderedPost = {
238 id: parseInt(postIdValue.textContent),
239 title: postTitleValue.textContent,
240 category: postCategory.textContent,
241 }
242β
243 expect(window.location.pathname).toBe('/posts/category_all/two')
244 expect(await screen.findByTestId('post-heading')).toBeInTheDocument()
245 expect(renderedPost).toEqual(posts[1])
246 expect(renderedPost.category).toBe('two')
247 expect(paramCategoryValue.textContent).toBe('all')
248 expect(paramPostIdValue.textContent).toBe('2')
249 expect(mockedfn).toHaveBeenCalledTimes(2)
250})