@@ -4,6 +4,21 @@ import { Analytics } from '../../core/analytics'
44import { AnalyticsBrowser } from '..'
55import { setGlobalCDNUrl } from '../../lib/parse-cdn'
66import { TEST_WRITEKEY } from '../../test-helpers/test-writekeys'
7+ import { createMockFetchImplementation } from '../../test-helpers/fixtures/create-fetch-method'
8+ import { parseFetchCall } from '../../test-helpers/fetch-parse'
9+ import { cdnSettingsKitchenSink } from '../../test-helpers/fixtures/cdn-settings'
10+
11+ const fetchCalls : ReturnType < typeof parseFetchCall > [ ] = [ ]
12+ jest . mock ( 'unfetch' , ( ) => {
13+ return {
14+ __esModule : true ,
15+ default : ( url : RequestInfo , body ?: RequestInit ) => {
16+ const call = parseFetchCall ( [ url , body ] )
17+ fetchCalls . push ( call )
18+ return createMockFetchImplementation ( cdnSettingsKitchenSink ) ( url , body )
19+ } ,
20+ }
21+ } )
722
823const writeKey = TEST_WRITEKEY
924
@@ -56,17 +71,22 @@ describe('queryString', () => {
5671
5772 it ( 'querystring events have middleware applied like any other event' , async ( ) => {
5873 jsd . reconfigure ( {
59- url : 'https://localhost/?ajs_aid=123 ' ,
74+ url : 'https://localhost/?ajs_event=Clicked ' ,
6075 } )
6176
6277 const analytics = new AnalyticsBrowser ( )
6378 void analytics . addSourceMiddleware ( ( { next, payload } ) => {
64- payload . obj . context ! . page ! . url = 'http://foo.com '
79+ payload . obj . event = payload . obj . event + ' Middleware Applied '
6580 return next ( payload )
6681 } )
6782 await analytics . load ( { writeKey } )
68- const pageContext = await analytics . page ( )
69- expect ( pageContext . event . context ! . page ! . url ) . toBe ( 'http://foo.com' )
83+ const trackCalls = fetchCalls . filter (
84+ ( call ) => call . url === 'https://api.segment.io/v1/t'
85+ )
86+ expect ( trackCalls . length ) . toBe ( 1 )
87+ expect ( trackCalls [ 0 ] . body . event ) . toMatchInlineSnapshot (
88+ `"Clicked Middleware Applied"`
89+ )
7090 } )
7191
7292 it ( 'applies query string logic if window.location.search is present' , async ( ) => {
0 commit comments