Skip to content

Commit

Permalink
chore: resolve conflicts and update testcase format to similar format
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip committed Apr 25, 2024
2 parents 6ad018f + bb7e1df commit 4610d92
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 114 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@preact/signals-core": "1.5.1",
"get-value": "3.0.1",
"ramda": "0.29.1",
"storejs": "2.0.7"
"storejs": "2.1.0"
},
"devDependencies": {
"@babel/core": "7.23.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const CNameMapping = {
criteo: NAME,
};

const supportedEvents = ['product viewed', 'cart viewed', 'order completed', 'product list viewed'];
const supportedEvents = ['product viewed', 'cart viewed', 'order completed', 'product list viewed', 'product added'];

export {
NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,177 @@
import md5 from 'md5';
import { handleCommonFields } from '../../../src/integrations/Criteo/utils'
import { handleCommonFields, handleProductAdded } from '../../../src/integrations/Criteo/utils';

describe('handleCommonFields', () => {
const inputEvent = {
message: {
userId: 'u1',
anonymousId: 'a1',
properties: {
email: '[email protected]'
}
}
}
email: '[email protected]',
},
},
};
const defaultExpected = [
{ event: 'setCustomerId', id: md5('u1').toString() },
{ event: 'setRetailerVisitorId', id: md5('a1').toString() },
]
const tcs = [
{
description: 'when properties.email is present, for md5 hashmethod the relevant mapping should be included',
inputEvent,
hashMethod: 'md5',
expected: [{
];
it('when properties.email is present, for md5 hashmethod the relevant mapping should be included', () => {
const out = handleCommonFields(inputEvent, 'md5');
expect(out).toEqual([
...defaultExpected,
{
email: '3f009d72559f51e7e454b16e5d0687a1',
hash_method: 'md5',
event: 'setEmail'
}]
},
{
description: 'when properties.email is present, for sha256 hashmethod the relevant mapping should be included',
inputEvent,
hashMethod: 'sha256',
expected: [{
event: 'setEmail',
},
]);
});
it('when properties.email is present, for sha256 hashmethod the relevant mapping should be included', () => {
const output = handleCommonFields(inputEvent, 'sha256');
expect(output).toEqual([
...defaultExpected,
{
email: '48ddb93f0b30c475423fe177832912c5bcdce3cc72872f8051627967ef278e08',
hash_method: 'sha256',
event: 'setEmail'
}]
},
{
description: 'when properties.email is present, for random hashmethod the relevant mapping should be included',
inputEvent,
hashMethod: 'random',
expected: [{
event: 'setEmail',
},
]);
});
it('when properties.email is present, for random hashmethod the relevant mapping should be included', () => {
const output = handleCommonFields(inputEvent, 'random');
expect(output).toEqual([
...defaultExpected,
{
email: '[email protected]',
hash_method: 'random',
event: 'setEmail'
}]
},
]
test.each(tcs)('$description', ({inputEvent, hashMethod, expected}) => {
const out = handleCommonFields(inputEvent, hashMethod)
expect(out).toEqual([...defaultExpected, ...expected])
})
})
event: 'setEmail',
},
]);
});
});

