Skip to content

Commit

Permalink
fix: identify validation page screen
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Feb 19, 2024
1 parent f97f9ce commit caa8d45
Show file tree
Hide file tree
Showing 9 changed files with 1,074 additions and 99 deletions.
1 change: 0 additions & 1 deletion src/v0/destinations/facebook_pixel/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ const processEvent = (message, destination) => {
let mappedEvent;
switch (messageType) {
case EventType.IDENTIFY:
console.log('Identify event advancedMapping', advancedMapping);
if (advancedMapping) {
category = CONFIG_CATEGORIES.USERDATA;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/v0/destinations/facebook_pixel/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const formatRevenue = (revenue) => {
*/
const getActionSource = (payload, channel) => {
let actionSource = 'other';
if (payload.action_source) {
if (payload?.action_source) {
const isActionSourceValid = ACTION_SOURCES_VALUES.includes(payload.action_source);
if (!isActionSourceValid) {
throw new InstrumentationError('Invalid Action Source type');
Expand Down
170 changes: 170 additions & 0 deletions src/v0/destinations/facebook_pixel/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { getActionSource, formatRevenue, getCategoryFromEvent } = require('./utils');
const { CONFIG_CATEGORIES, OTHER_STANDARD_EVENTS } = require('./config');

describe('Test Facebook Pixel Utils', () => {
describe('getActionSource', () => {
// Returns 'other' if payload.action_source is not defined and channel is neither 'web' nor 'mobile'
it('should return "other" when payload.action_source is not defined and channel is neither "web" nor "mobile"', () => {
const payload = {};
const channel = 'email';
const result = getActionSource(payload, channel);
expect(result).toBe('other');
});

// Returns payload.action_source if it is defined and is a valid value from ACTION_SOURCES_VALUES
it('should return payload.action_source when it is defined and is a valid value from ACTION_SOURCES_VALUES', () => {
const payload = { action_source: 'website' };
const channel = 'email';
const result = getActionSource(payload, channel);
expect(result).toBe('website');
});

// Returns 'website' if channel is 'web' and payload.action_source is not defined
it('should return "website" when channel is "web" and payload.action_source is not defined', () => {
const payload = {};
const channel = 'web';
const result = getActionSource(payload, channel);
expect(result).toBe('website');
});

// Throws an InstrumentationError if payload.action_source is defined but not a valid value from ACTION_SOURCES_VALUES
it('should throw an InstrumentationError when payload.action_source is defined but not a valid value from ACTION_SOURCES_VALUES', () => {
const payload = { action_source: 'invalid' };
const channel = 'email';
expect(() => {
getActionSource(payload, channel);
}).toThrow(InstrumentationError);
});

// Returns 'other' if payload is not defined
it('should return "other" when payload is not defined', () => {
const payload = undefined;
const channel = 'email';
const result = getActionSource(payload, channel);
expect(result).toBe('other');
});

// Returns 'website' if channel is 'web' and payload is not defined
it('should return "website" when channel is "web" and payload is not defined', () => {
const payload = undefined;
const channel = 'web';
const result = getActionSource(payload, channel);
expect(result).toBe('website');
});
});

describe('formatRevenue', () => {
// Returns a number with two decimal places when passed a valid revenue value.
it('should return a number with two decimal places when passed a valid revenue value', () => {
const revenue = '100.50';
const formattedRevenue = formatRevenue(revenue);
expect(formattedRevenue).toBe(100.5);
});

// Returns 0 when passed a null revenue value.
it('should return 0 when passed a null revenue value', () => {
const revenue = null;
const formattedRevenue = formatRevenue(revenue);
expect(formattedRevenue).toBe(0);
});

// Returns 0 when passed an undefined revenue value.
it('should return 0 when passed an undefined revenue value', () => {
const revenue = undefined;
const formattedRevenue = formatRevenue(revenue);
expect(formattedRevenue).toBe(0);
});

// Throws an InstrumentationError when passed a non-numeric string revenue value.
it('should throw an InstrumentationError when passed a non-numeric string revenue value', () => {
const revenue = 'abc';
expect(() => {
formatRevenue(revenue);
}).toThrow(InstrumentationError);
});

// Returns a number with two decimal places when passed a numeric string revenue value with more than two decimal places.
it('should return a number with two decimal places when passed a numeric string revenue value with more than two decimal places', () => {
const revenue = '100.555';
const formattedRevenue = formatRevenue(revenue);
expect(formattedRevenue).toBe(100.56);
});

// Returns a number with two decimal places when passed a numeric value with more than two decimal places.
it('should return a number with two decimal places when passed a numeric value with more than two decimal places', () => {
const revenue = 100.555;
const formattedRevenue = formatRevenue(revenue);
expect(formattedRevenue).toBe(100.56);
});
});

describe('getCategoryFromEvent', () => {
// The function correctly maps the eventName to its corresponding category.
it('should correctly map the eventName to its corresponding category', () => {
const eventName = CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.type;
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED);
});

// The function returns the correct category for a given eventName.
it('should return the correct category for a given eventName', () => {
const eventName = CONFIG_CATEGORIES.PRODUCT_VIEWED.type;
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.PRODUCT_VIEWED);
});

// The function returns the default category if the eventName is not recognized.
it('should return the default category if the eventName is not recognized', () => {
const eventName = 'unknownEvent';
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.SIMPLE_TRACK);
});

// The function handles null or undefined eventName inputs.
it('should handle null or undefined eventName inputs', () => {
const eventName = null;
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.SIMPLE_TRACK);
});

// The function handles empty string eventName inputs.
it('should handle empty string eventName inputs', () => {
const eventName = '';
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.SIMPLE_TRACK);
});

// The function handles eventName inputs that are not strings.
it('should handle eventName inputs that are not strings', () => {
const eventName = 123;
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.SIMPLE_TRACK);
});

// The function handles multiple eventNames that map to the same category.
it('should correctly map multiple eventNames to the same category', () => {
const eventName1 = CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.type;
const eventName2 = CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED.eventName;
const result1 = getCategoryFromEvent(eventName1);
const result2 = getCategoryFromEvent(eventName2);
expect(result1).toEqual(CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED);
expect(result2).toEqual(CONFIG_CATEGORIES.PRODUCT_LIST_VIEWED);
});

// The function handles eventNames that are included in the OTHER_STANDARD_EVENTS list.
it('should correctly handle eventNames included in the OTHER_STANDARD_EVENTS list', () => {
const eventName = OTHER_STANDARD_EVENTS[0];
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.OTHER_STANDARD);
expect(result.eventName).toEqual(eventName);
});

// The function handles eventNames that are not recognized and not in the OTHER_STANDARD_EVENTS list.
it('should correctly handle unrecognized eventNames', () => {
const eventName = 'unrecognizedEvent';
const result = getCategoryFromEvent(eventName);
expect(result).toEqual(CONFIG_CATEGORIES.SIMPLE_TRACK);
});
});
});
1 change: 1 addition & 0 deletions src/v0/util/facebookUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,5 @@ module.exports = {
transformedPayloadData,
formingFinalResponse,
fetchUserData,
deduceFbcParam,
};
Loading

0 comments on commit caa8d45

Please sign in to comment.