Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 25, 2023
1 parent a4c0673 commit 2dd3d95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
60 changes: 54 additions & 6 deletions packages/browser/src/core/buffer/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,49 @@ import { Context } from '../../context'
import { sleep } from '../../../lib/sleep'
import { User } from '../../user'
import { getBufferedPageCtxFixture } from '../../../test-helpers/fixtures'
import * as GlobalAnalytics from '../../../lib/global-analytics-helper'

describe('PreInitMethodCallBuffer', () => {
describe(PreInitMethodCallBuffer, () => {
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', () => {
it('should convert the map back to an array', async () => {
it('should convert the map back to an array', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const call2 = new PreInitMethodCall('identify', [], jest.fn())
const call3 = new PreInitMethodCall('group', [], jest.fn())
const buffer = new PreInitMethodCallBuffer(call1, call2, call3)
expect(buffer.toArray()).toEqual([call1, call2, call3])
})

it('should also read from global analytics buffer', () => {
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
;(window as any).analytics = [['track', 'foo']]

const buffer = new PreInitMethodCallBuffer(call1)
expect(buffer.toArray()).toEqual([
call1,
expect.objectContaining<Partial<PreInitMethodCall>>({
method: 'track',
args: ['foo', getBufferedPageCtxFixture()],
}),
])
})
})

describe('push', () => {
it('should add method calls', async () => {
it('should add method calls', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const buffer = new PreInitMethodCallBuffer()
buffer.push(call1)
expect(buffer.toArray()).toEqual([call1])
})

it('should work if the calls were added at different times or in different ways', async () => {
it('should work if the calls were added at different times or in different ways', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const call2 = new PreInitMethodCall('identify', [], jest.fn())
const call3 = new PreInitMethodCall('group', [], jest.fn())
Expand All @@ -57,20 +74,51 @@ describe('PreInitMethodCallBuffer', () => {
expect(buffer.getCalls('identify')).toEqual([call1, call2])
expect(buffer.getCalls('group')).toEqual([call3])
})
it('should also read from global analytics buffer', () => {
const call1 = new PreInitMethodCall('identify', ['foo'], jest.fn())
GlobalAnalytics.setGlobalAnalytics([['identify', 'bar']] as any)

const buffer = new PreInitMethodCallBuffer(call1)
expect(buffer.getCalls('identify')).toEqual(
expect.arrayContaining([
call1,
expect.objectContaining<Partial<PreInitMethodCall>>({
method: 'identify',
args: ['bar', getBufferedPageCtxFixture()],
}),
])
)
})
})
describe('clear', () => {
it('should clear calls', async () => {
it('should clear calls', () => {
const call1 = new PreInitMethodCall('identify', [], jest.fn())
const call2 = new PreInitMethodCall('identify', [], jest.fn())
const call3 = new PreInitMethodCall('group', [], jest.fn())
GlobalAnalytics.setGlobalAnalytics([['track', 'bar']] as any)
const buffer = new PreInitMethodCallBuffer(call1, call2, call3)
buffer.clear()
expect(buffer.toArray()).toEqual([])
expect(GlobalAnalytics.getGlobalAnalytics()).toEqual([])
})
})

describe('snippet buffer', () => {
it('should read from a dynamic analytics key', () => {
const getGlobalAnalyticsSpy = jest.spyOn(
GlobalAnalytics,
'getGlobalAnalytics'
)

const buffer = new PreInitMethodCallBuffer()
expect(getGlobalAnalyticsSpy).not.toBeCalled()
buffer.toArray()
expect(getGlobalAnalyticsSpy).toBeCalled()
})
})
})

describe('AnalyticsBuffered', () => {
describe(AnalyticsBuffered, () => {
describe('Happy path', () => {
it('should return a promise-like object', async () => {
const ajs = new Analytics({ writeKey: 'foo' })
Expand Down
3 changes: 3 additions & 0 deletions packages/browser/src/core/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export class PreInitMethodCallBuffer {
}

clear(): void {
// clear calls in the global snippet buffered array.
this._pushSnippetWindowBuffer()
// clear calls in this instance
this._calls = {}
}

Expand Down

0 comments on commit 2dd3d95

Please sign in to comment.