Skip to content

Commit c243371

Browse files
Rename isInitialized to isKitReady and update related checks for clarity
1 parent a3b952f commit c243371

2 files changed

Lines changed: 107 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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,105 @@ 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+
window.mParticle.Rokt.attachKit = async (kit) => {
460+
console.log('Rokt Kit: attachKit called');
461+
window.mParticle.Rokt.attachKitCalled = true;
462+
window.mParticle.Rokt.kit = kit;
463+
464+
// Call queued messages
465+
mockMessageQueue.forEach((message) => message());
466+
mockMessageQueue = [];
467+
468+
return Promise.resolve();
469+
};
470+
window.mParticle.Rokt.filters = {
471+
userAttributesFilters: [],
472+
filterUserAttributes: function (attributes) {
473+
return attributes;
474+
},
475+
filteredUser: {
476+
getMPID: function () {
477+
return '123';
478+
},
479+
},
480+
};
481+
});
482+
483+
it('should call attachKit', async () => {
484+
await window.mParticle.forwarder.init(
485+
{ accountId: '123456' },
486+
reportService.cb,
487+
true,
488+
null,
489+
{}
490+
);
491+
492+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
493+
494+
window.mParticle.Rokt.attachKitCalled.should.equal(true);
495+
});
496+
497+
it('should set isInitialized to true', async () => {
498+
await window.mParticle.forwarder.init(
499+
{ accountId: '123456' },
500+
reportService.cb,
501+
true,
502+
null,
503+
{}
504+
);
505+
506+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
507+
508+
window.mParticle.forwarder.isInitialized.should.equal(true);
509+
});
510+
511+
it('should initialize the kit before calling queued messages', async () => {
512+
let queuedMessageCalled = false;
513+
let wasKitInitializedFirst = false;
514+
515+
const queuedMessage = () => {
516+
wasKitInitializedFirst =
517+
window.mParticle.Rokt.kit &&
518+
window.mParticle.Rokt.kit.isInitialized;
519+
queuedMessageCalled = true;
520+
};
521+
522+
mockMessageQueue.push(queuedMessage);
523+
524+
await window.mParticle.forwarder.init(
525+
{ accountId: '123456' },
526+
reportService.cb,
527+
true,
528+
null,
529+
{}
530+
);
531+
532+
window.mParticle.forwarder.isInitialized.should.equal(false);
533+
queuedMessageCalled.should.equal(false);
534+
535+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
536+
537+
window.mParticle.forwarder.isInitialized.should.equal(true);
538+
queuedMessageCalled.should.equal(true);
539+
540+
wasKitInitializedFirst.should.equal(true);
541+
542+
mockMessageQueue.length.should.equal(0);
543+
});
544+
});
545+
447546
describe('#selectPlacements', () => {
448547
beforeEach(() => {
449548
window.Rokt = new MockRoktForwarder();

0 commit comments

Comments
 (0)