@@ -16,6 +16,12 @@ jest.mock("./eventCollector", () => ({
1616let eventOnSpy : jest . SpyInstance ;
1717let eventOffSpy : jest . SpyInstance ;
1818
19+ // jsdom makes window.location non-configurable, so it can't be replaced with
20+ // Object.defineProperty. pushState updates pathname/search/href in place instead.
21+ function setLocation ( pathname : string , search = "" ) {
22+ window . history . pushState ( { } , "" , `${ pathname } ${ search } ` ) ;
23+ }
24+
1925beforeEach ( ( ) => {
2026 mockLogEvent . mockReset ( ) ;
2127 mockDestroyCollector . mockReset ( ) ;
@@ -26,15 +32,7 @@ beforeEach(() => {
2632 // Reset router to root
2733 mockRouter . setCurrentUrl ( "/" ) ;
2834
29- Object . defineProperty ( window , "location" , {
30- value : {
31- pathname : "/" ,
32- search : "" ,
33- href : "http://localhost/" ,
34- } ,
35- writable : true ,
36- configurable : true ,
37- } ) ;
35+ setLocation ( "/" ) ;
3836} ) ;
3937
4038afterEach ( ( ) => {
@@ -112,15 +110,10 @@ describe("AnalyticsProvider", () => {
112110
113111 describe ( "search property (item 9)" , ( ) => {
114112 it ( "includes non-UTM query params as search in initial page.viewed" , ( ) => {
115- Object . defineProperty ( window , "location" , {
116- value : {
117- pathname : "/search" ,
118- search : "?q=paris&guests=2&utm_source=google&utm_medium=cpc" ,
119- href : "http://localhost/search?q=paris&guests=2&utm_source=google&utm_medium=cpc" ,
120- } ,
121- writable : true ,
122- configurable : true ,
123- } ) ;
113+ setLocation (
114+ "/search" ,
115+ "?q=paris&guests=2&utm_source=google&utm_medium=cpc" ,
116+ ) ;
124117
125118 render (
126119 < AnalyticsProvider >
@@ -136,15 +129,7 @@ describe("AnalyticsProvider", () => {
136129 } ) ;
137130
138131 it ( "sets search to null when only UTM params are present" , ( ) => {
139- Object . defineProperty ( window , "location" , {
140- value : {
141- pathname : "/landing" ,
142- search : "?utm_source=google&utm_campaign=spring" ,
143- href : "http://localhost/landing?utm_source=google&utm_campaign=spring" ,
144- } ,
145- writable : true ,
146- configurable : true ,
147- } ) ;
132+ setLocation ( "/landing" , "?utm_source=google&utm_campaign=spring" ) ;
148133
149134 render (
150135 < AnalyticsProvider >
@@ -176,15 +161,10 @@ describe("AnalyticsProvider", () => {
176161
177162 describe ( "UTM params in session.started" , ( ) => {
178163 it ( "includes UTM params in session.started properties" , ( ) => {
179- Object . defineProperty ( window , "location" , {
180- value : {
181- pathname : "/" ,
182- search : "?utm_source=twitter&utm_medium=social&utm_campaign=launch" ,
183- href : "http://localhost/?utm_source=twitter&utm_medium=social&utm_campaign=launch" ,
184- } ,
185- writable : true ,
186- configurable : true ,
187- } ) ;
164+ setLocation (
165+ "/" ,
166+ "?utm_source=twitter&utm_medium=social&utm_campaign=launch" ,
167+ ) ;
188168
189169 render (
190170 < AnalyticsProvider >
0 commit comments