@@ -9,6 +9,58 @@ import { Analytics } from '../analytics'
99import { Context } from '../core/context'
1010import { 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+
1264describe ( '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 ( )
0 commit comments