11import { describe , expect , test , vi } from 'vitest'
22import { createMemoryHistory } from '@tanstack/history'
33import { BaseRootRoute , BaseRoute } from '../src'
4+ import { replaceEqualDeep } from '../src/utils'
45import { createTestRouter } from './routerTestUtils'
56
67describe ( 'buildLocation - params function receives parsed params' , ( ) => {
@@ -1207,6 +1208,7 @@ describe('buildLocation - location output structure', () => {
12071208 // Verify all expected properties exist
12081209 expect ( location ) . toEqual ( {
12091210 external : false ,
1211+ getUrl : expect . any ( Function ) ,
12101212 hash : 'section' ,
12111213 href : '/posts?page=1#section' ,
12121214 origin : 'http://localhost:3000' ,
@@ -1248,7 +1250,7 @@ describe('buildLocation - location output structure', () => {
12481250 expect ( location . href ) . toBe ( '/posts' )
12491251 } )
12501252
1251- test ( 'location should expose a memoized lazy url ' , async ( ) => {
1253+ test ( 'location should expose a memoized lazy getUrl method ' , async ( ) => {
12521254 const rootRoute = new BaseRootRoute ( { } )
12531255 const postsRoute = new BaseRoute ( {
12541256 getParentRoute : ( ) => rootRoute ,
@@ -1270,14 +1272,14 @@ describe('buildLocation - location output structure', () => {
12701272 hash : 'section' ,
12711273 } )
12721274
1273- expect ( Object . keys ( location ) ) . not . toContain ( 'url' )
1274- expect ( location . url ) . toBe ( location . url )
1275- expect ( location . url . pathname ) . toBe ( '/posts' )
1276- expect ( location . url . search ) . toBe ( '?page=1' )
1277- expect ( location . url . hash ) . toBe ( '#section' )
1275+ expect ( location . getUrl ) . toBeDefined ( )
1276+ expect ( location . getUrl ( ) ) . toBe ( location . getUrl ( ) )
1277+ expect ( location . getUrl ( ) . pathname ) . toBe ( '/posts' )
1278+ expect ( location . getUrl ( ) . search ) . toBe ( '?page=1' )
1279+ expect ( location . getUrl ( ) . hash ) . toBe ( '#section' )
12781280 } )
12791281
1280- test ( 'router state location should expose url after parsing history' , async ( ) => {
1282+ test ( 'router state location should expose getUrl after parsing history' , async ( ) => {
12811283 const rootRoute = new BaseRootRoute ( { } )
12821284 const postsRoute = new BaseRoute ( {
12831285 getParentRoute : ( ) => rootRoute ,
@@ -1295,9 +1297,44 @@ describe('buildLocation - location output structure', () => {
12951297
12961298 await router . load ( )
12971299
1298- expect ( router . state . location . url . pathname ) . toBe ( '/posts' )
1299- expect ( router . state . location . url . search ) . toBe ( '?page=1' )
1300- expect ( router . state . location . url . hash ) . toBe ( '#section' )
1300+ expect ( router . state . location . getUrl ( ) . pathname ) . toBe ( '/posts' )
1301+ expect ( router . state . location . getUrl ( ) . search ) . toBe ( '?page=1' )
1302+ expect ( router . state . location . getUrl ( ) . hash ) . toBe ( '#section' )
1303+ } )
1304+
1305+ test ( 'replaceEqualDeep should preserve getUrl on structurally shared locations' , async ( ) => {
1306+ const rootRoute = new BaseRootRoute ( { } )
1307+ const postsRoute = new BaseRoute ( {
1308+ getParentRoute : ( ) => rootRoute ,
1309+ path : '/posts' ,
1310+ } )
1311+
1312+ const routeTree = rootRoute . addChildren ( [ postsRoute ] )
1313+
1314+ const router = createTestRouter ( {
1315+ routeTree,
1316+ history : createMemoryHistory ( {
1317+ initialEntries : [ '/posts?page=1#section' ] ,
1318+ } ) ,
1319+ } )
1320+
1321+ await router . load ( )
1322+
1323+ const prev = router . state . location
1324+ const next = router . buildLocation ( {
1325+ to : '/posts' ,
1326+ search : { page : 2 } ,
1327+ hash : 'section' ,
1328+ } )
1329+
1330+ const shared = replaceEqualDeep ( prev , next )
1331+
1332+ expect ( shared ) . not . toBe ( prev )
1333+ expect ( shared . getUrl ) . toBe ( prev . getUrl )
1334+ expect ( shared . search ) . not . toBe ( prev . search )
1335+ expect ( shared . getUrl ( ) . pathname ) . toBe ( '/posts' )
1336+ expect ( shared . getUrl ( ) . search ) . toBe ( '?page=2' )
1337+ expect ( shared . getUrl ( ) . hash ) . toBe ( '#section' )
13011338 } )
13021339} )
13031340
0 commit comments