@@ -2,6 +2,7 @@ import { IKitConfigs } from "../../src/configAPIClient";
22import { IMParticleUser } from "../../src/identity-user-interfaces" ;
33import { SDKIdentityApi } from "../../src/identity.interfaces" ;
44import { IMParticleWebSDKInstance } from "../../src/mp-instance" ;
5+ import { IPreInit } from "../../src/pre-init-utils" ;
56import RoktManager , { IRoktKit , IRoktSelectPlacementsOptions } from "../../src/roktManager" ;
67import { testMPID } from '../src/config/constants' ;
78
@@ -10,6 +11,7 @@ const resolvePromise = () => new Promise(resolve => setTimeout(resolve, 0));
1011describe ( 'RoktManager' , ( ) => {
1112 let roktManager : RoktManager ;
1213 let currentUser : IMParticleUser ;
14+ let preInit : IPreInit ;
1315
1416 const mockMPInstance = ( {
1517 Identity : {
@@ -42,7 +44,12 @@ describe('RoktManager', () => {
4244 } as unknown ) as IMParticleWebSDKInstance ;
4345
4446 beforeEach ( ( ) => {
45- roktManager = new RoktManager ( ) ;
47+ preInit = {
48+ readyQueue : [ ] ,
49+ integrationDelays : { } ,
50+ forwarderConstructors : [ ] ,
51+ } ;
52+ roktManager = new RoktManager ( preInit ) ;
4653 currentUser = {
4754 setUserAttributes : jest . fn ( ) ,
4855 getUserIdentities : jest . fn ( ) . mockReturnValue ( {
@@ -440,8 +447,10 @@ describe('RoktManager', () => {
440447 } ) ;
441448
442449 describe ( '#attachKit' , ( ) => {
443- it ( 'should attach a kit' , ( ) => {
444- const kit : IRoktKit = {
450+ let kit : IRoktKit ;
451+
452+ beforeEach ( ( ) => {
453+ kit = {
445454 launcher : {
446455 selectPlacements : jest . fn ( ) ,
447456 hashAttributes : jest . fn ( ) ,
@@ -455,10 +464,77 @@ describe('RoktManager', () => {
455464 setExtensionData : jest . fn ( ) ,
456465 use : jest . fn ( ) ,
457466 } ;
467+ } ) ;
458468
469+ it ( 'should attach a kit' , ( ) => {
459470 roktManager . attachKit ( kit ) ;
460471 expect ( roktManager [ 'kit' ] ) . not . toBeNull ( ) ;
461472 } ) ;
473+
474+ it ( 'should call processMessageQueue when kit is attached' , ( ) => {
475+ const processMessageQueueSpy = jest . spyOn ( roktManager as any , 'processMessageQueue' ) ;
476+
477+ roktManager . attachKit ( kit ) ;
478+
479+ expect ( processMessageQueueSpy ) . toHaveBeenCalledTimes ( 1 ) ;
480+ processMessageQueueSpy . mockRestore ( ) ;
481+ } ) ;
482+
483+ it ( 'should process ready queue when kit is attached and identity call failed' , ( ) => {
484+ const readyQueueItem1 = jest . fn ( ) ;
485+ const readyQueueItem2 = jest . fn ( ) ;
486+ preInit . readyQueue = [ readyQueueItem1 , readyQueueItem2 ] ;
487+
488+ // Set identityCallFailed to true
489+ roktManager [ 'store' ] . identityCallFailed = true ;
490+
491+ roktManager . attachKit ( kit ) ;
492+
493+ expect ( readyQueueItem1 ) . toHaveBeenCalledTimes ( 1 ) ;
494+ expect ( readyQueueItem2 ) . toHaveBeenCalledTimes ( 1 ) ;
495+ expect ( preInit . readyQueue ) . toEqual ( [ ] ) ;
496+ } ) ;
497+
498+ it ( 'should NOT process ready queue when identity call did not fail' , ( ) => {
499+ const readyQueueItem1 = jest . fn ( ) ;
500+ const readyQueueItem2 = jest . fn ( ) ;
501+ preInit . readyQueue = [ readyQueueItem1 , readyQueueItem2 ] ;
502+
503+ // Set identityCallFailed to false
504+ roktManager [ 'store' ] . identityCallFailed = false ;
505+
506+ roktManager . attachKit ( kit ) ;
507+
508+ // Ready queue items should NOT be called
509+ expect ( readyQueueItem1 ) . not . toHaveBeenCalled ( ) ;
510+ expect ( readyQueueItem2 ) . not . toHaveBeenCalled ( ) ;
511+ // Ready queue should remain unchanged
512+ expect ( preInit . readyQueue ) . toEqual ( [ readyQueueItem1 , readyQueueItem2 ] ) ;
513+ } ) ;
514+
515+ it ( 'should process message queue before processing ready queue when identity call failed' , ( ) => {
516+ const processMessageQueueSpy = jest . spyOn ( roktManager as any , 'processMessageQueue' ) ;
517+ const processReadyQueueSpy = jest . spyOn ( require ( '../../src/pre-init-utils' ) , 'processReadyQueue' ) ;
518+
519+ // Set identityCallFailed to true so ready queue is processed
520+ roktManager [ 'store' ] . identityCallFailed = true ;
521+
522+ roktManager . attachKit ( kit ) ;
523+
524+ // Verify both were called
525+ expect ( processMessageQueueSpy ) . toHaveBeenCalled ( ) ;
526+ expect ( processReadyQueueSpy ) . toHaveBeenCalled ( ) ;
527+
528+ // Verify processMessageQueue was called before processReadyQueue
529+ // by checking the order of calls
530+ const messageQueueCallIndex = processMessageQueueSpy . mock . invocationCallOrder [ 0 ] ;
531+ const readyQueueCallIndex = processReadyQueueSpy . mock . invocationCallOrder [ 0 ] ;
532+
533+ expect ( messageQueueCallIndex ) . toBeLessThan ( readyQueueCallIndex ) ;
534+
535+ processMessageQueueSpy . mockRestore ( ) ;
536+ processReadyQueueSpy . mockRestore ( ) ;
537+ } ) ;
462538 } ) ;
463539
464540 describe ( '#processMessageQueue' , ( ) => {
0 commit comments