@@ -82,8 +82,7 @@ describe('EnhanceAffiliateLinks', () => {
8282 ) ;
8383 } ) ;
8484
85- it ( 'should include UTM parameters in xcust if present' , ( ) => {
86- // replace window.location safely
85+ it ( 'should use article URL UTM parameters when present' , ( ) => {
8786 Object . defineProperty ( window , 'location' , {
8887 value : new URL (
8988 'https://example.test/this?utm_source=growth&utm_medium=epicuk&utm_campaign=q3_test&utm_content=filter_general' ,
@@ -100,4 +99,54 @@ describe('EnhanceAffiliateLinks', () => {
10099 'utm_source%7Cgrowth%7Cutm_medium%7Cepicuk%7Cutm_campaign%7Cq3_test%7Cutm_content%7Cfilter_general' ,
101100 ) ;
102101 } ) ;
102+
103+ it ( 'should use UTM parameters from the referrer if the article URL has none' , ( ) => {
104+ Object . defineProperty ( window , 'location' , {
105+ value : new URL ( 'https://example.test/page' ) ,
106+ configurable : true ,
107+ } ) ;
108+
109+ Object . defineProperty ( document , 'referrer' , {
110+ value : 'https://foo.bar/some?utm_source=testsource&utm_medium=somemedium&utm_campaign=refcamp' ,
111+ configurable : true ,
112+ } ) ;
113+
114+ document . body . innerHTML = `
115+ <a href="https://go.skimresources.com/?id=12345">Skimlink</a>
116+ ` ;
117+
118+ render ( < EnhanceAffiliateLinks /> ) ;
119+
120+ const link = document . querySelector ( 'a' ) ;
121+
122+ expect ( link ?. href ) . toContain (
123+ 'utm_source%7Ctestsource%7Cutm_medium%7Csomemedium%7Cutm_campaign%7Crefcamp' ,
124+ ) ;
125+ } ) ;
126+
127+ it ( 'should use UTM parameters from the article URL over the referrer if both exist' , ( ) => {
128+ Object . defineProperty ( window , 'location' , {
129+ value : new URL (
130+ 'https://example.test/page?utm_source=pagegrow&utm_medium=somemedium' ,
131+ ) ,
132+ configurable : true ,
133+ } ) ;
134+
135+ Object . defineProperty ( document , 'referrer' , {
136+ value : 'https://foo.bar?utm_source=refgrow&utm_medium=refmed' ,
137+ configurable : true ,
138+ } ) ;
139+
140+ document . body . innerHTML = `
141+ <a href="https://go.skimresources.com/?id=12345">Skimlink</a>
142+ ` ;
143+
144+ render ( < EnhanceAffiliateLinks /> ) ;
145+
146+ const link = document . querySelector ( 'a' ) ;
147+ expect ( link ?. href ) . toContain (
148+ 'utm_source%7Cpagegrow%7Cutm_medium%7Csomemedium' ,
149+ ) ;
150+ expect ( link ?. href ) . not . toContain ( 'refgrow' ) ;
151+ } ) ;
103152} ) ;
0 commit comments