@@ -10,6 +10,7 @@ import {
1010
1111import { z } from 'zod'
1212
13+ import { trailingSlashOptions } from '@tanstack/router-core'
1314import {
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+
50885179describe ( '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' ,
0 commit comments