Skip to content

Commit

Permalink
Rework tests for stability and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
SbDove committed Jun 27, 2024
1 parent 2f84727 commit a710f53
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 55 deletions.
17 changes: 7 additions & 10 deletions src/commerce-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,14 @@ function buildProductActionEvents(event) {
}

function buildActionEvent(event, eventName, productSkus) {
var properties = event.EventAttributes == null ? {} : event.EventAttributes;
var properties = event && event.EventAttributes ? event.EventAttributes : {};
properties[HeapConstants.KeyProductSkus] = productSkus;
return {Name: eventName, Properties: properties};
}

function buildItemEvent(product) {
var event = {};
var properties = product.Attributes;
if (!properties) {
properties = {};
}
var properties = product && product.Attributes ? product.Attributes : {};

var validatedName = validateHeapPropertyValue(product.Name);
if (validatedName) {
Expand Down Expand Up @@ -262,22 +259,22 @@ function buildItemEvent(product) {
event.Name = HeapConstants.EventNameItem;
event.Properties = properties;

var productSku = validatedSku;
return {event: event, sku: productSku};
return {event: event, sku: validatedSku};
}

function buildPromotionItemEvent(promotion) {
var event = {};
var properties = promotion.Attributes ? promotion.Attributes : {};
var properties = promotion && promotion.Attributes ? promotion.Attributes : {};

var validatedPromotionValues = {
KeyPromotionCreative: validateHeapPropertyValue(promotion.Creative),
KeyPromotionId: validateHeapPropertyValue(promotion.Id),
KeyPromotionPosition: validateHeapPropertyValue(promotion.Position),
}

for (var i = 0; i < Object.keys(validatedPromotionValues).length; i++) {
var key = Object.keys(validatedPromotionValues)[i];
var validatedPromotionKeys = Object.keys(validatedPromotionValues);
for (var i = 0; i < validatedPromotionKeys.length; i++) {
var key = validatedPromotionKeys[i];
var value = validatedPromotionValues[key];

if (value === undefined || key === undefined) {
Expand Down
71 changes: 26 additions & 45 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,7 @@ describe('Heap Forwarder', function () {
});

describe('CommerceEventProcessing', function () {
var product1 = {
Name: 'iphone',
Sku: 'iphoneSKU',
Price: 999,
Quantity: 1,
Brand: 'brand',
Variant: 'variant',
Category: 'category',
Position: 1,
CouponCode: 'coupon',
TotalAmount: 999,
Attributes: {
prod1AttrKey1: 'value1',
prod1AttrKey2: 'value2',
},
};

var product2 = {
var product = {
Name: 'galaxy',
Sku: 'galaxySKU',
Price: 799,
Expand All @@ -363,8 +346,8 @@ describe('Heap Forwarder', function () {
ProductAction: {
ProductActionType: ProductActionType.Purchase,
ProductList: [
product1,
product1,
product,
product,
],
TransactionId: 123,
Affiliation: 'my-affiliation',
Expand All @@ -383,15 +366,15 @@ describe('Heap Forwarder', function () {
{
ProductImpressionList: 'Suggested Products List1',
ProductList: [
product1,
product2,
product,
product,
],
},
{
ProductImpressionList: 'Suggested Products List2',
ProductList: [
product1,
product2,
product,
product,
],
},
],
Expand Down Expand Up @@ -419,25 +402,15 @@ describe('Heap Forwarder', function () {
},
};

var validateProductProperties = function (properties) {
if (properties.product_name === 'iphone') {
properties.prod1AttrKey1.should.equal('value1');
properties.prod1AttrKey2.should.equal('value2');
properties.product_brand.should.equal('brand');
properties.product_category.should.equal('category');
properties.product_id.should.equal('iphoneSKU');
properties.product_price.should.equal(999);
properties.product_quantity.should.equal(1);
} else {
properties.prod2AttrKey1.should.equal('value1');
properties.prod2AttrKey2.should.equal('value2');
properties.product_brand.should.equal('brand');
properties.product_category.should.equal('category');
properties.product_id.should.equal('galaxySKU');
properties.product_price.should.equal(799);
properties.product_quantity.should.equal(1);
}

var validatedProductProperties = {
prod2AttrKey1: 'value1',
prod2AttrKey2: 'value2',
product_name: 'galaxy',
product_brand: 'brand',
product_category: 'category',
product_id: 'galaxySKU',
product_price: 799,
product_quantity: 1,
};

it('should process a product purchase commerce event', function (done) {
Expand All @@ -451,14 +424,18 @@ describe('Heap Forwarder', function () {
mParticle.forwarder.process(purchaseEvent);

window.heap.trackCalled.should.equal(true);

// An mParticle product action events map to n+1
// events in Heap based on the number of associated
// products.
window.heap.events.length.should.equal(3);

for (var i = 0; i < window.heap.events.length; i++) {
var eventName = window.heap.events[i];
var properties = window.heap.eventProperties[i];

if (eventName === 'Item') {
validateProductProperties(properties);
properties.should.deep.equal(validatedProductProperties);
}

if (eventName.includes('Action')) {
Expand All @@ -480,14 +457,18 @@ describe('Heap Forwarder', function () {
mParticle.forwarder.process(impressionEvent);

window.heap.trackCalled.should.equal(true);

// Each mParticle impression event will map
// to n+1 events in heap for each impression
// based on the number of associated products.
window.heap.events.length.should.equal(6);

for (var i = 0; i < window.heap.events.length; i++) {
var eventName = window.heap.events[i];
var properties = window.heap.eventProperties[i];

if (eventName === 'Item') {
validateProductProperties(properties);
properties.should.deep.equal(validatedProductProperties);
}

if (eventName.includes('Action')) {
Expand Down

0 comments on commit a710f53

Please sign in to comment.