Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 26, 2023
1 parent f24219d commit b68f94e
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions packages/browser/src/core/buffer/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PreInitMethodCall,
flushAnalyticsCallsInNewTask,
PreInitMethodCallBuffer,
PreInitMethodName,
} from '..'
import { Analytics } from '../../analytics'
import { Context } from '../../context'
Expand All @@ -16,14 +17,8 @@ describe(PreInitMethodCallBuffer, () => {
beforeEach(() => {
GlobalAnalytics.setGlobalAnalytics(undefined as any)
})
describe('page context', () => {
it('should append page context to arguments', () => {
const call = new PreInitMethodCall('identify', ['foo'], jest.fn())
expect(call.args).toEqual(['foo', getBufferedPageCtxFixture()])
})
})

describe('toArray', () => {
describe('toArray()', () => {
it('should convert the map back to an array', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const call2 = new PreInitMethodCall('identify', [], jest.fn())
Expand All @@ -49,7 +44,7 @@ describe(PreInitMethodCallBuffer, () => {
})
})

describe('push', () => {
describe('push()', () => {
it('should add method calls', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const buffer = new PreInitMethodCallBuffer()
Expand All @@ -69,7 +64,7 @@ describe(PreInitMethodCallBuffer, () => {
})
})

describe('getCalls', () => {
describe('getCalls()', () => {
it('should fetch calls by name', async () => {
const buffer = new PreInitMethodCallBuffer()
const call1 = new PreInitMethodCall('identify', [], jest.fn())
Expand All @@ -79,7 +74,7 @@ describe(PreInitMethodCallBuffer, () => {
expect(buffer.getCalls('identify')).toEqual([call1, call2])
expect(buffer.getCalls('group')).toEqual([call3])
})
it('should also read from global analytics buffer', () => {
it('should read from Snippet Buffer', () => {
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
GlobalAnalytics.setGlobalAnalytics([['identify', 'snippet']] as any)

Expand All @@ -95,7 +90,7 @@ describe(PreInitMethodCallBuffer, () => {
expect(calls[1]).toEqual(call1)
})
})
describe('clear', () => {
describe('clear()', () => {
it('should clear calls', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const call2 = new PreInitMethodCall('identify', [], jest.fn())
Expand All @@ -108,8 +103,8 @@ describe(PreInitMethodCallBuffer, () => {
})
})

describe('snippet buffer', () => {
it('should read from the global analytics instance', () => {
describe('Snippet buffer (method calls)', () => {
it('should be read from the global analytics instance', () => {
const getGlobalAnalyticsSpy = jest.spyOn(
GlobalAnalytics,
'getGlobalAnalytics'
Expand All @@ -121,6 +116,26 @@ describe(PreInitMethodCallBuffer, () => {
expect(getGlobalAnalyticsSpy).toBeCalled()
})
})
describe('BufferedPageContext', () => {
test.each([
'track',
'screen',
'alias',
'group',
'page',
'identify',
] as PreInitMethodName[])('should be appended to %p calls.', (method) => {
const call = new PreInitMethodCall(method, ['foo'], jest.fn())
expect(call.args).toEqual(['foo', getBufferedPageCtxFixture()])
})
it('should not be appended for other method calls', () => {
const fn = jest.fn()
const onCall = new PreInitMethodCall('on', ['foo', fn])
expect(onCall.args).toEqual(['foo', fn])
const setAnonIdCall = new PreInitMethodCall('setAnonymousId', [])
expect(setAnonIdCall.args).toEqual([])
})
})
})

describe(AnalyticsBuffered, () => {
Expand Down

0 comments on commit b68f94e

Please sign in to comment.