@@ -9,6 +9,10 @@ import { Analytics } from '../analytics'
9
9
import { Context } from '../core/context'
10
10
import { sleep } from './test-helpers/sleep'
11
11
12
+ afterEach ( ( ) => {
13
+ jest . clearAllMocks ( )
14
+ } )
15
+
12
16
describe ( 'PreInitMethodCallBuffer' , ( ) => {
13
17
describe ( 'push' , ( ) => {
14
18
it ( 'should return this' , async ( ) => {
@@ -75,6 +79,19 @@ describe('AnalyticsBuffered', () => {
75
79
expect ( typeof buffered . finally ) . toBe ( 'function' )
76
80
} )
77
81
82
+ it ( 'should have instance and ctx properties defined when loader is done' , async ( ) => {
83
+ const ajs = new Analytics ( { writeKey : 'foo' } )
84
+ const ctx = new Context ( { type : 'track' } )
85
+ const buffered = new AnalyticsBuffered ( ( ) =>
86
+ Promise . resolve < [ Analytics , Context ] > ( [ ajs , ctx ] )
87
+ )
88
+ expect ( buffered . instance ) . not . toBeDefined ( )
89
+ expect ( buffered . ctx ) . not . toBeDefined ( )
90
+ await buffered // finish loading
91
+ expect ( buffered . instance ) . toBe ( ajs )
92
+ expect ( buffered . ctx ) . toBe ( ctx )
93
+ } )
94
+
78
95
it ( 'should convert to a promise on await' , async ( ) => {
79
96
const ajs = new Analytics ( { writeKey : 'foo' } )
80
97
const ctx = new Context ( { type : 'track' } )
@@ -85,6 +102,20 @@ describe('AnalyticsBuffered', () => {
85
102
expect ( analytics ) . toEqual ( ajs )
86
103
expect ( context ) . toEqual ( ctx )
87
104
} )
105
+
106
+ it ( 'should set the "this" value of any proxied analytics methods to the analytics instance' , async ( ) => {
107
+ const ajs = new Analytics ( { writeKey : 'foo' } )
108
+ jest . spyOn ( ajs , 'track' ) . mockImplementation ( function ( this : Analytics ) {
109
+ expect ( this ) . toBe ( ajs )
110
+ return Promise . resolve ( ctx )
111
+ } )
112
+ const ctx = new Context ( { type : 'track' } )
113
+ const result : [ Analytics , Context ] = [ ajs , ctx ]
114
+ const buffered = new AnalyticsBuffered ( ( ) => Promise . resolve ( result ) )
115
+ await buffered // finish loading
116
+ void buffered . track ( 'foo' , { } )
117
+ expect . assertions ( 1 )
118
+ } )
88
119
} )
89
120
90
121
describe ( 'Unhappy path' , ( ) => {
@@ -144,10 +175,6 @@ describe('callAnalyticsMethod', () => {
144
175
writeKey : 'abc' ,
145
176
} )
146
177
} )
147
- afterEach ( ( ) => {
148
- jest . clearAllMocks ( )
149
- } )
150
-
151
178
it ( 'should change called to true' , async ( ) => {
152
179
methodCall . called = false
153
180
await callAnalyticsMethod ( ajs , methodCall )
0 commit comments