@@ -27,6 +27,9 @@ describe('Pre-initialization', () => {
2727 const trackSpy = jest . spyOn ( Analytics . prototype , 'track' )
2828 const identifySpy = jest . spyOn ( Analytics . prototype , 'identify' )
2929 const onSpy = jest . spyOn ( Analytics . prototype , 'on' )
30+ const getOnSpyCalls = ( event : string ) =>
31+ onSpy . mock . calls . filter ( ( [ arg1 ] ) => arg1 === event )
32+
3033 const readySpy = jest . spyOn ( Analytics . prototype , 'ready' )
3134 const browserLoadSpy = jest . spyOn ( AnalyticsBrowser , 'load' )
3235 const consoleErrorSpy = jest . spyOn ( console , 'error' )
@@ -236,11 +239,9 @@ describe('Pre-initialization', () => {
236239 expect ( trackSpy ) . toBeCalledWith ( 'bar' )
237240 expect ( trackSpy ) . toBeCalledTimes ( 2 )
238241
239- expect ( identifySpy ) . toBeCalledWith ( )
240242 expect ( identifySpy ) . toBeCalledTimes ( 1 )
241243
242- expect ( onSpy ) . toBeCalledTimes ( 1 )
243-
244+ expect ( getOnSpyCalls ( 'track' ) . length ) . toBe ( 1 )
244245 expect ( onTrackCb ) . toBeCalledTimes ( 2 ) // gets called once for each track event
245246 expect ( onTrackCb ) . toBeCalledWith ( 'foo' , { } , undefined )
246247 expect ( onTrackCb ) . toBeCalledWith ( 'bar' , { } , undefined )
@@ -269,9 +270,10 @@ describe('Pre-initialization', () => {
269270 expect ( identifySpy ) . toBeCalledWith ( )
270271 expect ( identifySpy ) . toBeCalledTimes ( 1 )
271272 expect ( consoleErrorSpy ) . toBeCalledTimes ( 1 )
273+
272274 expect ( consoleErrorSpy ) . toBeCalledWith ( 'identity rejection' )
273275
274- expect ( onSpy ) . toBeCalledTimes ( 1 )
276+ expect ( getOnSpyCalls ( 'track' ) . length ) . toBe ( 1 )
275277
276278 expect ( onTrackCb ) . toBeCalledTimes ( 2 ) // gets called once for each track event
277279 expect ( onTrackCb ) . toBeCalledWith ( 'foo' , { } , undefined )
@@ -288,7 +290,7 @@ describe('Pre-initialization', () => {
288290
289291 await ajsBrowser
290292 expect ( onSpy ) . toBeCalledWith ( ...args )
291- expect ( onSpy ) . toHaveBeenCalledTimes ( 1 )
293+ expect ( getOnSpyCalls ( 'track' ) . length ) . toBe ( 1 )
292294 } )
293295
294296 test ( 'If, before initialization .on("track") is called and then .track is called, the callback method should be called after analytics loads' , async ( ) => {
@@ -302,10 +304,10 @@ describe('Pre-initialization', () => {
302304 await Promise . all ( [ analytics , trackCtxPromise ] )
303305
304306 expect ( onSpy ) . toBeCalledWith ( 'track' , onFnCb )
305- expect ( onSpy ) . toHaveBeenCalledTimes ( 1 )
307+ expect ( getOnSpyCalls ( 'track' ) . length ) . toBe ( 1 )
306308
307309 expect ( onFnCb ) . toHaveBeenCalledWith ( 'foo' , { name : 123 } , undefined )
308- expect ( onFnCb ) . toHaveBeenCalledTimes ( 1 )
310+ expect ( onFnCb ) . toBeCalledTimes ( 1 )
309311 } )
310312
311313 test ( 'If, before initialization, .ready is called, the callback method should be called after analytics loads' , async ( ) => {
0 commit comments