Skip to content

Commit 3a13833

Browse files
fix: Correct isInitialized check in hashAttributes and setExtensionData functions (#39)
1 parent 05e6ee9 commit 3a13833

2 files changed

Lines changed: 115 additions & 7 deletions

File tree

src/Rokt-Kit.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var constructor = function () {
5858
* hashed attributes from the launcher, or `null` if the kit is not initialized
5959
*/
6060
function hashAttributes(attributes) {
61-
if (!isInitialized()) {
61+
if (!isKitReady()) {
6262
console.error('Rokt Kit: Not initialized');
6363
return null;
6464
}
@@ -223,7 +223,7 @@ var constructor = function () {
223223
* @returns {void} Nothing is returned
224224
*/
225225
function setExtensionData(partnerExtensionData) {
226-
if (!isInitialized()) {
226+
if (!isKitReady()) {
227227
console.error('Rokt Kit: Not initialized');
228228
return;
229229
}
@@ -271,10 +271,11 @@ var constructor = function () {
271271
);
272272
}
273273
}
274-
// Attaches the kit to the Rokt manager
275-
window.mParticle.Rokt.attachKit(self);
276274

275+
// Kit must be initialized before attaching to the Rokt manager
277276
self.isInitialized = true;
277+
// Attaches the kit to the Rokt manager
278+
window.mParticle.Rokt.attachKit(self);
278279
})
279280
.catch(function (err) {
280281
console.error('Error creating Rokt launcher:', err);
@@ -335,13 +336,13 @@ var constructor = function () {
335336
this.removeUserAttribute = removeUserAttribute;
336337

337338
/**
338-
* Checks if the kit is properly initialized and ready for use.
339+
* Checks if the Rokt kit is ready to use.
339340
* Both conditions must be true:
340341
* 1. self.isInitialized - Set after successful initialization of the kit
341342
* 2. self.launcher - The Rokt launcher instance must be available
342-
* @returns {boolean} Whether the kit is fully initialized
343+
* @returns {boolean} Whether the kit is ready for use
343344
*/
344-
function isInitialized() {
345+
function isKitReady() {
345346
return !!(self.isInitialized && self.launcher);
346347
}
347348
};

test/src/tests.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,113 @@ describe('Rokt Forwarder', () => {
444444
});
445445
});
446446

447+
describe('#attachLauncher', () => {
448+
let mockMessageQueue;
449+
450+
beforeEach(() => {
451+
mockMessageQueue = [];
452+
453+
// Reset forwarder state between tests
454+
window.mParticle.forwarder.isInitialized = false;
455+
456+
window.Rokt = new MockRoktForwarder();
457+
window.mParticle.Rokt = window.Rokt;
458+
window.mParticle.Rokt.attachKitCalled = false;
459+
460+
// Set attachKit as async to allow for await calls in the test
461+
// This is necessary to simiulate a race condition between the
462+
// core sdk and the Rokt forwarder
463+
window.mParticle.Rokt.attachKit = async (kit) => {
464+
window.mParticle.Rokt.attachKitCalled = true;
465+
window.mParticle.Rokt.kit = kit;
466+
467+
// Call queued messages
468+
mockMessageQueue.forEach((message) => message());
469+
mockMessageQueue = [];
470+
471+
return Promise.resolve();
472+
};
473+
window.mParticle.Rokt.filters = {
474+
userAttributesFilters: [],
475+
filterUserAttributes: function (attributes) {
476+
return attributes;
477+
},
478+
filteredUser: {
479+
getMPID: function () {
480+
return '123';
481+
},
482+
},
483+
};
484+
});
485+
486+
it('should call attachKit', async () => {
487+
await window.mParticle.forwarder.init(
488+
{ accountId: '123456' },
489+
reportService.cb,
490+
true,
491+
null,
492+
{}
493+
);
494+
495+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
496+
497+
window.mParticle.Rokt.attachKitCalled.should.equal(true);
498+
});
499+
500+
it('should set isInitialized to true', async () => {
501+
await window.mParticle.forwarder.init(
502+
{ accountId: '123456' },
503+
reportService.cb,
504+
true,
505+
null,
506+
{}
507+
);
508+
509+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
510+
511+
window.mParticle.forwarder.isInitialized.should.equal(true);
512+
});
513+
514+
// This test is to ensure the kit is initialized before attaching to the Rokt manager
515+
// so we can ensure that the Rokt Manager's message queue is processed and that
516+
// all the isReady() checks are properly handled in by the Rokt Manager.
517+
// This is to validate in case a bug that was found in the Rokt Manager's
518+
// queueing logic regresses.
519+
it('should initialize the kit before calling queued messages', async () => {
520+
let queuedMessageCalled = false;
521+
let wasKitInitializedFirst = false;
522+
523+
const queuedMessage = () => {
524+
wasKitInitializedFirst =
525+
window.mParticle.Rokt.kit &&
526+
window.mParticle.Rokt.kit.isInitialized;
527+
queuedMessageCalled = true;
528+
};
529+
530+
mockMessageQueue.push(queuedMessage);
531+
532+
await window.mParticle.forwarder.init(
533+
{ accountId: '123456' },
534+
reportService.cb,
535+
true,
536+
null,
537+
{}
538+
);
539+
540+
window.mParticle.forwarder.isInitialized.should.equal(false);
541+
queuedMessageCalled.should.equal(false);
542+
543+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
544+
545+
window.mParticle.forwarder.isInitialized.should.equal(true);
546+
queuedMessageCalled.should.equal(true);
547+
548+
wasKitInitializedFirst.should.equal(true);
549+
550+
mockMessageQueue.length.should.equal(0);
551+
});
552+
});
553+
447554
describe('#selectPlacements', () => {
448555
beforeEach(() => {
449556
window.Rokt = new MockRoktForwarder();

0 commit comments

Comments
 (0)