Skip to content

Commit f785c64

Browse files
committed
update pre-init buffer, add tests
1 parent e77db68 commit f785c64

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

src/__tests__/analytics-pre-init.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ describe('Pre-initialization', () => {
318318
expect(onIdentifyCb).toHaveBeenCalledWith('bar', {}, undefined)
319319
})
320320

321-
test('the "this" value of event callbacks should be Analytics', async () => {
321+
test('the "this" value of "emitted" event callbacks should be Analytics', async () => {
322322
const ajsBuffered = AnalyticsBrowser.load({ writeKey })
323323
ajsBuffered.on('track', function onTrackCb(this: any) {
324324
expect(this).toBeInstanceOf(Analytics)

src/__tests__/analytics-pre-init.test.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,58 @@ import { Analytics } from '../analytics'
99
import { Context } from '../core/context'
1010
import { sleep } from './test-helpers/sleep'
1111

12+
describe('PreInitMethodCallBuffer', () => {
13+
describe('push', () => {
14+
it('should return this', async () => {
15+
const buffer = new PreInitMethodCallBuffer()
16+
const result = buffer.push({} as any)
17+
expect(result).toBeInstanceOf(PreInitMethodCallBuffer)
18+
})
19+
})
20+
describe('toArray should return an array', () => {
21+
it('toArray() should convert the map back to an array', async () => {
22+
const buffer = new PreInitMethodCallBuffer()
23+
const method1 = { method: 'foo' } as any
24+
const method2 = { method: 'foo', args: [1] } as any
25+
const method3 = { method: 'bar' } as any
26+
buffer.push(method1, method2, method3)
27+
expect(buffer.toArray()).toEqual([method1, method2, method3])
28+
})
29+
})
30+
31+
describe('clear', () => {
32+
it('should return this', async () => {
33+
const buffer = new PreInitMethodCallBuffer()
34+
const result = buffer.push({} as any)
35+
expect(result).toBeInstanceOf(PreInitMethodCallBuffer)
36+
})
37+
})
38+
describe('getCalls', () => {
39+
it('should return calls', async () => {
40+
const buffer = new PreInitMethodCallBuffer()
41+
42+
const fooCall1 = {
43+
method: 'foo',
44+
args: ['bar'],
45+
} as any
46+
47+
const barCall = {
48+
method: 'bar',
49+
args: ['foobar'],
50+
} as any
51+
52+
const fooCall2 = {
53+
method: 'foo',
54+
args: ['baz'],
55+
} as any
56+
57+
const calls: PreInitMethodCall<any>[] = [fooCall1, fooCall2, barCall]
58+
const result = buffer.push(...calls)
59+
expect(result.getCalls('foo' as any)).toEqual([fooCall1, fooCall2])
60+
})
61+
})
62+
})
63+
1264
describe('AnalyticsBuffered', () => {
1365
describe('Happy path', () => {
1466
it('should return a promise-like object', async () => {
@@ -177,7 +229,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
177229
reject: jest.fn(),
178230
} as PreInitMethodCall<any>
179231

180-
const buffer = new PreInitMethodCallBuffer([synchronousMethod, asyncMethod])
232+
const buffer = new PreInitMethodCallBuffer().push(
233+
synchronousMethod,
234+
asyncMethod
235+
)
181236

182237
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
183238
expect(synchronousMethod.resolve).not.toBeCalled()
@@ -199,7 +254,7 @@ describe('flushAnalyticsCallsInNewTask', () => {
199254
reject: jest.fn(),
200255
} as PreInitMethodCall<any>
201256

202-
const buffer = new PreInitMethodCallBuffer([asyncMethod])
257+
const buffer = new PreInitMethodCallBuffer().push(asyncMethod)
203258
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
204259
await sleep(0)
205260
expect(asyncMethod.reject).toBeCalledWith('oops!')
@@ -230,7 +285,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
230285
reject: jest.fn(),
231286
} as PreInitMethodCall<any>
232287

233-
const buffer = new PreInitMethodCallBuffer([synchronousMethod, asyncMethod])
288+
const buffer = new PreInitMethodCallBuffer().push(
289+
synchronousMethod,
290+
asyncMethod
291+
)
234292
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
235293
await sleep(0)
236294
expect(synchronousMethod.reject).toBeCalled()

src/analytics-pre-init.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ type MethodCallMap = Partial<Record<PreInitMethodName, PreInitMethodCall[]>>
141141
* Represents any and all the buffered method calls that occurred before initialization.
142142
*/
143143
export class PreInitMethodCallBuffer {
144-
constructor(calls?: PreInitMethodCall[]) {
145-
if (calls) {
146-
this.push(...calls)
147-
}
148-
}
149-
150144
private _value = {} as MethodCallMap
151145

152146
public toArray(): PreInitMethodCall[] {
@@ -161,18 +155,20 @@ export class PreInitMethodCallBuffer {
161155
return (this._value[methodName] ?? []) as PreInitMethodCall<T>[]
162156
}
163157

164-
push(...calls: PreInitMethodCall[]): void {
158+
push(...calls: PreInitMethodCall[]): PreInitMethodCallBuffer {
165159
calls.forEach((el) => {
166160
if (this._value[el.method]) {
167161
this._value[el.method]?.push(el)
168162
} else {
169163
this._value[el.method] = [el]
170164
}
171165
})
166+
return this
172167
}
173168

174-
clear(): void {
169+
clear(): PreInitMethodCallBuffer {
175170
this._value = {} as MethodCallMap
171+
return this
176172
}
177173
}
178174

0 commit comments

Comments
 (0)