1
1
import { JSDOM } from 'jsdom'
2
2
import { Analytics } from '../../core/analytics'
3
- // @ts -ignore loadCDNSettings mocked dependency is accused as unused
4
3
import { AnalyticsBrowser } from '..'
5
4
import { setGlobalCDNUrl } from '../../lib/parse-cdn'
6
5
import { 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
+ } )
7
21
8
22
const writeKey = TEST_WRITEKEY
9
23
10
24
describe ( 'queryString' , ( ) => {
11
25
let jsd : JSDOM
12
26
13
27
beforeEach ( async ( ) => {
14
- jest . restoreAllMocks ( )
15
- jest . resetAllMocks ( )
16
-
17
28
const html = `
18
29
<!DOCTYPE html>
19
30
<head>
@@ -37,33 +48,41 @@ describe('queryString', () => {
37
48
setGlobalCDNUrl ( undefined as any )
38
49
} )
39
50
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' )
55
53
56
54
jsd . reconfigure ( {
57
55
url : 'https://localhost/?ajs_aid=123' ,
58
56
} )
59
57
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
65
63
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
+ )
67
86
} )
68
87
69
88
it ( 'applies query string logic if window.location.search is present' , async ( ) => {
0 commit comments