describe('handleProductAdded', () => {
// The function correctly extracts the 'properties' object from the 'message' parameter.
it('should correctly extract properties object from message parameter', () => {
const message = {
properties: {
product_id: '123',
price: '9.99',
quantity: '5',
currency: 'USD',
},
};
const finalPayload = [];

handleProductAdded(message, finalPayload);

expect(finalPayload.length).toBe(1);
expect(finalPayload[0].event).toBe('addToCart');
expect(finalPayload[0].currency).toBe('USD');
expect(finalPayload[0].item.length).toBe(1);
expect(finalPayload[0].item[0].id).toBe('123');
expect(finalPayload[0].item[0].price).toBe(9.99);
expect(finalPayload[0].item[0].quantity).toBe(5);
});

// When the 'message' parameter is undefined, the function throws an error.
it('should throw an error when message parameter is undefined', () => {
const message = undefined;
const finalPayload = [];

expect(() => {
handleProductAdded(message, finalPayload);
}).toThrow();
});

// When the 'properties' object is valid, the function creates an 'addToCartObject' with the correct structure.
it('should create addToCartObject with correct structure when properties object is valid', () => {
const message = {
properties: {
product_id: '123',
price: '9.99',
quantity: '5',
currency: 'USD',
},
};
const finalPayload = [];

handleProductAdded(message, finalPayload);

expect(finalPayload.length).toBe(1);
expect(finalPayload[0].event).toBe('addToCart');
expect(finalPayload[0].currency).toBe('USD');
expect(finalPayload[0].item.length).toBe(1);
expect(finalPayload[0].item[0].id).toBe('123');
expect(finalPayload[0].item[0].price).toBe(9.99);
expect(finalPayload[0].item[0].quantity).toBe(5);
});

// When the product is valid, the function adds the product object to the 'item' property of the 'addToCartObject'.
it("should add product object to 'item' property when product is valid", () => {
const message = {
properties: {
product_id: '123',
price: '9.99',
quantity: '5',
currency: 'USD',
},
};
const finalPayload = [];

handleProductAdded(message, finalPayload);

expect(finalPayload.length).toBe(1);
expect(finalPayload[0].event).toBe('addToCart');
expect(finalPayload[0].currency).toBe('USD');
expect(finalPayload[0].item.length).toBe(1);
expect(finalPayload[0].item[0].id).toBe('123');
expect(finalPayload[0].item[0].price).toBe(9.99);
expect(finalPayload[0].item[0].quantity).toBe(5);
});

// The function correctly pushes the 'addToCartObject' to the 'finalPayload' array.
it("should correctly push 'addToCartObject' to 'finalPayload' array", () => {
const message = {
properties: {
product_id: '123',
price: '9.99',
quantity: '5',
currency: 'USD',
},
};
const finalPayload = [];

handleProductAdded(message, finalPayload);

expect(finalPayload.length).toBe(1);
expect(finalPayload[0].event).toBe('addToCart');
expect(finalPayload[0].currency).toBe('USD');
expect(finalPayload[0].item.length).toBe(1);
expect(finalPayload[0].item[0].id).toBe('123');
expect(finalPayload[0].item[0].price).toBe(9.99);
expect(finalPayload[0].item[0].quantity).toBe(5);
});

// When the 'properties' object is missing the 'currency' property, the function creates an 'addToCartObject' without the 'currency' property.
it("should create 'addToCartObject' without 'currency' property when 'properties' object is missing 'currency'", () => {
const message = {
properties: {
product_id: '123',
price: '9.99',
quantity: '5',
},
};
const finalPayload = [];

handleProductAdded(message, finalPayload);

expect(finalPayload.length).toBe(1);
expect(finalPayload[0].event).toBe('addToCart');
expect(finalPayload[0].currency).toBeUndefined();
expect(finalPayload[0].item.length).toBe(1);
expect(finalPayload[0].item[0].id).toBe('123');
expect(finalPayload[0].item[0].price).toBe(9.99);
expect(finalPayload[0].item[0].quantity).toBe(5);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ const destinationInfo = {
// jest.mock('this.analytics.getUserId', () => '1234');

describe('Google Analytics 4 init tests', () => {
test('Testing init call of Google Analytics 4 with MeasurementId', () => {
test('Testing init call of Google Analytics 4 with MeasurementId and SdkBaseUrl', () => {
const ga4 = new GA4(
{ measurementId: 'G-123456', debugView: true },
{ measurementId: 'G-123456', debugView: true, sdkBaseUrl: 'https://www.example.com//' },
{ getUserId: () => '1234', getUserTraits: () => {} },
destinationInfo,
);
ga4.init();
expect(typeof window.gtag).toBe('function');
expect(ga4.sdkBaseUrl).toEqual('https://www.example.com');
});
});

Expand Down Expand Up @@ -56,7 +57,11 @@ describe('Google Analytics 4 Page events tests', () => {
let ga4;
const { description, input, output } = event;
beforeEach(() => {
ga4 = new GA4(input.config, { getUserId: () => '1234', getUserTraits: () => {} }, destinationInfo);
ga4 = new GA4(
input.config,
{ getUserId: () => '1234', getUserTraits: () => {} },
destinationInfo,
);
ga4.init();
window.gtag = jest.fn();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
handleProductView,
generateExtraData,
handleCommonFields,
handleProductAdded,
} from './utils';
import { getHashFromArrayWithDuplicate } from '../../utils/commonUtils';

Expand Down Expand Up @@ -76,7 +77,7 @@ class Criteo {
};
finalPayload.push(homeEvent);
} else {
logger.error('Home page is not detected');
logger.warn('Home page is not detected');
return;
}

Expand All @@ -99,15 +100,15 @@ class Criteo {
}

if (!properties || Object.keys(properties).length === 0) {
logger.error('Either properties object is missing or empty in the track call');
logger.warn('Either properties object is missing or empty in the track call');
return;
}

const eventMapping = getHashFromArrayWithDuplicate(this.eventsToStandard);
const trimmedEvent = event.toLowerCase().trim();

if (!supportedEvents.includes(trimmedEvent) && !eventMapping[trimmedEvent]) {
logger.error(`event ${trimmedEvent} is not supported`);
logger.warn(`event ${trimmedEvent} is not supported`);
return;
}
let events = [];
Expand All @@ -129,6 +130,9 @@ class Criteo {
case 'product list viewed':
handleListView(rudderElement.message, finalPayload, this.OPERATOR_LIST);
break;
case 'product added':
handleProductAdded(rudderElement.message, finalPayload)
break;
default:
break;
}
Expand Down
Loading

0 comments on commit 4610d92

Please sign in to comment.