Skip to content

Commit 80098fe

Browse files
committed
add tests too
1 parent fbd963f commit 80098fe

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

packages/browser/src/browser/__tests__/query-string.integration.test.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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 () => {

packages/browser/src/browser/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,10 @@ function flushPreBuffer(
208208
*/
209209
async function flushFinalBuffer(
210210
analytics: Analytics,
211+
queryString: string,
211212
buffer: PreInitMethodCallBuffer
212213
): Promise<void> {
213-
// Call popSnippetWindowBuffer before each flush task since there may be
214-
// analytics calls during async function calls.
215-
await flushAddSourceMiddleware(analytics, buffer)
214+
await flushQueryString(analytics, queryString)
216215
flushAnalyticsCallsInNewTask(analytics, buffer)
217216
}
218217

@@ -355,6 +354,9 @@ async function registerPlugins(
355354
})
356355
}
357356

357+
// register any source middleware that was added before initialization. Could probably be added to the buffer, but it's a bit of a special case.
358+
await flushAddSourceMiddleware(analytics, preInitBuffer)
359+
358360
return ctx
359361
}
360362

@@ -435,11 +437,7 @@ async function loadAnalytics(
435437

436438
analytics.initialized = true
437439
analytics.emit('initialize', settings, options)
438-
439-
await flushFinalBuffer(analytics, preInitBuffer)
440-
441-
// not sure why we need to await this, but it was legacy -- can probably be removed
442-
await flushQueryString(analytics, queryString)
440+
await flushFinalBuffer(analytics, queryString, preInitBuffer)
443441

444442
return [analytics, ctx]
445443
}

0 commit comments

Comments
 (0)