@@ -449,6 +449,7 @@ describe('RoktManager', () => {
449449 } ) ;
450450
451451 it ( 'should process queued selectPlacements calls once the launcher and kit are attached' , async ( ) => {
452+ const expectedResult = { placements : [ 'placement1' , 'placement2' ] } ;
452453 const kit : IRoktKit = {
453454 launcher : {
454455 selectPlacements : jest . fn ( ) ,
@@ -457,7 +458,7 @@ describe('RoktManager', () => {
457458 filters : undefined ,
458459 filteredUser : undefined ,
459460 userAttributes : undefined ,
460- selectPlacements : jest . fn ( ) ,
461+ selectPlacements : jest . fn ( ) . mockResolvedValue ( expectedResult ) ,
461462 hashAttributes : jest . fn ( ) ,
462463 setExtensionData : jest . fn ( ) ,
463464 } ;
@@ -466,16 +467,27 @@ describe('RoktManager', () => {
466467 attributes : { }
467468 } as IRoktSelectPlacementsOptions ;
468469
469- roktManager . selectPlacements ( options ) ;
470+ // Call selectPlacements and get the promise (should be deferred since kit not ready)
471+ const selectionPromise = roktManager . selectPlacements ( options ) ;
472+
473+ // Verify the call was queued
470474 expect ( roktManager [ 'kit' ] ) . toBeNull ( ) ;
471475 expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 1 ) ;
472476 expect ( roktManager [ 'messageQueue' ] [ 0 ] . methodName ) . toBe ( 'selectPlacements' ) ;
473477 expect ( roktManager [ 'messageQueue' ] [ 0 ] . payload ) . toBe ( options ) ;
478+ expect ( roktManager [ 'messageQueue' ] [ 0 ] . messageId ) . toBeDefined ( ) ;
474479
480+ // Attach kit (should trigger processing of queued messages)
475481 roktManager . attachKit ( kit ) ;
482+
483+ // Verify kit was attached and queue was processed
476484 expect ( roktManager [ 'kit' ] ) . not . toBeNull ( ) ;
477485 expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 0 ) ;
478486 expect ( kit . selectPlacements ) . toHaveBeenCalledWith ( options ) ;
487+
488+ // Most importantly: verify the original promise resolves with the actual result
489+ const result = await selectionPromise ;
490+ expect ( result ) . toEqual ( expectedResult ) ;
479491 } ) ;
480492
481493 it ( 'should pass through the correct attributes to kit.selectPlacements' , ( ) => {
@@ -1027,4 +1039,120 @@ describe('RoktManager', () => {
10271039 } ) . toThrow ( 'Error setting extension data: ' + mockError . message ) ;
10281040 } ) ;
10291041 } ) ;
1042+
1043+ describe ( '#deferredCall' , ( ) => {
1044+ it ( 'should create a deferred promise with unique messageId' , ( ) => {
1045+ const testPayload = { test : 'data' } ;
1046+
1047+ // Call deferredCall
1048+ const promise = roktManager [ 'deferredCall' ] < string > ( 'testMethod' , testPayload ) ;
1049+
1050+ // Verify promise was created
1051+ expect ( promise ) . toBeInstanceOf ( Promise ) ;
1052+
1053+ // Verify message was queued with unique messageId
1054+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 1 ) ;
1055+ const queuedMessage = roktManager [ 'messageQueue' ] [ 0 ] ;
1056+ expect ( queuedMessage . methodName ) . toBe ( 'testMethod' ) ;
1057+ expect ( queuedMessage . payload ) . toBe ( testPayload ) ;
1058+ expect ( queuedMessage . messageId ) . toBeDefined ( ) ;
1059+ expect ( typeof queuedMessage . messageId ) . toBe ( 'string' ) ;
1060+
1061+ // Verify pending promise is tracked with that messageId
1062+ expect ( roktManager [ 'pendingPromises' ] . has ( queuedMessage . messageId ! ) ) . toBe ( true ) ;
1063+ } ) ;
1064+
1065+ it ( 'should generate unique messageIds for multiple calls' , ( ) => {
1066+ // Make multiple deferred calls
1067+ const promise1 = roktManager [ 'deferredCall' ] < string > ( 'method1' , { data : 1 } ) ;
1068+ const promise2 = roktManager [ 'deferredCall' ] < string > ( 'method2' , { data : 2 } ) ;
1069+ const promise3 = roktManager [ 'deferredCall' ] < string > ( 'method3' , { data : 3 } ) ;
1070+
1071+ // Verify all promises are created
1072+ expect ( promise1 ) . toBeInstanceOf ( Promise ) ;
1073+ expect ( promise2 ) . toBeInstanceOf ( Promise ) ;
1074+ expect ( promise3 ) . toBeInstanceOf ( Promise ) ;
1075+
1076+ // Verify all messages are queued
1077+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 3 ) ;
1078+
1079+ // Extract messageIds
1080+ const messageId1 = roktManager [ 'messageQueue' ] [ 0 ] . messageId ! ;
1081+ const messageId2 = roktManager [ 'messageQueue' ] [ 1 ] . messageId ! ;
1082+ const messageId3 = roktManager [ 'messageQueue' ] [ 2 ] . messageId ! ;
1083+
1084+ // Verify all messageIds are unique
1085+ expect ( messageId1 ) . toBeDefined ( ) ;
1086+ expect ( messageId2 ) . toBeDefined ( ) ;
1087+ expect ( messageId3 ) . toBeDefined ( ) ;
1088+ expect ( messageId1 ) . not . toBe ( messageId2 ) ;
1089+ expect ( messageId2 ) . not . toBe ( messageId3 ) ;
1090+ expect ( messageId1 ) . not . toBe ( messageId3 ) ;
1091+
1092+ // Verify all are tracked in pendingPromises
1093+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId1 ) ) . toBe ( true ) ;
1094+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId2 ) ) . toBe ( true ) ;
1095+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId3 ) ) . toBe ( true ) ;
1096+ } ) ;
1097+ } ) ;
1098+
1099+ describe ( '#completePendingPromise' , ( ) => {
1100+ it ( 'should resolve pending promise with success result' , async ( ) => {
1101+ const promise = roktManager [ 'deferredCall' ] < string > ( 'testMethod' , { } ) ;
1102+ const messageId = roktManager [ 'messageQueue' ] [ 0 ] . messageId ! ;
1103+
1104+ // Complete the promise with success result
1105+ roktManager [ 'completePendingPromise' ] ( messageId , 'success result' ) ;
1106+
1107+ // Promise should resolve with the result
1108+ await expect ( promise ) . resolves . toBe ( 'success result' ) ;
1109+
1110+ // Should clean up the pending promise
1111+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId ) ) . toBe ( false ) ;
1112+ } ) ;
1113+
1114+ it ( 'should reject pending promise with error' , async ( ) => {
1115+ const promise = roktManager [ 'deferredCall' ] < string > ( 'testMethod' , { } ) ;
1116+ const messageId = roktManager [ 'messageQueue' ] [ 0 ] . messageId ! ;
1117+ const error = new Error ( 'test error' ) ;
1118+
1119+ // Complete the promise with error (wrapped in rejected promise)
1120+ roktManager [ 'completePendingPromise' ] ( messageId , Promise . reject ( error ) ) ;
1121+
1122+ // Promise should reject with the error
1123+ await expect ( promise ) . rejects . toThrow ( 'test error' ) ;
1124+
1125+ // Should clean up the pending promise
1126+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId ) ) . toBe ( false ) ;
1127+ } ) ;
1128+
1129+ it ( 'should handle async results correctly' , async ( ) => {
1130+ const promise = roktManager [ 'deferredCall' ] < any > ( 'testMethod' , { } ) ;
1131+ const messageId = roktManager [ 'messageQueue' ] [ 0 ] . messageId ! ;
1132+ const asyncResult = { data : 'async data' } ;
1133+
1134+ // Complete with an async promise
1135+ roktManager [ 'completePendingPromise' ] ( messageId , Promise . resolve ( asyncResult ) ) ;
1136+
1137+ // Should get the unwrapped result, not the promise
1138+ const result = await promise ;
1139+ expect ( result ) . toEqual ( asyncResult ) ;
1140+ expect ( result ) . not . toBeInstanceOf ( Promise ) ;
1141+
1142+ // Should clean up the pending promise
1143+ expect ( roktManager [ 'pendingPromises' ] . has ( messageId ) ) . toBe ( false ) ;
1144+ } ) ;
1145+
1146+ it ( 'should handle missing messageId gracefully' , ( ) => {
1147+ // Should not throw when messageId is undefined
1148+ expect ( ( ) => {
1149+ roktManager [ 'completePendingPromise' ] ( undefined , 'result' ) ;
1150+ } ) . not . toThrow ( ) ;
1151+
1152+ // Should not throw when messageId does not exist
1153+ expect ( ( ) => {
1154+ roktManager [ 'completePendingPromise' ] ( 'nonexistent' , 'result' ) ;
1155+ } ) . not . toThrow ( ) ;
1156+ } ) ;
1157+ } ) ;
10301158} ) ;
0 commit comments