@@ -302,6 +302,7 @@ type SetupOpts = {
302302 future ?: FutureConfig ;
303303} ;
304304
305+ let globalWindow = window ;
305306function setup ( {
306307 routes,
307308 basename,
@@ -428,6 +429,15 @@ function setup({
428429 } ) ;
429430 }
430431
432+ // jsdom is making more and more properties non-configurable, so we inject our own jest-friendly window 😅
433+ let window = {
434+ ...globalWindow ,
435+ location : {
436+ ...globalWindow . location ,
437+ assign : jest . fn ( ) ,
438+ replace : jest . fn ( ) ,
439+ } ,
440+ } as unknown as Window ; // spread makes TS sad, since `window.NaN` conflicts with the `[index: number]: Window` index signature
431441 let history = createMemoryHistory ( { initialEntries, initialIndex } ) ;
432442 jest . spyOn ( history , "push" ) ;
433443 jest . spyOn ( history , "replace" ) ;
@@ -437,6 +447,7 @@ function setup({
437447 routes : enhanceRoutes ( routes ) ,
438448 hydrationData,
439449 future,
450+ window,
440451 } ) . initialize ( ) ;
441452
442453 function getRouteHelpers (
@@ -843,6 +854,7 @@ function setup({
843854 }
844855
845856 return {
857+ window,
846858 history,
847859 router : currentRouter ,
848860 navigate,
@@ -6496,15 +6508,6 @@ describe("a router", () => {
64966508 ] ;
64976509
64986510 for ( let url of urls ) {
6499- // This is gross, don't blame me, blame SO :)
6500- // https://stackoverflow.com/a/60697570
6501- let oldLocation = window . location ;
6502- const location = new URL ( window . location . href ) as unknown as Location ;
6503- location . assign = jest . fn ( ) ;
6504- location . replace = jest . fn ( ) ;
6505- delete ( window as any ) . location ;
6506- window . location = location as unknown as Location ;
6507-
65086511 let t = setup ( { routes : REDIRECT_ROUTES } ) ;
65096512
65106513 let A = await t . navigate ( "/parent/child" , {
@@ -6513,10 +6516,8 @@ describe("a router", () => {
65136516 } ) ;
65146517
65156518 await A . actions . child . redirectReturn ( url ) ;
6516- expect ( window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6517- expect ( window . location . replace ) . not . toHaveBeenCalled ( ) ;
6518-
6519- window . location = oldLocation ;
6519+ expect ( t . window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6520+ expect ( t . window . location . replace ) . not . toHaveBeenCalled ( ) ;
65206521 }
65216522 } ) ;
65226523
@@ -6529,15 +6530,6 @@ describe("a router", () => {
65296530 ] ;
65306531
65316532 for ( let url of urls ) {
6532- // This is gross, don't blame me, blame SO :)
6533- // https://stackoverflow.com/a/60697570
6534- let oldLocation = window . location ;
6535- const location = new URL ( window . location . href ) as unknown as Location ;
6536- location . assign = jest . fn ( ) ;
6537- location . replace = jest . fn ( ) ;
6538- delete ( window as any ) . location ;
6539- window . location = location as unknown as Location ;
6540-
65416533 let t = setup ( { routes : REDIRECT_ROUTES } ) ;
65426534
65436535 let A = await t . navigate ( "/parent/child" , {
@@ -6547,10 +6539,8 @@ describe("a router", () => {
65476539 } ) ;
65486540
65496541 await A . actions . child . redirectReturn ( url ) ;
6550- expect ( window . location . replace ) . toHaveBeenCalledWith ( url ) ;
6551- expect ( window . location . assign ) . not . toHaveBeenCalled ( ) ;
6552-
6553- window . location = oldLocation ;
6542+ expect ( t . window . location . replace ) . toHaveBeenCalledWith ( url ) ;
6543+ expect ( t . window . location . assign ) . not . toHaveBeenCalled ( ) ;
65546544 }
65556545 } ) ;
65566546
@@ -6605,15 +6595,6 @@ describe("a router", () => {
66056595 } ) ;
66066596
66076597 it ( "treats same-origin absolute URLs as external if they don't match the basename" , async ( ) => {
6608- // This is gross, don't blame me, blame SO :)
6609- // https://stackoverflow.com/a/60697570
6610- let oldLocation = window . location ;
6611- const location = new URL ( window . location . href ) as unknown as Location ;
6612- location . assign = jest . fn ( ) ;
6613- location . replace = jest . fn ( ) ;
6614- delete ( window as any ) . location ;
6615- window . location = location as unknown as Location ;
6616-
66176598 let t = setup ( { routes : REDIRECT_ROUTES , basename : "/base" } ) ;
66186599
66196600 let A = await t . navigate ( "/base/parent/child" , {
@@ -6623,10 +6604,8 @@ describe("a router", () => {
66236604
66246605 let url = "http://localhost/not/the/same/basename" ;
66256606 await A . actions . child . redirectReturn ( url ) ;
6626- expect ( window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6627- expect ( window . location . replace ) . not . toHaveBeenCalled ( ) ;
6628-
6629- window . location = oldLocation ;
6607+ expect ( t . window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6608+ expect ( t . window . location . replace ) . not . toHaveBeenCalled ( ) ;
66306609 } ) ;
66316610
66326611 describe ( "redirect status code handling" , ( ) => {
0 commit comments