Skip to content

Commit 92a1269

Browse files
committed
replicate tests for solid
1 parent b74616e commit 92a1269

3 files changed

Lines changed: 268 additions & 0 deletions

File tree

‎packages/solid-router/tests/link.test.tsx‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010

1111
import { z } from 'zod'
1212

13+
import { trailingSlashOptions } from '@tanstack/router-core'
1314
import {
1415
Link,
1516
Outlet,
@@ -5085,6 +5086,96 @@ describe.each([{ basepath: '' }, { basepath: '/basepath' }])(
50855086
},
50865087
)
50875088

5089+
describe('splat routes with empty splat', () => {
5090+
test.each(Object.values(trailingSlashOptions))(
5091+
'should handle empty _splat parameter with trailingSlash: $trailingSlash',
5092+
async (trailingSlash) => {
5093+
const tail = trailingSlash === 'always' ? '/' : ''
5094+
5095+
const rootRoute = createRootRoute()
5096+
const indexRoute = createRoute({
5097+
getParentRoute: () => rootRoute,
5098+
path: '/',
5099+
component: () => {
5100+
return (
5101+
<>
5102+
<h1>Index Route</h1>
5103+
<Link
5104+
data-testid="splat-link-with-empty-splat"
5105+
to="/splat/$"
5106+
params={{ _splat: '' }}
5107+
activeProps={{ class: 'active' }}
5108+
>
5109+
Link to splat with _splat value
5110+
</Link>
5111+
<Link
5112+
data-testid="splat-link-with-undefined-splat"
5113+
to="/splat/$"
5114+
params={{ _splat: undefined }}
5115+
activeProps={{ class: 'active' }}
5116+
>
5117+
Link to splat with undefined _splat
5118+
</Link>
5119+
<Link
5120+
data-testid="splat-link-with-no-splat"
5121+
to="/splat/$"
5122+
params={{}}
5123+
activeProps={{ class: 'active' }}
5124+
>
5125+
Link to splat with no _splat at all
5126+
</Link>
5127+
</>
5128+
)
5129+
},
5130+
})
5131+
5132+
const splatRoute = createRoute({
5133+
getParentRoute: () => rootRoute,
5134+
path: 'splat/$',
5135+
component: () => {
5136+
return <h1>Splat Route</h1>
5137+
},
5138+
})
5139+
5140+
const router = createRouter({
5141+
routeTree: rootRoute.addChildren([indexRoute, splatRoute]),
5142+
history,
5143+
trailingSlash,
5144+
})
5145+
5146+
render(() => <RouterProvider router={router} />)
5147+
5148+
const splatLinkWithEmptySplat = await screen.findByTestId(
5149+
'splat-link-with-empty-splat',
5150+
)
5151+
const splatLinkWithUndefinedSplat = await screen.findByTestId(
5152+
'splat-link-with-undefined-splat',
5153+
)
5154+
const splatLinkWithNoSplat = await screen.findByTestId(
5155+
'splat-link-with-no-splat',
5156+
)
5157+
5158+
// When _splat has a value, it should follow the trailingSlash setting
5159+
expect(splatLinkWithEmptySplat.getAttribute('href')).toBe(`/splat${tail}`)
5160+
expect(splatLinkWithUndefinedSplat.getAttribute('href')).toBe(
5161+
`/splat${tail}`,
5162+
)
5163+
expect(splatLinkWithNoSplat.getAttribute('href')).toBe(`/splat${tail}`)
5164+
5165+
// Click the link with empty _splat and ensure the route matches
5166+
await waitFor(() => {
5167+
fireEvent.click(splatLinkWithEmptySplat)
5168+
})
5169+
5170+
expect(splatLinkWithEmptySplat).toHaveClass('active')
5171+
expect(splatLinkWithUndefinedSplat).toHaveClass('active')
5172+
expect(splatLinkWithNoSplat).toHaveClass('active')
5173+
expect(window.location.pathname).toBe(`/splat${tail}`)
5174+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
5175+
},
5176+
)
5177+
})
5178+
50885179
describe('relative links to current route', () => {
50895180
test.each([true, false])(
50905181
'should navigate to current route when using "." in nested route structure from Index Route with trailingSlash: %s',

‎packages/solid-router/tests/navigate.test.tsx‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest'
22

3+
import { trailingSlashOptions } from '@tanstack/router-core'
34
import {
45
createMemoryHistory,
56
createRootRoute,
@@ -567,3 +568,57 @@ describe('relative navigation', () => {
567568
expect(router.state.location.pathname).toBe('/posts/tkdodo')
568569
})
569570
})
571+
572+
describe('splat routes with empty splat', () => {
573+
it.each(Object.values(trailingSlashOptions))(
574+
'should handle empty _splat parameter with trailingSlash: $trailingSlash',
575+
async (trailingSlash) => {
576+
const tail = trailingSlash === 'always' ? '/' : ''
577+
578+
const history = createMemoryHistory({ initialEntries: ['/'] })
579+
580+
const rootRoute = createRootRoute()
581+
const indexRoute = createRoute({
582+
getParentRoute: () => rootRoute,
583+
path: '/',
584+
})
585+
586+
const splatRoute = createRoute({
587+
getParentRoute: () => rootRoute,
588+
path: 'splat/$',
589+
})
590+
591+
const router = createRouter({
592+
routeTree: rootRoute.addChildren([indexRoute, splatRoute]),
593+
history,
594+
trailingSlash,
595+
})
596+
597+
await router.load()
598+
599+
// All of these route params should navigate to the same location
600+
const paramSets = [
601+
{
602+
_splat: '',
603+
},
604+
{
605+
_splat: undefined,
606+
},
607+
{},
608+
]
609+
610+
for (const params of paramSets) {
611+
await router.navigate({
612+
to: '/splat/$',
613+
params,
614+
})
615+
await router.invalidate()
616+
617+
expect(router.state.location.pathname).toBe(`/splat${tail}`)
618+
// Navigate back to index
619+
await router.navigate({ to: '/' })
620+
await router.invalidate()
621+
}
622+
},
623+
)
624+
})

‎packages/solid-router/tests/useNavigate.test.tsx‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@solidjs/testing-library'
1111

1212
import { z } from 'zod'
13+
import { trailingSlashOptions } from '@tanstack/router-core'
1314
import {
1415
Outlet,
1516
RouterProvider,
@@ -2523,3 +2524,124 @@ describe('relative navigate to from route', () => {
25232524
},
25242525
)
25252526
})
2527+
2528+
describe('splat routes with empty splat', () => {
2529+
test.each(Object.values(trailingSlashOptions))(
2530+
'should handle empty _splat parameter with trailingSlash: $trailingSlash',
2531+
async (trailingSlash) => {
2532+
const tail = trailingSlash === 'always' ? '/' : ''
2533+
2534+
const rootRoute = createRootRoute()
2535+
const indexRoute = createRoute({
2536+
getParentRoute: () => rootRoute,
2537+
path: '/',
2538+
component: function IndexComponent() {
2539+
const navigate = useNavigate()
2540+
return (
2541+
<>
2542+
<h1>Index Route</h1>
2543+
<button
2544+
data-testid="splat-btn-with-empty-splat"
2545+
onClick={() =>
2546+
navigate({
2547+
to: '/splat/$',
2548+
params: { _splat: '' },
2549+
})
2550+
}
2551+
type="button"
2552+
>
2553+
Navigate to splat with empty _splat
2554+
</button>
2555+
<button
2556+
data-testid="splat-btn-with-undefined-splat"
2557+
onClick={() =>
2558+
navigate({
2559+
to: '/splat/$',
2560+
params: { _splat: undefined },
2561+
})
2562+
}
2563+
type="button"
2564+
>
2565+
Navigate to splat with undefined _splat
2566+
</button>
2567+
<button
2568+
data-testid="splat-btn-with-no-splat"
2569+
onClick={() =>
2570+
navigate({
2571+
to: '/splat/$',
2572+
params: {},
2573+
})
2574+
}
2575+
type="button"
2576+
>
2577+
Navigate to splat with no _splat
2578+
</button>
2579+
</>
2580+
)
2581+
},
2582+
})
2583+
2584+
const splatRoute = createRoute({
2585+
getParentRoute: () => rootRoute,
2586+
path: 'splat/$',
2587+
component: () => {
2588+
return <h1>Splat Route</h1>
2589+
},
2590+
})
2591+
2592+
const router = createRouter({
2593+
routeTree: rootRoute.addChildren([indexRoute, splatRoute]),
2594+
history,
2595+
trailingSlash,
2596+
})
2597+
2598+
render(() => <RouterProvider router={router} />)
2599+
2600+
// Navigate with empty _splat
2601+
const splatBtnWithEmptySplat = await screen.findByTestId(
2602+
'splat-btn-with-empty-splat',
2603+
)
2604+
2605+
await waitFor(() => {
2606+
fireEvent.click(splatBtnWithEmptySplat)
2607+
})
2608+
2609+
expect(window.location.pathname).toBe(`/splat${tail}`)
2610+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2611+
2612+
// Navigate back to index
2613+
await waitFor(() => {
2614+
history.push('/')
2615+
})
2616+
2617+
// Navigate with undefined _splat
2618+
const splatBtnWithUndefinedSplat = await screen.findByTestId(
2619+
'splat-btn-with-undefined-splat',
2620+
)
2621+
2622+
await waitFor(() => {
2623+
fireEvent.click(splatBtnWithUndefinedSplat)
2624+
})
2625+
2626+
expect(window.location.pathname).toBe(`/splat${tail}`)
2627+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2628+
2629+
// Navigate back to index
2630+
await waitFor(() => {
2631+
history.push('/')
2632+
})
2633+
2634+
// Navigate with no _splat
2635+
const splatBtnWithNoSplat = await screen.findByTestId(
2636+
'splat-btn-with-no-splat',
2637+
)
2638+
2639+
await waitFor(() => {
2640+
fireEvent.click(splatBtnWithNoSplat)
2641+
})
2642+
2643+
expect(window.location.pathname).toBe(`/splat${tail}`)
2644+
expect(await screen.findByText('Splat Route')).toBeInTheDocument()
2645+
},
2646+
)
2647+
})

0 commit comments

Comments
 (0)