@@ -167,7 +167,9 @@ describe('findRouteMatch', () => {
167167 } )
168168 it ( '/optional/static/static vs /static/static' , ( ) => {
169169 const tree = makeTree ( [ '/{-$other}/posts/new' , '/posts/new' ] )
170- expect ( findRouteMatch ( '/posts/new' , tree ) ?. route . id ) . toBe ( '/posts/new' )
170+ expect ( findRouteMatch ( '/posts/new' , tree ) ?. route . id ) . toBe (
171+ '/{-$other}/posts/new' ,
172+ )
171173 } )
172174 it ( '/optional/static/static/dynamic vs /static/dynamic/static/dynamic' , ( ) => {
173175 const tree = makeTree ( [ '/{-$other}/posts/a/b/$c' , '/posts/$a/b/$c' ] )
@@ -223,6 +225,14 @@ describe('findRouteMatch', () => {
223225 expect ( findRouteMatch ( '/a/b' , tree ) ?. route . id ) . toBe ( '/a/{-$b}/$c' )
224226 } )
225227 } )
228+ it ( 'optional child vs. shorter route' , ( ) => {
229+ const tree = makeTree ( [ '/a' , '/a/{-$b}' ] )
230+ expect ( findRouteMatch ( '/a' , tree ) ?. route . id ) . toBe ( '/a/{-$b}' )
231+
232+ // but index can still win over optional child
233+ const treeWithIndex = makeTree ( [ '/a/' , '/a/{-$b}' ] )
234+ expect ( findRouteMatch ( '/a' , treeWithIndex ) ?. route . id ) . toBe ( '/a/' )
235+ } )
226236 } )
227237 } )
228238
@@ -487,14 +497,25 @@ describe('findRouteMatch', () => {
487497 const present = findRouteMatch ( '/yo/foo123bar/ma' , tree )
488498 expect ( present ?. route . id ) . toBe ( '/yo/foo{-$id}bar/ma' )
489499 } )
490- it ( 'edge-case: ??? ' , ( ) => {
500+ it ( 'edge-case: deeper optional chain vs. shallower optional chain ' , ( ) => {
491501 // This test comes from the previous processRouteTree tests.
492- // > This demonstrates that `/foo/{-$p}.tsx` will be matched, not `/foo/{-$p}/{-$x }.tsx`
493- // > This route has 1 optional parameter, making it more specific than the route with 2
502+ // > This demonstrates that `/foo/{-$p}/{-$x} .tsx` will be matched, not `/foo/{-$p}.tsx`
503+ // > This route has 2 optional parameter, making it more specific than the route with 1
494504 const tree = makeTree ( [ '/foo/{-$p}.tsx' , '/foo/{-$p}/{-$x}.tsx' ] )
495- expect ( findRouteMatch ( '/foo' , tree ) ?. route . id ) . toBe ( '/foo/{-$p}.tsx' )
505+ expect ( findRouteMatch ( '/foo' , tree ) ?. route . id ) . toBe (
506+ '/foo/{-$p}/{-$x}.tsx' ,
507+ )
496508 expect ( findRouteMatch ( '/foo/bar.tsx' , tree ) ?. route . id ) . toBe (
497- '/foo/{-$p}.tsx' ,
509+ '/foo/{-$p}/{-$x}.tsx' ,
510+ )
511+
512+ // but index can still win over deeper optional chain
513+ const treeWithIndex = makeTree ( [
514+ '/foo/{-$p}.tsx/' ,
515+ '/foo/{-$p}/{-$x}.tsx' ,
516+ ] )
517+ expect ( findRouteMatch ( '/foo/' , treeWithIndex ) ?. route . id ) . toBe (
518+ '/foo/{-$p}.tsx/' ,
498519 )
499520 } )
500521
0 commit comments