Skip to content

Commit 5044dbc

Browse files
authored
Add / improve offline integration tests (#729)
1 parent 0080fa3 commit 5044dbc

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ import jar from 'js-cookie'
1616
import { PriorityQueue } from '../../lib/priority-queue'
1717
import { getCDN, setGlobalCDNUrl } from '../../lib/parse-cdn'
1818
import { clearAjsBrowserStorage } from '../../test-helpers/browser-storage'
19+
import { parseFetchCall } from '../../test-helpers/fetch-parse'
1920
import { ActionDestination } from '@/plugins/remote-loader'
2021

21-
let fetchCalls: Array<any>[] = []
22+
let fetchCalls: ReturnType<typeof parseFetchCall>[] = []
2223

2324
jest.mock('unfetch', () => {
2425
return {
2526
__esModule: true,
2627
default: (url: RequestInfo, body?: RequestInit) => {
27-
fetchCalls.push([url, body])
28+
const call = parseFetchCall([url, body])
29+
fetchCalls.push(call)
2830
return createMockFetchImplementation(cdnSettingsKitchenSink)(url, body)
2931
},
3032
}
@@ -200,7 +202,7 @@ describe('Initialization', () => {
200202
],
201203
})
202204

203-
expect(fetchCalls[0][0]).toContain(overriddenCDNUrl)
205+
expect(fetchCalls[0].url).toContain(overriddenCDNUrl)
204206
expect.assertions(3)
205207
})
206208
})
@@ -265,7 +267,7 @@ describe('Initialization', () => {
265267
})
266268

267269
expect(fetchCalls.length).toBeGreaterThan(0)
268-
expect(fetchCalls[0][0]).toMatch(/\/settings$/)
270+
expect(fetchCalls[0].url).toMatch(/\/settings$/)
269271
})
270272

271273
it('does not fetch source settings if cdnSettings is set', async () => {
@@ -897,26 +899,56 @@ describe('retries', () => {
897899
expect(ajs.queue.queue.getAttempts(fruitBasketEvent)).toEqual(2)
898900
})
899901

900-
it('does not queue up events when offline if retryQueue setting is set to false', async () => {
901-
const [ajs] = await AnalyticsBrowser.load({ writeKey })
902+
it('does not queue events / dispatch when offline if retryQueue setting is set to false', async () => {
903+
const [ajs] = await AnalyticsBrowser.load(
904+
{ writeKey },
905+
{ retryQueue: false }
906+
)
902907

908+
const trackSpy = jest.fn().mockImplementation((ctx) => ctx)
903909
await ajs.queue.register(
904910
Context.system(),
905911
{
906912
...testPlugin,
907913
ready: () => Promise.resolve(true),
908-
track: (ctx) => ctx,
914+
track: trackSpy,
909915
},
910916
ajs
911917
)
912918

913919
// @ts-ignore ignore reassining function
914920
isOffline = jest.fn().mockReturnValue(true)
915921

916-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
917-
ajs.track('event')
922+
await ajs.track('event')
918923

919-
expect(ajs.queue.queue.length).toBe(0)
924+
expect(trackSpy).toBeCalledTimes(0)
925+
})
926+
927+
it('enqueues events / dispatches if the client is currently offline and retries are *enabled* for the main event queue', async () => {
928+
const [ajs] = await AnalyticsBrowser.load(
929+
{ writeKey },
930+
{ retryQueue: true }
931+
)
932+
933+
const trackSpy = jest.fn().mockImplementation((ctx) => ctx)
934+
await ajs.queue.register(
935+
Context.system(),
936+
{
937+
...testPlugin,
938+
ready: () => Promise.resolve(true),
939+
track: trackSpy,
940+
},
941+
ajs
942+
)
943+
944+
// @ts-ignore ignore reassining function
945+
isOffline = jest.fn().mockReturnValue(true)
946+
947+
expect(trackSpy).toBeCalledTimes(0)
948+
949+
await ajs.track('event')
950+
951+
expect(trackSpy).toBeCalledTimes(1)
920952
})
921953
})
922954

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type FetchCall = [input: RequestInfo, init?: RequestInit | undefined]
2+
3+
export const parseFetchCall = ([url, request]: FetchCall) => ({
4+
url,
5+
method: request?.method,
6+
headers: request?.headers,
7+
body: request?.body ? JSON.parse(request.body as any) : undefined,
8+
})

0 commit comments

Comments
 (0)