import { afterEach, describe, expect, test, vi } from 'vitest' import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library' import { z } from 'zod' import { createSignal } from 'solid-js' import { Block, RouterProvider, createMemoryHistory, createRootRoute, createRoute, createRouter, useBlocker, useNavigate, } from '../src' import type { Setter } from 'solid-js' afterEach(() => { window.history.replaceState(null, 'root', '/') cleanup() }) describe('useBlocker', () => { test('does not block navigation when not enabled', async () => { const rootRoute = createRootRoute() const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn: () => false }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Posts' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/posts') }) test('does not block navigation when disabled', async () => { const rootRoute = createRootRoute() const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn: () => true, disabled: true }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Posts' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/posts') }) test('blocks navigation when enabled', async () => { const rootRoute = createRootRoute() const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn: () => true }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') }) test('gives correct arguments to shouldBlockFn', async () => { const rootRoute = createRootRoute() const shouldBlockFn = vi.fn().mockReturnValue(true) const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') expect(shouldBlockFn).toHaveBeenCalledWith({ action: 'REPLACE', current: { routeId: indexRoute.id, fullPath: indexRoute.fullPath, pathname: '/', params: {}, search: {}, }, next: { routeId: postsRoute.id, fullPath: postsRoute.fullPath, pathname: '/posts', params: {}, search: {}, }, }) }) test('gives correct arguments to shouldBlockFn with path and search params', async () => { const rootRoute = createRootRoute() const shouldBlockFn = vi.fn().mockReturnValue(true) const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, validateSearch: z.object({ param1: z.string().default('param1-default'), param2: z.string().default('param2-default'), }), path: '/posts/$postId', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') expect(shouldBlockFn).toHaveBeenCalledWith({ action: 'PUSH', current: { routeId: indexRoute.id, fullPath: indexRoute.fullPath, pathname: '/', params: {}, search: {}, }, next: { routeId: postsRoute.id, fullPath: postsRoute.fullPath, pathname: '/posts/10', params: { postId: '10' }, search: { param1: 'foo', param2: 'bar' }, }, }) }) test('conditionally blocking navigation works', async () => { const rootRoute = createRootRoute() const IndexComponent = () => { const navigate = useNavigate() useBlocker({ shouldBlockFn: ({ next }) => { if (next.fullPath === '/posts') { return true } return false }, }) return ( <>

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const invoicesRoute = createRoute({ getParentRoute: () => rootRoute, path: '/invoices', component: () => { return ( <>

Invoices

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute, invoicesRoute]), }) type Router = typeof router render(() => ) const postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') const invoicesButton = await screen.findByRole('button', { name: 'Invoices', }) fireEvent.click(invoicesButton) expect( await screen.findByRole('heading', { name: 'Invoices' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/invoices') }) test(' disabled property is reactive', async () => { const rootRoute = createRootRoute() let _setDisabled: Setter = null! const IndexComponent = () => { const navigate = useNavigate() const [disabled, setDisabled] = createSignal(false) _setDisabled = setDisabled return ( <> true} disabled={disabled()} />

Index

) } const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: IndexComponent, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) let postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') _setDisabled(true) postsButton = await screen.findByRole('button', { name: 'Posts' }) fireEvent.click(postsButton) expect( await screen.findByRole('heading', { name: 'Posts' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/posts') }) test('should allow navigation from 404 page when blocker is active', async () => { const rootRoute = createRootRoute({ notFoundComponent: function NotFoundComponent() { const navigate = useNavigate() useBlocker({ shouldBlockFn: () => true }) return ( <>

Not Found

) }, }) const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: () => { return ( <>

Index

) }, }) const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/posts', component: () => { return ( <>

Posts

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute, postsRoute]), }) render(() => ) await router.navigate({ to: '/non-existent' as any }) expect( await screen.findByRole('heading', { name: 'Not Found' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/non-existent') const homeButton = await screen.findByRole('button', { name: 'Go Home' }) fireEvent.click(homeButton) expect( await screen.findByRole('heading', { name: 'Index' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/') }) test('should handle blocker navigation from 404 to another 404', async () => { const rootRoute = createRootRoute({ notFoundComponent: function NotFoundComponent() { const navigate = useNavigate() useBlocker({ shouldBlockFn: () => true }) return ( <>

Not Found

) }, }) const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: () => { return ( <>

Index

) }, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute]), }) render(() => ) await router.navigate({ to: '/non-existent' }) expect( await screen.findByRole('heading', { name: 'Not Found' }), ).toBeInTheDocument() const anotherButton = await screen.findByRole('button', { name: 'Go to Another 404', }) fireEvent.click(anotherButton) expect( await screen.findByRole('heading', { name: 'Not Found' }), ).toBeInTheDocument() expect(window.location.pathname).toBe('/non-existent') }) test('navigate function should handle external URLs with ignoreBlocker', async () => { const rootRoute = createRootRoute() const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: () =>
Home
, }) const router = createRouter({ routeTree: rootRoute.addChildren([indexRoute]), history: createMemoryHistory({ initialEntries: ['/'], }), }) await expect( router.navigate({ to: 'https://example.com', ignoreBlocker: true, }), ).resolves.toBeUndefined() await expect( router.navigate({ to: 'https://example.com', }), ).resolves.toBeUndefined() }) })