@@ -2,7 +2,7 @@ import Ajv, { DefinedError } from 'ajv';
22
33import OpenApiSpec from '../../OpenAPISpec' ;
44import Recommendations from '../../../graphql-proxy/recommendations/recommendations' ;
5- import { responseTransformer } from './response' ;
5+ import { appendUtmSource , responseTransformer } from './response' ;
66import { WebAuth } from '../../../auth/types' ;
77
88jest . mock ( '../../../graphql-proxy/recommendations/recommendations' ) ;
@@ -47,9 +47,38 @@ describe('response', () => {
4747 if ( valid ) {
4848 // any additional expectations can be defined here
4949 expect ( res . data . length ) . toEqual ( 30 ) ;
50+ expect ( res . data [ 0 ] . url . endsWith ( 'utm_source=pocket-newtab-bff' ) ) ;
5051 } else {
5152 throw validate . errors ;
5253 }
5354 } ) ;
5455 } ) ;
56+
57+ describe ( 'appendUtmSource' , ( ) => {
58+ it ( 'should add a utm_source query parameter when input URL does not have any query parameters' , ( ) => {
59+ const url = 'https://example.com' ;
60+ const expected = 'https://example.com/?utm_source=pocket-newtab-bff' ;
61+ expect ( appendUtmSource ( url ) ) . toBe ( expected ) ;
62+ } ) ;
63+
64+ it ( 'should add a utm_source query parameter when the input URL already has a query parameter' , ( ) => {
65+ const url = 'https://example.com?foo=bar' ;
66+ const expected =
67+ 'https://example.com/?foo=bar&utm_source=pocket-newtab-bff' ;
68+ expect ( appendUtmSource ( url ) ) . toBe ( expected ) ;
69+ } ) ;
70+
71+ it ( 'should add utm_source query parameter when the input URL ends with a fragment' , ( ) => {
72+ const url = 'https://example.com#my-fragment' ;
73+ const expected =
74+ 'https://example.com/?utm_source=pocket-newtab-bff#my-fragment' ;
75+ expect ( appendUtmSource ( url ) ) . toBe ( expected ) ;
76+ } ) ;
77+
78+ it ( 'should override utm_source query parameter if the url already contains utm_source' , ( ) => {
79+ const url = 'https://example.com/?utm_source=fgfeed' ;
80+ const expected = 'https://example.com/?utm_source=pocket-newtab-bff' ;
81+ expect ( appendUtmSource ( url ) ) . toBe ( expected ) ;
82+ } ) ;
83+ } ) ;
5584} ) ;
0 commit comments