@@ -9,6 +9,58 @@ 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 ( '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
+
12
64
describe ( 'AnalyticsBuffered' , ( ) => {
13
65
describe ( 'Happy path' , ( ) => {
14
66
it ( 'should return a promise-like object' , async ( ) => {
@@ -177,7 +229,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
177
229
reject : jest . fn ( ) ,
178
230
} as PreInitMethodCall < any >
179
231
180
- const buffer = new PreInitMethodCallBuffer ( [ synchronousMethod , asyncMethod ] )
232
+ const buffer = new PreInitMethodCallBuffer ( ) . push (
233
+ synchronousMethod ,
234
+ asyncMethod
235
+ )
181
236
182
237
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
183
238
expect ( synchronousMethod . resolve ) . not . toBeCalled ( )
@@ -199,7 +254,7 @@ describe('flushAnalyticsCallsInNewTask', () => {
199
254
reject : jest . fn ( ) ,
200
255
} as PreInitMethodCall < any >
201
256
202
- const buffer = new PreInitMethodCallBuffer ( [ asyncMethod ] )
257
+ const buffer = new PreInitMethodCallBuffer ( ) . push ( asyncMethod )
203
258
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
204
259
await sleep ( 0 )
205
260
expect ( asyncMethod . reject ) . toBeCalledWith ( 'oops!' )
@@ -230,7 +285,10 @@ describe('flushAnalyticsCallsInNewTask', () => {
230
285
reject : jest . fn ( ) ,
231
286
} as PreInitMethodCall < any >
232
287
233
- const buffer = new PreInitMethodCallBuffer ( [ synchronousMethod , asyncMethod ] )
288
+ const buffer = new PreInitMethodCallBuffer ( ) . push (
289
+ synchronousMethod ,
290
+ asyncMethod
291
+ )
234
292
flushAnalyticsCallsInNewTask ( new Analytics ( { writeKey : 'abc' } ) , buffer )
235
293
await sleep ( 0 )
236
294
expect ( synchronousMethod . reject ) . toBeCalled ( )
0 commit comments