@@ -428,6 +428,18 @@ function setup({
428
428
} ) ;
429
429
}
430
430
431
+ // jsdom is making more and more properties non-configurable, so we inject
432
+ // our own jest-friendly window.
433
+ let testWindow = {
434
+ ...window ,
435
+ location : {
436
+ ...window . location ,
437
+ assign : jest . fn ( ) ,
438
+ replace : jest . fn ( ) ,
439
+ } ,
440
+ } as unknown as Window ;
441
+ // ^ Spread makes TS sad - `window.NaN` conflicts with `[index: number]: Window`
442
+
431
443
let history = createMemoryHistory ( { initialEntries, initialIndex } ) ;
432
444
jest . spyOn ( history , "push" ) ;
433
445
jest . spyOn ( history , "replace" ) ;
@@ -437,6 +449,7 @@ function setup({
437
449
routes : enhanceRoutes ( routes ) ,
438
450
hydrationData,
439
451
future,
452
+ window : testWindow ,
440
453
} ) . initialize ( ) ;
441
454
442
455
function getRouteHelpers (
@@ -843,6 +856,7 @@ function setup({
843
856
}
844
857
845
858
return {
859
+ window : testWindow ,
846
860
history,
847
861
router : currentRouter ,
848
862
navigate,
@@ -6545,15 +6559,6 @@ describe("a router", () => {
6545
6559
] ;
6546
6560
6547
6561
for ( let url of urls ) {
6548
- // This is gross, don't blame me, blame SO :)
6549
- // https://stackoverflow.com/a/60697570
6550
- let oldLocation = window . location ;
6551
- const location = new URL ( window . location . href ) as unknown as Location ;
6552
- location . assign = jest . fn ( ) ;
6553
- location . replace = jest . fn ( ) ;
6554
- delete ( window as any ) . location ;
6555
- window . location = location as unknown as Location ;
6556
-
6557
6562
let t = setup ( { routes : REDIRECT_ROUTES } ) ;
6558
6563
6559
6564
let A = await t . navigate ( "/parent/child" , {
@@ -6562,10 +6567,8 @@ describe("a router", () => {
6562
6567
} ) ;
6563
6568
6564
6569
await A . actions . child . redirectReturn ( url ) ;
6565
- expect ( window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6566
- expect ( window . location . replace ) . not . toHaveBeenCalled ( ) ;
6567
-
6568
- window . location = oldLocation ;
6570
+ expect ( t . window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6571
+ expect ( t . window . location . replace ) . not . toHaveBeenCalled ( ) ;
6569
6572
}
6570
6573
} ) ;
6571
6574
@@ -6578,15 +6581,6 @@ describe("a router", () => {
6578
6581
] ;
6579
6582
6580
6583
for ( let url of urls ) {
6581
- // This is gross, don't blame me, blame SO :)
6582
- // https://stackoverflow.com/a/60697570
6583
- let oldLocation = window . location ;
6584
- const location = new URL ( window . location . href ) as unknown as Location ;
6585
- location . assign = jest . fn ( ) ;
6586
- location . replace = jest . fn ( ) ;
6587
- delete ( window as any ) . location ;
6588
- window . location = location as unknown as Location ;
6589
-
6590
6584
let t = setup ( { routes : REDIRECT_ROUTES } ) ;
6591
6585
6592
6586
let A = await t . navigate ( "/parent/child" , {
@@ -6596,10 +6590,8 @@ describe("a router", () => {
6596
6590
} ) ;
6597
6591
6598
6592
await A . actions . child . redirectReturn ( url ) ;
6599
- expect ( window . location . replace ) . toHaveBeenCalledWith ( url ) ;
6600
- expect ( window . location . assign ) . not . toHaveBeenCalled ( ) ;
6601
-
6602
- window . location = oldLocation ;
6593
+ expect ( t . window . location . replace ) . toHaveBeenCalledWith ( url ) ;
6594
+ expect ( t . window . location . assign ) . not . toHaveBeenCalled ( ) ;
6603
6595
}
6604
6596
} ) ;
6605
6597
@@ -6654,15 +6646,6 @@ describe("a router", () => {
6654
6646
} ) ;
6655
6647
6656
6648
it ( "treats same-origin absolute URLs as external if they don't match the basename" , async ( ) => {
6657
- // This is gross, don't blame me, blame SO :)
6658
- // https://stackoverflow.com/a/60697570
6659
- let oldLocation = window . location ;
6660
- const location = new URL ( window . location . href ) as unknown as Location ;
6661
- location . assign = jest . fn ( ) ;
6662
- location . replace = jest . fn ( ) ;
6663
- delete ( window as any ) . location ;
6664
- window . location = location as unknown as Location ;
6665
-
6666
6649
let t = setup ( { routes : REDIRECT_ROUTES , basename : "/base" } ) ;
6667
6650
6668
6651
let A = await t . navigate ( "/base/parent/child" , {
@@ -6672,10 +6655,8 @@ describe("a router", () => {
6672
6655
6673
6656
let url = "http://localhost/not/the/same/basename" ;
6674
6657
await A . actions . child . redirectReturn ( url ) ;
6675
- expect ( window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6676
- expect ( window . location . replace ) . not . toHaveBeenCalled ( ) ;
6677
-
6678
- window . location = oldLocation ;
6658
+ expect ( t . window . location . assign ) . toHaveBeenCalledWith ( url ) ;
6659
+ expect ( t . window . location . replace ) . not . toHaveBeenCalled ( ) ;
6679
6660
} ) ;
6680
6661
6681
6662
describe ( "redirect status code handling" , ( ) => {
0 commit comments