11import { JSDOM } from 'jsdom'
22import { Analytics } from '../../core/analytics'
3- // @ts -ignore loadCDNSettings mocked dependency is accused as unused
43import { AnalyticsBrowser } from '..'
54import { setGlobalCDNUrl } from '../../lib/parse-cdn'
65import { TEST_WRITEKEY } from '../../test-helpers/test-writekeys'
6+ import { createMockFetchImplementation } from '../../test-helpers/fixtures/create-fetch-method'
7+ import { parseFetchCall } from '../../test-helpers/fetch-parse'
8+ import { cdnSettingsKitchenSink } from '../../test-helpers/fixtures/cdn-settings'
9+
10+ const fetchCalls : ReturnType < typeof parseFetchCall > [ ] = [ ]
11+ jest . mock ( 'unfetch' , ( ) => {
12+ return {
13+ __esModule : true ,
14+ default : ( url : RequestInfo , body ?: RequestInit ) => {
15+ const call = parseFetchCall ( [ url , body ] )
16+ fetchCalls . push ( call )
17+ return createMockFetchImplementation ( cdnSettingsKitchenSink ) ( url , body )
18+ } ,
19+ }
20+ } )
721
822const writeKey = TEST_WRITEKEY
923
1024describe ( 'queryString' , ( ) => {
1125 let jsd : JSDOM
1226
1327 beforeEach ( async ( ) => {
14- jest . restoreAllMocks ( )
15- jest . resetAllMocks ( )
16-
1728 const html = `
1829 <!DOCTYPE html>
1930 <head>
@@ -37,33 +48,41 @@ describe('queryString', () => {
3748 setGlobalCDNUrl ( undefined as any )
3849 } )
3950
40- it ( 'applies query string logic before analytics is finished initializing' , async ( ) => {
41- let analyticsInitializedBeforeQs : boolean | undefined
42- const originalQueryString = Analytics . prototype . queryString
43- const mockQueryString = jest
44- . fn ( )
45- . mockImplementation ( async function ( this : Analytics , ...args ) {
46- // simulate network latency when retrieving the bundle
47- await new Promise ( ( r ) => setTimeout ( r , 500 ) )
48- return originalQueryString . apply ( this , args ) . then ( ( result ) => {
49- // ensure analytics has not finished initializing before querystring completes
50- analyticsInitializedBeforeQs = this . initialized
51- return result
52- } )
53- } )
54- Analytics . prototype . queryString = mockQueryString
51+ it ( 'querystring events that update anonymousId have priority over other buffered events' , async ( ) => {
52+ const queryStringSpy = jest . spyOn ( Analytics . prototype , 'queryString' )
5553
5654 jsd . reconfigure ( {
5755 url : 'https://localhost/?ajs_aid=123' ,
5856 } )
5957
60- const [ analytics ] = await AnalyticsBrowser . load ( { writeKey } )
61- expect ( mockQueryString ) . toHaveBeenCalledWith ( '?ajs_aid=123' )
62- expect ( analyticsInitializedBeforeQs ) . toBe ( false )
63- // check that calls made immediately after analytics is loaded use correct anonymousId
64- const pageContext = await analytics . page ( )
58+ const analytics = new AnalyticsBrowser ( )
59+ const pagePromise = analytics . page ( )
60+ await analytics . load ( { writeKey } )
61+ expect ( queryStringSpy ) . toHaveBeenCalledWith ( '?ajs_aid=123' )
62+ const pageContext = await pagePromise
6563 expect ( pageContext . event . anonymousId ) . toBe ( '123' )
66- expect ( analytics . user ( ) . anonymousId ( ) ) . toBe ( '123' )
64+ const user = await analytics . user ( )
65+ expect ( user . anonymousId ( ) ) . toBe ( '123' )
66+ } )
67+
68+ it ( 'querystring events have middleware applied like any other event' , async ( ) => {
69+ jsd . reconfigure ( {
70+ url : 'https://localhost/?ajs_event=Clicked' ,
71+ } )
72+
73+ const analytics = new AnalyticsBrowser ( )
74+ void analytics . addSourceMiddleware ( ( { next, payload } ) => {
75+ payload . obj . event = payload . obj . event + ' Middleware Applied'
76+ return next ( payload )
77+ } )
78+ await analytics . load ( { writeKey } )
79+ const trackCalls = fetchCalls . filter (
80+ ( call ) => call . url === 'https://api.segment.io/v1/t'
81+ )
82+ expect ( trackCalls . length ) . toBe ( 1 )
83+ expect ( trackCalls [ 0 ] . body . event ) . toMatchInlineSnapshot (
84+ `"Clicked Middleware Applied"`
85+ )
6786 } )
6887
6988 it ( 'applies query string logic if window.location.search is present' , async ( ) => {
0 commit comments