Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fb pixel test case refactor #3075

Merged
merged 18 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
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
28 changes: 28 additions & 0 deletions src/v0/destinations/facebook_pixel/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ const getTestMessage = () => {
return message;
};

const getTestMessageWithoutProductIdAndCategory = () => {
let message = {
properties: {
currency: 'CAD',
quantity: 1,
price: 24.75,
value: 30,
name: 'my product 1',
testDimension: true,
testMetric: true,
position: 4.5,
query: 'HDMI Cable',
},
};
return message;
};

const getTestCategoryToContent = () => {
let categoryToContent = [
{
Expand Down Expand Up @@ -52,6 +69,17 @@ describe('Unit test cases for facebook_pixel handle search', () => {
expect(handleSearch(getTestMessage())).toEqual(expectedOutput);
});

it('should return content with content_ids and content fields as empty array', async () => {
const expectedOutput = {
content_ids: [],
content_category: '',
value: 30,
search_string: 'HDMI Cable',
contents: [],
};
expect(handleSearch(getTestMessageWithoutProductIdAndCategory())).toEqual(expectedOutput);
});

it("mapping 'product_id' with contentId", async () => {
let message = getTestMessage();
message.properties.product_id = 'prd-123';
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) {
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading