Skip to content

Commit

Permalink
Merge branch 'develop' into feat.amplitudeSkipTraits
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip authored Mar 14, 2024
2 parents c0ad214 + cff7d1c commit 2fdbb7a
Show file tree
Hide file tree
Showing 12 changed files with 3,933 additions and 6,563 deletions.
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
154 changes: 154 additions & 0 deletions src/v0/destinations/facebook_pixel/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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);
});
});

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 2fdbb7a

Please sign in to comment.