@@ -37,33 +37,36 @@ describe('queryString', () => {
3737 setGlobalCDNUrl ( undefined as any )
3838 } )
3939
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
40+ it ( 'querystring events that update anonymousId have priority over other buffered events' , async ( ) => {
41+ const queryStringSpy = jest . spyOn ( Analytics . prototype , 'queryString' )
5542
5643 jsd . reconfigure ( {
5744 url : 'https://localhost/?ajs_aid=123' ,
5845 } )
5946
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 ( )
47+ const analytics = new AnalyticsBrowser ( )
48+ const pagePromise = analytics . page ( )
49+ await analytics . load ( { writeKey } )
50+ expect ( queryStringSpy ) . toHaveBeenCalledWith ( '?ajs_aid=123' )
51+ const pageContext = await pagePromise
6552 expect ( pageContext . event . anonymousId ) . toBe ( '123' )
66- expect ( analytics . user ( ) . anonymousId ( ) ) . toBe ( '123' )
53+ const user = await analytics . user ( )
54+ expect ( user . anonymousId ( ) ) . toBe ( '123' )
55+ } )
56+
57+ it ( 'querystring events have middleware applied like any other event' , async ( ) => {
58+ jsd . reconfigure ( {
59+ url : 'https://localhost/?ajs_aid=123' ,
60+ } )
61+
62+ const analytics = new AnalyticsBrowser ( )
63+ void analytics . addSourceMiddleware ( ( { next, payload } ) => {
64+ payload . obj . context ! . page ! . url = 'http://foo.com'
65+ return next ( payload )
66+ } )
67+ await analytics . load ( { writeKey } )
68+ const pageContext = await analytics . page ( )
69+ expect ( pageContext . event . context ! . page ! . url ) . toBe ( 'http://foo.com' )
6770 } )
6871
6972 it ( 'applies query string logic if window.location.search is present' , async ( ) => {
0 commit comments