@@ -9,6 +9,40 @@ import { Analytics } from '../analytics'
9
9
import { Context } from '../core/context'
10
10
import { sleep } from './test-helpers/sleep'
11
11
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
+
12
46
describe ( 'AnalyticsBuffered' , ( ) => {
13
47
describe ( 'Happy path' , ( ) => {
14
48
it ( 'should return a promise-like object' , async ( ) => {
@@ -177,7 +211,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
177
211
reject : jest . fn ( ) ,
178
212
} as PreInitMethodCall < any >
179
213
180
- const buffer = new PreInitMethodCallBuffer ( [ synchronousMethod , asyncMethod ] )
214
+ const buffer = new PreInitMethodCallBuffer ( ) . push (
215
+ synchronousMethod ,
216
+ asyncMethod
217
+ )
181
218
182
219
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
183
220
expect ( synchronousMethod . resolve ) . not . toBeCalled ( )
@@ -199,7 +236,7 @@ describe('flushAnalyticsCallsInNewTask', () => {
199
236
reject : jest . fn ( ) ,
200
237
} as PreInitMethodCall < any >
201
238
202
- const buffer = new PreInitMethodCallBuffer ( [ asyncMethod ] )
239
+ const buffer = new PreInitMethodCallBuffer ( ) . push ( asyncMethod )
203
240
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
204
241
await sleep ( 0 )
205
242
expect ( asyncMethod . reject ) . toBeCalledWith ( 'oops!' )
@@ -230,7 +267,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
230
267
reject : jest . fn ( ) ,
231
268
} as PreInitMethodCall < any >
232
269
233
- const buffer = new PreInitMethodCallBuffer ( [ synchronousMethod , asyncMethod ] )
270
+ const buffer = new PreInitMethodCallBuffer ( ) . push (
271
+ synchronousMethod ,
272
+ asyncMethod
273
+ )
234
274
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
235
275
await sleep ( 0 )
236
276
expect ( synchronousMethod . reject ) . toBeCalled ( )
0 commit comments