Skip to content

Commit 1e1db10

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

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-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: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,40 @@ 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('getCalls', () => {
21+
it('should return calls', async () => {
22+
const buffer = new PreInitMethodCallBuffer()
23+
24+
const fooCall1 = {
25+
method: 'foo',
26+
args: ['bar'],
27+
} as any
28+
29+
const barCall = {
30+
method: 'bar',
31+
args: ['foobar'],
32+
} as any
33+
34+
const fooCall2 = {
35+
method: 'foo',
36+
args: ['baz'],
37+
} as any
38+
39+
const calls: PreInitMethodCall<any>[] = [fooCall1, fooCall2, barCall]
40+
const result = buffer.push(...calls)
41+
expect(result.getCalls('foo' as any)).toEqual([fooCall1, fooCall2])
42+
})
43+
})
44+
})
45+
1246
describe('AnalyticsBuffered', () => {
1347
describe('Happy path', () => {
1448
it('should return a promise-like object', async () => {
@@ -177,7 +211,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
177211
reject: jest.fn(),
178212
} as PreInitMethodCall<any>
179213

180-
const buffer = new PreInitMethodCallBuffer([synchronousMethod, asyncMethod])
214+
const buffer = new PreInitMethodCallBuffer().push(
215+
synchronousMethod,
216+
asyncMethod
217+
)
181218

182219
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
183220
expect(synchronousMethod.resolve).not.toBeCalled()
@@ -199,7 +236,7 @@ describe('flushAnalyticsCallsInNewTask', () => {
199236
reject: jest.fn(),
200237
} as PreInitMethodCall<any>
201238

202-
const buffer = new PreInitMethodCallBuffer([asyncMethod])
239+
const buffer = new PreInitMethodCallBuffer().push(asyncMethod)
203240
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
204241
await sleep(0)
205242
expect(asyncMethod.reject).toBeCalledWith('oops!')
@@ -230,7 +267,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
230267
reject: jest.fn(),
231268
} as PreInitMethodCall<any>
232269

233-
const buffer = new PreInitMethodCallBuffer([synchronousMethod, asyncMethod])
270+
const buffer = new PreInitMethodCallBuffer().push(
271+
synchronousMethod,
272+
asyncMethod
273+
)
234274
flushAnalyticsCallsInNewTask(new Analytics({ writeKey: 'abc' }), buffer)
235275
await sleep(0)
236276
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)