@@ -245,7 +245,7 @@ describe("matchPath", () => {
245245 } ) ;
246246} ) ;
247247
248- describe ( "matchPath optional segments" , ( ) => {
248+ describe ( "matchPath optional dynamic segments" , ( ) => {
249249 it ( "should match when optional segment is provided" , ( ) => {
250250 const match = matchPath ( "/:lang?/user/:id" , "/en/user/123" ) ;
251251 expect ( match ) . toMatchObject ( { params : { lang : "en" , id : "123" } } ) ;
@@ -292,6 +292,87 @@ describe("matchPath optional segments", () => {
292292 } ) ;
293293} ) ;
294294
295+ describe ( "matchPath optional static segments" , ( ) => {
296+ it ( "should match when optional segment is provided" , ( ) => {
297+ const match = matchPath ( "/school?/user/:id" , "/school/user/123" ) ;
298+ expect ( match ) . toMatchObject ( {
299+ pathname : "/school/user/123" ,
300+ pathnameBase : "/school/user/123" ,
301+ } ) ;
302+ } ) ;
303+
304+ it ( "should match when optional segment is *not* provided" , ( ) => {
305+ const match = matchPath ( "/school?/user/:id" , "/user/123" ) ;
306+ expect ( match ) . toMatchObject ( {
307+ pathname : "/user/123" ,
308+ pathnameBase : "/user/123" ,
309+ } ) ;
310+ } ) ;
311+
312+ it ( "should match when middle optional segment is provided" , ( ) => {
313+ const match = matchPath ( "/school/user?/:id" , "/school/user/123" ) ;
314+ expect ( match ) . toMatchObject ( {
315+ pathname : "/school/user/123" ,
316+ pathnameBase : "/school/user/123" ,
317+ } ) ;
318+ } ) ;
319+
320+ it ( "should match when middle optional segment is *not* provided" , ( ) => {
321+ const match = matchPath ( "/school/user?/:id" , "/school/123" ) ;
322+ expect ( match ) . toMatchObject ( {
323+ pathname : "/school/123" ,
324+ pathnameBase : "/school/123" ,
325+ } ) ;
326+ } ) ;
327+
328+ it ( "should match when end optional segment is provided" , ( ) => {
329+ const match = matchPath ( "/school/user/admin?" , "/school/user/admin" ) ;
330+ expect ( match ) . toMatchObject ( {
331+ pathname : "/school/user/admin" ,
332+ pathnameBase : "/school/user/admin" ,
333+ } ) ;
334+ } ) ;
335+
336+ it ( "should match when end optional segment is *not* provided" , ( ) => {
337+ const match = matchPath ( "/school/user/admin?" , "/school/user" ) ;
338+ expect ( match ) . toMatchObject ( {
339+ pathname : "/school/user" ,
340+ pathnameBase : "/school/user" ,
341+ } ) ;
342+ } ) ;
343+
344+ it ( "should match multiple optional segments and none are provided" , ( ) => {
345+ const match = matchPath ( "/school?/user/admin?" , "/user" ) ;
346+ expect ( match ) . toMatchObject ( {
347+ pathname : "/user" ,
348+ pathnameBase : "/user" ,
349+ } ) ;
350+ } ) ;
351+
352+ it ( "should match multiple optional segments and one is provided" , ( ) => {
353+ const match = matchPath ( "/school?/user/admin?" , "/user/admin" ) ;
354+ expect ( match ) . toMatchObject ( {
355+ pathname : "/user/admin" ,
356+ pathnameBase : "/user/admin" ,
357+ } ) ;
358+ } ) ;
359+
360+ it ( "should match multiple optional segments and all are provided" , ( ) => {
361+ const match = matchPath ( "/school?/user/admin?" , "/school/user/admin" ) ;
362+ expect ( match ) . toMatchObject ( {
363+ pathname : "/school/user/admin" ,
364+ pathnameBase : "/school/user/admin" ,
365+ } ) ;
366+ } ) ;
367+
368+ it ( "does not trigger from question marks in the middle of the optional static segment" , ( ) => {
369+ let match = matchPath ( "/school?abc/user/:id" , "/abc/user/123" ) ;
370+ expect ( match ) . toBe ( null ) ;
371+ match = matchPath ( "/school?abc" , "/abc" ) ;
372+ expect ( match ) . toBe ( null ) ;
373+ } ) ;
374+ } ) ;
375+
295376describe ( "matchPath *" , ( ) => {
296377 it ( "matches the root URL" , ( ) => {
297378 expect ( matchPath ( "*" , "/" ) ) . toMatchObject ( {
0 commit comments