Skip to content

Commit

Permalink
Update unit tests and implement identity handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SbDove committed Oct 4, 2024
1 parent b186e54 commit d5d4503
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Common.prototype.exampleMethod = function () {
}

Common.prototype.logId5Id = function (id5Id) {
debugger;
//Checks and saves ID5 ID if it is new
if (id5Id != this.id5Id) {
this.id5Id = id5Id;
Expand All @@ -20,7 +21,7 @@ Common.prototype.logId5Id = function (id5Id) {
currentUser.setUserAttribute('ID5ID', id5Id);
this.id5IdSent = true;
}
};
}.bind(this);

Common.prototype.buildPartnerData = function (mParticleUser) {
var pdKeys = {};
Expand All @@ -46,7 +47,6 @@ Common.prototype.buildPartnerData = function (mParticleUser) {
}

Common.prototype.normalizeEmail = function(email) {
debugger;
var parts = email.split("@")
var charactersToRemove = ['+', '.']

Expand Down
22 changes: 18 additions & 4 deletions src/identity-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ IdentityHandler.prototype.onIdentifyComplete = function(
identityApiRequest
) {};
IdentityHandler.prototype.onLoginComplete = function(
mParticleUser,
identityApiRequest
) {};
mParticleUser
) {
var partnerData = this.common.buildPartnerData(mParticleUser);
var id5Instance = window.ID5.init({partnerId: this.common.partnerId, pd: partnerData})
var logId5Id = this.common.logId5Id;

id5Instance.onAvailable(function(status){
logId5Id(status.getUserId());
}.bind(logId5Id));
};
IdentityHandler.prototype.onLogoutComplete = function(
mParticleUser,
identityApiRequest
) {};
) {
var id5Instance = window.ID5.init({partnerId: this.common.partnerId})
var logId5Id = this.common.logId5Id;

id5Instance.onAvailable(function(status){
logId5Id(status.getUserId());
}.bind(logId5Id));
};
IdentityHandler.prototype.onModifyComplete = function(
mParticleUser,
identityApiRequest
Expand Down
10 changes: 8 additions & 2 deletions src/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ var initialization = {

id5Script.onload = function() {
isInitialized = true;

var id5Instance = window.ID5.init({partnerId: common.partnerId})

id5Instance.onAvailable(function(status){
common.logId5Id(status.getUserId());
}.bind(common));
};
} else {
// For testing, you should fill out this section in order to ensure any required initialization calls are made,
// clientSDKObject.initialize(forwarderSettings.apiKey)
isInitialized = true;

var id5Instance = window.ID5.init({partnerId: common.partnerId})

id5Instance.onAvailable(function(status){
common.logId5Id(status.getUserId());
}.bind(common));
}
}
};
Expand Down
149 changes: 55 additions & 94 deletions test/src/tests.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,7 @@
/* eslint-disable no-undef*/
describe('ID5 Forwarder', function () {
// -------------------DO NOT EDIT ANYTHING BELOW THIS LINE-----------------------
var MessageType = {
SessionStart: 1,
SessionEnd: 2,
PageView: 3,
PageEvent: 4,
CrashReport: 5,
OptOut: 6,
AppStateTransition: 10,
Profile: 14,
Commerce: 16,
Media: 20,
UserAttributeChange: 17,
UserIdentityChange: 18,
},
EventType = {
Unknown: 0,
Navigation: 1,
Location: 2,
Search: 3,
Transaction: 4,
UserContent: 5,
UserPreference: 6,
Social: 7,
Other: 8,
Media: 9,
getName: function() {
return 'blahblah';
}
},
ProductActionType = {
Unknown: 0,
AddToCart: 1,
RemoveFromCart: 2,
Checkout: 3,
CheckoutOption: 4,
Click: 5,
ViewDetail: 6,
Purchase: 7,
Refund: 8,
AddToWishlist: 9,
RemoveFromWishlist: 10
},
IdentityType = {
Other: 0,
CustomerId: 1,
Facebook: 2,
Twitter: 3,
Google: 4,
Microsoft: 5,
Yahoo: 6,
Email: 7,
FacebookCustomAudienceId: 9,
Other2: 10,
Other3: 11,
Other4: 12,
Other5: 13,
Other6: 14,
Other7: 15,
Other8: 16,
Other9: 17,
Other10: 18,
MobileNumber: 19,
PhoneNumber2: 20,
PhoneNumber3: 21,
},
ReportingService = function () {
var ReportingService = function () {
var self = this;

this.id = null;
Expand All @@ -84,7 +19,7 @@ describe('ID5 Forwarder', function () {
},
reportService = new ReportingService();

// -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE-----------------------
// -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE-----------------------
// -------------------START EDITING BELOW:-----------------------
// -------------------mParticle stubs - Add any additional stubbing to our methods as needed-----------------------
var userAttributes = {};
Expand All @@ -106,14 +41,15 @@ describe('ID5 Forwarder', function () {
}
};
// -------------------START EDITING BELOW:-----------------------
var MockID5Forwarder = function() {
var MockID5 = function() {
var self = this;

// create properties for each type of event you want tracked, see below for examples
this.trackCustomEventCalled = false;
this.logPurchaseEventCalled = false;
this.isInitialized = false;
this.initCalled = false;
this.numberOfInitsCalled = 0;
this.getUserIdCalled = false;

this.pd = null;
Expand All @@ -123,61 +59,86 @@ describe('ID5 Forwarder', function () {
// stub your different methods to ensure they are being called properly
this.init = function(id5Options) {
self.initCalled = true;
self.numberOfInitsCalled += 1;
self.partnerId = id5Options.partnerId;
self.pd = id5Options.pd
return this;
};

this.onAvailable = function(callback) {
return callback
return callback(self)
};

this.getUserId = function() {
self.getUserIdCalled = true;
return 'ID5*testtesttesttest'
}

return self;
};

before(function () {

});

beforeEach(function() {
window.ID5 = new MockID5Forwarder();
window.ID5 = new MockID5();
// Include any specific settings that is required for initializing your SDK here
var sdkSettings = {
partnerId: 1234,
};
// You may require userAttributes or userIdentities to be passed into initialization
var userAttributes = {
color: 'green'
};

var userIdentities = [{
Identity: 'customerId',
Type: IdentityType.CustomerId
}, {
Identity: 'email',
Type: IdentityType.Email
}, {
Identity: 'mobile_number',
Type: IdentityType.MobileNumber
}];


// The third argument here is a boolean to indicate that the integration is in test mode to avoid loading any third party scripts. Do not change this value.
mParticle.forwarder.init(sdkSettings, reportService.cb, true, null, userAttributes, userIdentities);
mParticle.forwarder.init(sdkSettings, reportService.cb, true);
});


it ('should load the script into the document during initialization', function(done) {
window.ID5 = new MockID5Forwarder();
mParticle.forwarder.init({
partnerId: 1234,
describe('Initialization', function() {
it('should call ID5.init', function(done) {
window.ID5.initCalled.should.equal(true);
done();
});
document.scripts[0].src.should.equal('https://cdn.id5-sync.com/api/1.0/id5-api.js');
done();
});

it('should call getUserId', function(done) {
window.ID5.getUserIdCalled.should.equal(true);
done();
});
})

describe('Identity Handling', function() {
it('should call ID5.init twice on userLoginComplete', function(done){
var user = {
getUserIdentities: function () {
return {
userIdentities: {
email: '[email protected]',
},
};
},
};
mParticle.forwarder.onLoginComplete(user);

window.ID5.numberOfInitsCalled.should.equal(2);
done();
});

it('should call ID5.init twice on userLogoutComplete', function(done){
var user = {
getUserIdentities: function () {
return {
userIdentities: {
email: '[email protected]',
},
};
},
};
mParticle.forwarder.onLoginComplete(user);

window.ID5.numberOfInitsCalled.should.equal(2);
done();
});
})


describe('Common Functions', function() {
it ('should log a user attribute when logId5 is called', function(done) {
Expand Down

0 comments on commit d5d4503

Please sign in to comment.