Open History to see the last commit
1import '@testing-library/jest-dom/vitest'
2import { afterEach, describe, expect, test, vi } from 'vitest'
3import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library'
4β
5import { z } from 'zod'
6import { createSignal } from 'solid-js'
7import {
8 Block,
9 RouterProvider,
10 createRootRoute,
11 createRoute,
12 createRouter,
13 useBlocker,
14 useNavigate,
15} from '../src'
16import type { Setter } from 'solid-js'
17β
18afterEach(() => {
19 window.history.replaceState(null, 'root', '/')
20 cleanup()
21})
22β
23describe('useBlocker', () => {
24 test('does not block navigation when not enabled', async () => {
25 const rootRoute = createRootRoute()
26β
27 const IndexComponent = () => {
28 const navigate = useNavigate()
29β
30 useBlocker({ shouldBlockFn: () => false })
31β
32 return (
33 <>
34 <h1>Index</h1>
35 <button onClick={() => navigate({ to: '/' })}>Index</button>
36 <button onClick={() => navigate({ to: '/posts' })}>Posts</button>
37 </>
38 )
39 }
40β
41 const indexRoute = createRoute({
42 getParentRoute: () => rootRoute,
43 path: '/',
44 component: IndexComponent,
45 })
46β
47 const postsRoute = createRoute({
48 getParentRoute: () => rootRoute,
49 path: '/posts',
50 component: () => {
51 return (
52 <>
53 <h1>Posts</h1>
54 </>
55 )
56 },
57 })
58β
59 const router = createRouter({
60 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
61 })
62β
63 render(() => <RouterProvider router={router} />)
64β
65 const postsButton = await screen.findByRole('button', { name: 'Posts' })
66β
67 fireEvent.click(postsButton)
68β
69 expect(
70 await screen.findByRole('heading', { name: 'Posts' }),
71 ).toBeInTheDocument()
72β
73 expect(window.location.pathname).toBe('/posts')
74 })
75β
76 test('does not block navigation when disabled', async () => {
77 const rootRoute = createRootRoute()
78β
79 const IndexComponent = () => {
80 const navigate = useNavigate()
81β
82 useBlocker({ shouldBlockFn: () => true, disabled: true })
83β
84 return (
85 <>
86 <h1>Index</h1>
87 <button onClick={() => navigate({ to: '/' })}>Index</button>
88 <button onClick={() => navigate({ to: '/posts' })}>Posts</button>
89 </>
90 )
91 }
92β
93 const indexRoute = createRoute({
94 getParentRoute: () => rootRoute,
95 path: '/',
96 component: IndexComponent,
97 })
98β
99 const postsRoute = createRoute({
100 getParentRoute: () => rootRoute,
101 path: '/posts',
102 component: () => {
103 return (
104 <>
105 <h1>Posts</h1>
106 </>
107 )
108 },
109 })
110β
111 const router = createRouter({
112 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
113 })
114β
115 render(() => <RouterProvider router={router} />)
116β
117 const postsButton = await screen.findByRole('button', { name: 'Posts' })
118β
119 fireEvent.click(postsButton)
120β
121 expect(
122 await screen.findByRole('heading', { name: 'Posts' }),
123 ).toBeInTheDocument()
124β
125 expect(window.location.pathname).toBe('/posts')
126 })
127β
128 test('blocks navigation when enabled', async () => {
129 const rootRoute = createRootRoute()
130β
131 const IndexComponent = () => {
132 const navigate = useNavigate()
133β
134 useBlocker({ shouldBlockFn: () => true })
135β
136 return (
137 <>
138 <h1>Index</h1>
139 <button onClick={() => navigate({ to: '/' })}>Index</button>
140 <button onClick={() => navigate({ to: '/posts' })}>Posts</button>
141 </>
142 )
143 }
144β
145 const indexRoute = createRoute({
146 getParentRoute: () => rootRoute,
147 path: '/',
148 component: IndexComponent,
149 })
150β
151 const postsRoute = createRoute({
152 getParentRoute: () => rootRoute,
153 path: '/posts',
154 component: () => {
155 return (
156 <>
157 <h1>Posts</h1>
158 </>
159 )
160 },
161 })
162β
163 const router = createRouter({
164 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
165 })
166β
167 render(() => <RouterProvider router={router} />)
168β
169 const postsButton = await screen.findByRole('button', { name: 'Posts' })
170β
171 fireEvent.click(postsButton)
172β
173 expect(
174 await screen.findByRole('heading', { name: 'Index' }),
175 ).toBeInTheDocument()
176β
177 expect(window.location.pathname).toBe('/')
178 })
179β
180 test('gives correct arguments to shouldBlockFn', async () => {
181 const rootRoute = createRootRoute()
182β
183 const shouldBlockFn = vi.fn().mockReturnValue(true)
184β
185 const IndexComponent = () => {
186 const navigate = useNavigate()
187β
188 useBlocker({ shouldBlockFn })
189β
190 return (
191 <>
192 <h1>Index</h1>
193 <button onClick={() => navigate({ to: '/' })}>Index</button>
194 <button onClick={() => navigate({ to: '/posts', replace: true })}>
195 Posts
196 </button>
197 </>
198 )
199 }
200β
201 const indexRoute = createRoute({
202 getParentRoute: () => rootRoute,
203 path: '/',
204 component: IndexComponent,
205 })
206β
207 const postsRoute = createRoute({
208 getParentRoute: () => rootRoute,
209 path: '/posts',
210 component: () => {
211 return (
212 <>
213 <h1>Posts</h1>
214 </>
215 )
216 },
217 })
218β
219 const router = createRouter({
220 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
221 })
222β
223 render(() => <RouterProvider router={router} />)
224β
225 const postsButton = await screen.findByRole('button', { name: 'Posts' })
226β
227 fireEvent.click(postsButton)
228β
229 expect(
230 await screen.findByRole('heading', { name: 'Index' }),
231 ).toBeInTheDocument()
232β
233 expect(window.location.pathname).toBe('/')
234β
235 expect(shouldBlockFn).toHaveBeenCalledWith({
236 action: 'REPLACE',
237 current: {
238 routeId: indexRoute.id,
239 fullPath: indexRoute.fullPath,
240 pathname: '/',
241 params: {},
242 search: {},
243 },
244 next: {
245 routeId: postsRoute.id,
246 fullPath: postsRoute.fullPath,
247 pathname: '/posts',
248 params: {},
249 search: {},
250 },
251 })
252 })
253β
254 test('gives correct arguments to shouldBlockFn with path and search params', async () => {
255 const rootRoute = createRootRoute()
256β
257 const shouldBlockFn = vi.fn().mockReturnValue(true)
258β
259 const IndexComponent = () => {
260 const navigate = useNavigate()
261β
262 useBlocker({ shouldBlockFn })
263β
264 return (
265 <>
266 <h1>Index</h1>
267 <button onClick={() => navigate({ to: '/' })}>Index</button>
268 <button
269 onClick={() =>
270 navigate({
271 to: '/posts/$postId',
272 params: { postId: '10' },
273 search: { param1: 'foo', param2: 'bar' },
274 })
275 }
276 >
277 Posts
278 </button>
279 </>
280 )
281 }
282β
283 const indexRoute = createRoute({
284 getParentRoute: () => rootRoute,
285 path: '/',
286 component: IndexComponent,
287 })
288β
289 const postsRoute = createRoute({
290 getParentRoute: () => rootRoute,
291 validateSearch: z.object({
292 param1: z.string().default('param1-default'),
293 param2: z.string().default('param2-default'),
294 }),
295 path: '/posts/$postId',
296 component: () => {
297 return (
298 <>
299 <h1>Posts</h1>
300 </>
301 )
302 },
303 })
304β
305 const router = createRouter({
306 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
307 })
308β
309 render(() => <RouterProvider router={router} />)
310β
311 const postsButton = await screen.findByRole('button', { name: 'Posts' })
312β
313 fireEvent.click(postsButton)
314β
315 expect(
316 await screen.findByRole('heading', { name: 'Index' }),
317 ).toBeInTheDocument()
318β
319 expect(window.location.pathname).toBe('/')
320β
321 expect(shouldBlockFn).toHaveBeenCalledWith({
322 action: 'PUSH',
323 current: {
324 routeId: indexRoute.id,
325 fullPath: indexRoute.fullPath,
326 pathname: '/',
327 params: {},
328 search: {},
329 },
330 next: {
331 routeId: postsRoute.id,
332 fullPath: postsRoute.fullPath,
333 pathname: '/posts/10',
334 params: { postId: '10' },
335 search: { param1: 'foo', param2: 'bar' },
336 },
337 })
338 })
339β
340 test('conditionally blocking navigation works', async () => {
341 const rootRoute = createRootRoute()
342β
343 const IndexComponent = () => {
344 const navigate = useNavigate()
345β
346 useBlocker<Router>({
347 shouldBlockFn: ({ next }) => {
348 if (next.fullPath === '/posts') {
349 return true
350 }
351 return false
352 },
353 })
354β
355 return (
356 <>
357 <h1>Index</h1>
358 <button onClick={() => navigate({ to: '/' })}>Index</button>
359 <button onClick={() => navigate({ to: '/posts' })}>Posts</button>
360 <button onClick={() => navigate({ to: '/invoices' })}>
361 Invoices
362 </button>
363 </>
364 )
365 }
366β
367 const indexRoute = createRoute({
368 getParentRoute: () => rootRoute,
369 path: '/',
370 component: IndexComponent,
371 })
372β
373 const postsRoute = createRoute({
374 getParentRoute: () => rootRoute,
375 path: '/posts',
376 component: () => {
377 return (
378 <>
379 <h1>Posts</h1>
380 </>
381 )
382 },
383 })
384β
385 const invoicesRoute = createRoute({
386 getParentRoute: () => rootRoute,
387 path: '/invoices',
388 component: () => {
389 return (
390 <>
391 <h1>Invoices</h1>
392 </>
393 )
394 },
395 })
396β
397 const router = createRouter({
398 routeTree: rootRoute.addChildren([indexRoute, postsRoute, invoicesRoute]),
399 })
400β
401 type Router = typeof router
402β
403 render(() => <RouterProvider router={router} />)
404β
405 const postsButton = await screen.findByRole('button', { name: 'Posts' })
406β
407 fireEvent.click(postsButton)
408β
409 expect(
410 await screen.findByRole('heading', { name: 'Index' }),
411 ).toBeInTheDocument()
412β
413 expect(window.location.pathname).toBe('/')
414β
415 const invoicesButton = await screen.findByRole('button', {
416 name: 'Invoices',
417 })
418β
419 fireEvent.click(invoicesButton)
420β
421 expect(
422 await screen.findByRole('heading', { name: 'Invoices' }),
423 ).toBeInTheDocument()
424β
425 expect(window.location.pathname).toBe('/invoices')
426 })
427β
428 test('<Block /> disabled property is reactive', async () => {
429 const rootRoute = createRootRoute()
430β
431 let _setDisabled: Setter<boolean> = null!
432β
433 const IndexComponent = () => {
434 const navigate = useNavigate()
435β
436 const [disabled, setDisabled] = createSignal(false)
437 _setDisabled = setDisabled
438β
439 return (
440 <>
441 <Block shouldBlockFn={() => true} disabled={disabled()} />
442 <h1>Index</h1>
443 <button onClick={() => navigate({ to: '/' })}>Index</button>
444 <button onClick={() => navigate({ to: '/posts' })}>Posts</button>
445 </>
446 )
447 }
448β
449 const indexRoute = createRoute({
450 getParentRoute: () => rootRoute,
451 path: '/',
452 component: IndexComponent,
453 })
454β
455 const postsRoute = createRoute({
456 getParentRoute: () => rootRoute,
457 path: '/posts',
458 component: () => {
459 return (
460 <>
461 <h1>Posts</h1>
462 </>
463 )
464 },
465 })
466β
467 const router = createRouter({
468 routeTree: rootRoute.addChildren([indexRoute, postsRoute]),
469 })
470β
471 render(() => <RouterProvider router={router} />)
472β
473 let postsButton = await screen.findByRole('button', { name: 'Posts' })
474β
475 fireEvent.click(postsButton)
476β
477 expect(
478 await screen.findByRole('heading', { name: 'Index' }),
479 ).toBeInTheDocument()
480β
481 expect(window.location.pathname).toBe('/')
482β
483 _setDisabled(true)
484β
485 postsButton = await screen.findByRole('button', { name: 'Posts' })
486β
487 fireEvent.click(postsButton)
488β
489 expect(
490 await screen.findByRole('heading', { name: 'Posts' }),
491 ).toBeInTheDocument()
492β
493 expect(window.location.pathname).toBe('/posts')
494 })
495})