Skip to content

Commit 7cc7d00

Browse files
committed
Added useNavigate test
1 parent 3e11ca1 commit 7cc7d00

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

packages/react-router/tests/useNavigate.test.tsx

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,3 +2555,126 @@ describe.each([{ basepath: '' }, { basepath: '/basepath' }])(
25552555
})
25562556
},
25572557
)
2558+
2559+
describe('splat routes with empty splat', () => {
2560+
test.each([{ trailingSlash: true }, { trailingSlash: false }])(
2561+
'should handle empty _splat parameter with trailingSlash: $trailingSlash',
2562+
async ({ trailingSlash }) => {
2563+
const tail = trailingSlash ? '/' : ''
2564+
2565+
const rootRoute = createRootRoute()
2566+
const indexRoute = createRoute({
2567+
getParentRoute: () => rootRoute,
2568+
path: '/',
2569+
component: function IndexComponent() {
2570+
const navigate = useNavigate()
2571+
return (
2572+
<>
2573+
<h1>Index Route</h1>
2574+
<button
2575+
data-testid="splat-btn-with-empty-splat"
2576+
onClick={() =>
2577+
navigate({
2578+
to: '/splat/$',
2579+
params: { _splat: '' },
2580+
})
2581+
}
2582+
type="button"
2583+
>
2584+
Navigate to splat with empty _splat
2585+
</button>
2586+
<button
2587+
data-testid="splat-btn-with-undefined-splat"
2588+
onClick={() =>
2589+
navigate({
2590+
to: '/splat/$',
2591+
params: { _splat: undefined },
2592+
})
2593+
}
2594+
type="button"
2595+
>
2596+
Navigate to splat with undefined _splat
2597+
</button>
2598+
<button
2599+
data-testid="splat-btn-with-no-splat"
2600+
onClick={() =>
2601+
navigate({
2602+
to: '/splat/$',
2603+
params: {},
2604+
})
2605+
}
2606+
type="button"
2607+
>
2608+
Navigate to splat with no _splat
2609+
</button>
2610+
</>
2611+
)
2612+
},
2613+
})
2614+
2615+
const splatRoute = createRoute({
2616+
getParentRoute: () => rootRoute,
2617+
path: 'splat/$',
2618+
component: () => {
2619+
return <h1>Splat Route</h1>
2620+
},
2621+
})
2622+
2623+
const router = createRouter({
2624+
routeTree: rootRoute.addChildren([indexRoute, splatRoute]),
2625+
history,
2626+
trailingSlash: trailingSlash ? 'always' : 'never',
2627+
})
2628+
2629+
render(<RouterProvider router={router} />)
2630+
2631+
// Navigate with empty _splat
2632+
const splatBtnWithEmptySplat = await screen.findByTestId(
2633+
'splat-btn-with-empty-splat',
2634+
)
2635+
2636+
await act(async () => {
2637+
fireEvent.click(splatBtnWithEmptySplat)
2638+
})
2639+
2640+
expect(window.location.pathname).toBe(`/splat${tail}`)
2641+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2642+
2643+
// Navigate back to index
2644+
await act(async () => {
2645+
history.push('/')
2646+
})
2647+
2648+
// Navigate with undefined _splat
2649+
const splatBtnWithUndefinedSplat = await screen.findByTestId(
2650+
'splat-btn-with-undefined-splat',
2651+
)
2652+
2653+
await act(async () => {
2654+
fireEvent.click(splatBtnWithUndefinedSplat)
2655+
})
2656+
2657+
expect(window.location.pathname).toBe(`/splat${tail}`)
2658+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2659+
2660+
// Navigate back to index
2661+
await act(async () => {
2662+
history.push('/')
2663+
})
2664+
2665+
// Navigate with no _splat
2666+
const splatBtnWithNoSplat = await screen.findByTestId(
2667+
'splat-btn-with-no-splat',
2668+
)
2669+
2670+
await act(async () => {
2671+
fireEvent.click(splatBtnWithNoSplat)
2672+
})
2673+
2674+
expect(window.location.pathname).toBe(`/splat${tail}`)
2675+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2676+
2677+
2678+
},
2679+
)
2680+
})

0 commit comments

Comments
 (0)