Open History to see the last commit
1import { expect, test, vi } from 'vitest'
2import { fireEvent, render, screen, waitFor } from '@solidjs/testing-library'
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 params: {
75 parse: (params) => {
76 mockedfn()
77 return {
78 ...params,
79 postId: params.postId === 'one' ? '1' : '2',
80 }
81 },
82 },
83 component: PostComponent,
84 loader: ({ params }) => ({
85 post: posts.find((post) => post.id === parseInt(params.postId)),
86 }),
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 from={postCategoryRoute.fullPath}
123 to="./$postId"
124 params={{ postId: id }}
125 data-testid={`post-${id}-link`}
126 >
127 {post.title}
128 </Link>
129 )
130 })}
131 <Outlet />
132 </div>
133 )
134 }
135β
136 function PostComponent() {
137 const params = useParams({ from: postRoute.fullPath })
138β
139 const data = postRoute.useLoaderData()
140β
141 return (
142 <div>
143 <h1 data-testid="post-heading">Post Route</h1>
144 <div>
145 Category_Param:{' '}
146 <span data-testid="param_category_value">{params().category}</span>
147 </div>
148 <div>
149 PostId_Param:{' '}
150 <span data-testid="param_postId_value">{params().postId}</span>
151 </div>
152 <div>
153 PostId: <span data-testid="post_id_value">{data().post.id}</span>
154 </div>
155 <div>
156 Title: <span data-testid="post_title_value">{data().post.title}</span>
157 </div>
158 <div>
159 Category:{' '}
160 <span data-testid="post_category_value">{data().post.category}</span>
161 </div>
162 </div>
163 )
164 }
165β
166 window.history.replaceState({}, '', '/posts')
167β
168 const router = createRouter({
169 routeTree: rootRoute.addChildren([
170 postsRoute.addChildren([postCategoryRoute.addChildren([postRoute])]),
171 ]),
172 })
173β
174 render(() => <RouterProvider router={router} />)
175β
176 await waitFor(() => router.load())
177β
178 expect(await screen.findByTestId('posts-heading')).toBeInTheDocument()
179β
180 const firstCategoryLink = await screen.findByTestId('first-category-link')
181β
182 expect(firstCategoryLink).toBeInTheDocument()
183β
184 mockedfn.mockClear()
185 await waitFor(() => fireEvent.click(firstCategoryLink))
186β
187 const firstPostLink = await screen.findByTestId('post-one-link')
188β
189 expect(window.location.pathname).toBe('/posts/category_first')
190 expect(await screen.findByTestId('post-category-heading')).toBeInTheDocument()
191 // Dev-only buildLocation roundtrip validation uses normal matching for links.
192 expect(mockedfn).toHaveBeenCalled()
193β
194 mockedfn.mockClear()
195 await waitFor(() => 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 let renderedPost = {
204 id: parseInt(postIdValue.textContent),
205 title: postTitleValue.textContent,
206 category: postCategory.textContent,
207 }
208β
209 expect(window.location.pathname).toBe('/posts/category_first/one')
210 expect(await screen.findByTestId('post-heading')).toBeInTheDocument()
211 expect(renderedPost).toEqual(posts[0])
212 expect(renderedPost.category).toBe('one')
213 expect(paramCategoryValue.textContent).toBe('one')
214 expect(paramPostIdValue.textContent).toBe('1')
215 expect(mockedfn).toHaveBeenCalled()
216 // maybe we could theoretically reach 1 single call, but i'm not sure, building links depends on a bunch of things
217 // expect(mockedfn).toHaveBeenCalledTimes(1)
218 expect(allCategoryLink).toBeInTheDocument()
219β
220 mockedfn.mockClear()
221 await waitFor(() => fireEvent.click(allCategoryLink))
222β
223 const secondPostLink = await screen.findByTestId('post-two-link')
224β
225 expect(window.location.pathname).toBe('/posts/category_all')
226 expect(await screen.findByTestId('post-category-heading')).toBeInTheDocument()
227 expect(secondPostLink).toBeInTheDocument()
228 // expect(mockedfn).not.toHaveBeenCalled()
229β
230 mockedfn.mockClear()
231 await waitFor(() => fireEvent.click(secondPostLink))
232β
233 paramCategoryValue = await screen.findByTestId('param_category_value')
234 paramPostIdValue = await screen.findByTestId('param_postId_value')
235 postCategory = await screen.findByTestId('post_category_value')
236 postTitleValue = await screen.findByTestId('post_title_value')
237 postIdValue = await screen.findByTestId('post_id_value')
238 renderedPost = {
239 id: parseInt(postIdValue.textContent),
240 title: postTitleValue.textContent,
241 category: postCategory.textContent,
242 }
243β
244 expect(window.location.pathname).toBe('/posts/category_all/two')
245 expect(await screen.findByTestId('post-heading')).toBeInTheDocument()
246 expect(renderedPost).toEqual(posts[1])
247 expect(renderedPost.category).toBe('two')
248 expect(paramCategoryValue.textContent).toBe('all')
249 expect(paramPostIdValue.textContent).toBe('2')
250 expect(mockedfn).toHaveBeenCalled()
251})