Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ var constructor = function () {
self.filteredUser = {};
self.userAttributes = {};

/**
* Passes attributes to the Rokt Web SDK for server-side hashing
Comment thread
alexs-mparticle marked this conversation as resolved.
Outdated
* @param {Object} attributes - The attributes to be hashed
* @returns {Object} The hashed attributes from the launcher
Comment thread
alexs-mparticle marked this conversation as resolved.
Outdated
*/
function hashedAttributes(attributes) {
if (!isInitialized()) {
console.error('Rokt Kit: Not initialized');
return null;
}
return self.launcher.hashedAttributes(attributes);
}

function initForwarder(
settings,
_service,
Expand Down Expand Up @@ -192,8 +205,6 @@ var constructor = function () {
});
}

// Called by the mParticle Rokt Manager
this.selectPlacements = selectPlacements;

// mParticle Kit Callback Methods
function fetchOptimizely() {
Expand Down Expand Up @@ -236,10 +247,29 @@ var constructor = function () {
}
return {};
}


// Called by the mParticle Rokt Manager
this.selectPlacements = selectPlacements;
this.hashedAttributes = hashedAttributes;

// Kit Callback Methods
this.init = initForwarder;
this.setUserAttribute = setUserAttribute;
this.onUserIdentified = onUserIdentified;
this.removeUserAttribute = removeUserAttribute;

/**
* Checks if the kit is properly initialized and ready for use.
* Both conditions must be true:
* 1. self.isInitialized - Set after successful initialization of the kit
* 2. self.launcher - The Rokt launcher instance must be available
* @returns {boolean} Whether the kit is fully initialized
*/
function isInitialized() {
return !!(self.isInitialized && self.launcher);
}

};

function getId() {
Expand Down
115 changes: 115 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,121 @@ describe('Rokt Forwarder', () => {
});
});

describe('#hashedAttributes', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async (kit) => {
window.mParticle.Rokt.attachKitCalled = true;
window.mParticle.Rokt.kit = kit;
Promise.resolve();
};
window.mParticle.forwarder.launcher = {
hashedAttributes: function (attributes) {
window.mParticle.Rokt.hashedAttributesOptions = attributes;
window.mParticle.Rokt.hashedAttributesCalled = true;

// Mocking the hashedAttributes method to show that
// the attributes will be transformed by the launcher's
// hashedAttributes method.
return Promise.resolve({
'test-attribute': 'hashed-value',
});
},
};
});

it('should call launcher.hashedAttributes with passed through attributes when fully initialized', function () {
// Ensure both initialization conditions are met
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = {
hashedAttributes: function (attributes) {
window.mParticle.Rokt.hashedAttributesOptions = attributes;
window.mParticle.Rokt.hashedAttributesCalled = true;
return {
'test-attribute': 'hashed-value',
};
},
};

var attributes = {
'test-attribute': 'test-value',
};

window.mParticle.forwarder.hashedAttributes(attributes);

window.Rokt.hashedAttributesCalled.should.equal(true);
window.Rokt.hashedAttributesOptions.should.deepEqual(attributes);
});

it('should return null when launcher exists but kit is not initialized', function () {
// Set launcher but ensure isInitialized is false
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = {
hashedAttributes: function () {},
};

var result = window.mParticle.forwarder.hashedAttributes({
'test-attribute': 'test-value',
});

(result === null).should.equal(true);
});

it('should log an error when called before initialization', function () {
var errorLogged = false;
var errorMessage = null;
window.console.error = function (message) {
errorLogged = true;
errorMessage = message;
};

// Ensure kit is not initialized
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = null;

window.mParticle.forwarder.hashedAttributes({
'test-attribute': 'test-value',
});

errorLogged.should.equal(true);
errorMessage.should.equal('Rokt Kit: Not initialized');
});

it('should return null when kit is initialized but launcher is missing', function () {
Comment thread
alexs-mparticle marked this conversation as resolved.
// Mock isInitialized but remove launcher
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = null;

var result = window.mParticle.forwarder.hashedAttributes({
'test-attribute': 'test-value',
});

(result === null).should.equal(true);
});

it('should return hashed attributes from launcher', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);

const result = await window.mParticle.forwarder.hashedAttributes({
'test-attribute': 'test-value',
});

result.should.deepEqual({
'test-attribute': 'hashed-value',
});
});
});

describe('#selectPlacements', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
Expand Down