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

chore: migrate all lodash usages with ramda in web device mode integrations #1684

Merged
merged 20 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 1 addition & 32 deletions package-lock.json

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import {
getDataFromContext,
handleLists,
setConfig,
mapMerchProductEvents,
} from '../../../src/integrations/AdobeAnalytics/util';

let windowSpy;
beforeEach(() => {
windowSpy = jest.spyOn(window, 'window', 'get');
});

afterEach(() => {
windowSpy.mockRestore();
});
describe('AdobeAnalytics Utility functions tests', () => {
describe('getDataFromContext Tests', () => {
it('should return an empty object when contextMap is empty', () => {
const contextMap = {};
const rudderElement = { message: {} };
const result = getDataFromContext(contextMap, rudderElement);
expect(result).toEqual({});
});
it('should return an object with values mapped from context and properties of rudder message', () => {
const contextMap = {
'page.name': 'pName',
'page.url': 'pUrl',
'page.arr.0.name': 'p0Name',
};
const rudderElement = {
message: {
context: {
page: {
name: 'Home Page',
url: 'https://example.com',
arr: [{ name: 'arrName' }],
},
path: '/page1',
},
properties: {
property1: 'value1',
property2: 'value2',
},
},
};
const result = getDataFromContext(contextMap, rudderElement);
expect(result).toEqual({
pName: 'Home Page',
pUrl: 'https://example.com',
p0Name: 'arrName',
});
});
it('should map values from top level properties of rudder message to keys specified in contextMap', () => {
const contextMap = { anonymousId: 'aId', userId: 'uId' };
const rudderElement = {
message: {
anonymousId: '12345',
userId: '67890',
properties: {
property1: 'value1',
property2: 'value2',
userId: '67890',
},
},
};
const result = getDataFromContext(contextMap, rudderElement);
expect(result).toEqual({
aId: '12345',
uId: '67890',
});
});
});
describe('handleLists Tests', () => {
// Sets list variable of window.s with correct delimiter and list name
it('should set list variable of window.s with correct delimiter and list name when mapping and delimiter are present', () => {
const rudderElement = {
message: {
properties: {
key1: 'value1',
key2: 'value2',
},
},
};
windowSpy.mockImplementation(() => ({
s: { list1: 'value1', list2: 'value2' },
}));
const config = {
listMapping: [
{ from: 'key1', to: 'list1', delimiter: ',' },
{ from: 'key2', to: 'list2', delimiter: ';' },
],
};
setConfig(config);
handleLists(rudderElement);

expect(window.s.list1).toBe('value1');
expect(window.s.list2).toBe('value2');
});
// Skips list variable update if mapping or delimiter is missing
it('should skip list variable update if mapping or delimiter is missing', () => {
const rudderElement = {
message: {
properties: {
key1: 'value1',
key2: 'value2',
},
},
};
windowSpy.mockImplementation(() => ({
s: { list1: 'value1', list2: undefined },
}));
const config = {
listMapping: [
{ from: 'key1', to: 'list1', delimiter: ',' },
{ from: 'key3', to: 'list3', delimiter: ';' },
],
};
setConfig(config);

handleLists(rudderElement);

expect(window.s.list1).toBe('value1');
expect(window.s.list2).toBeUndefined();
});
ItsSudip marked this conversation as resolved.
Show resolved Hide resolved
});
describe('mapMerchProductEvents', () => {
// Should return an empty array when productMerchEventToAdobeEventHashmap does not contain the event
it('should return an empty array when productMerchEventToAdobeEventHashmap does not contain the event', () => {
const event = 'testEvent';
const properties = {
currencyProdMerch: 'someRandomData',
prodLevelCurrency: 'some RandomCurrency',
};
const adobeEvent = 'testAdobeEvent';
const config = {
productMerchEventToAdobeEvent: [
{ from: 'key1', to: 'list1', delimiter: ',' },
{ from: 'testEvent', to: 'Test Adobe Event', delimiter: ';' },
],
productMerchProperties: [
{
productMerchProperties: 'currencyProdMerch',
},
{
productMerchProperties: 'addressProdMerch',
},
{
productMerchProperties: 'products.prodLevelCurrency',
},
],
productIdentifier: 'id',
};
setConfig(config);
const result = mapMerchProductEvents(event, properties, adobeEvent);

expect(result).toEqual([
'testAdobeEvent=someRandomData',
'testAdobeEvent=some RandomCurrency',
]);
});
// Should handle gracefully when productMerchProperties is undefined
it('should handle gracefully when productMerchProperties is undefined', () => {
const event = 'testEvent';
const properties = {};
const adobeEvent = 'testAdobeEvent';
const config = {
productMerchEventToAdobeEvent: {
testevent: 'testadobeevent',
},
productMerchProperties: undefined,
productIdentifier: 'id',
};
setConfig(config);
const result = mapMerchProductEvents(event, properties, adobeEvent);

expect(result).toEqual([]);
});
ItsSudip marked this conversation as resolved.
Show resolved Hide resolved
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,20 @@ describe('isDefinedAndNotNullAndNotEmpty Tests', () => {
const result = utils.isDefinedAndNotNullAndNotEmpty(testVar);
expect(result).toStrictEqual(false);
});
test('integer returns false', () => {
test('integer returns true', () => {
const testVar = 124;
const result = utils.isDefinedAndNotNullAndNotEmpty(testVar);
expect(result).toStrictEqual(false);
expect(result).toStrictEqual(true);
});
test('true returns false', () => {
test('true returns true', () => {
const testVar = true;
const result = utils.isDefinedAndNotNullAndNotEmpty(testVar);
expect(result).toStrictEqual(false);
expect(result).toStrictEqual(true);
});
test('false returns false', () => {
test('false returns true', () => {
const testVar = false;
const result = utils.isDefinedAndNotNullAndNotEmpty(testVar);
expect(result).toStrictEqual(false);
expect(result).toStrictEqual(true);
});
});

Expand Down
19 changes: 6 additions & 13 deletions packages/analytics-js-integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,16 @@
"build:integration:all:bundle-size:modern": "VISUALIZER=true npm run build:integration:all:modern"
},
"dependencies": {
"@rudderstack/analytics-js-common": "*",
"@ndhoule/each": "2.0.1",
"lodash.get": "4.4.2",
"lodash.isequal": "4.5.0",
"obj-case": "0.2.1",
"on-body": "0.0.1",
"md5": "2.3.0",
"@ndhoule/extend": "2.0.0",
"@rudderstack/analytics-js-common": "*",
"component-each": "0.2.6",
"crypto-js": "4.2.0",
"is": "3.3.0",
"component-each": "0.2.6",
"@ndhoule/extend": "2.0.0",
"lodash.isundefined": "3.0.1",
"lodash.isempty": "4.4.0",
"lodash.pickby": "4.6.0",
"lodash.tostring": "4.1.4"
"md5": "2.3.0",
"obj-case": "0.2.1",
"on-body": "0.0.1"
},
"devDependencies": {},
"overrides": {},
"browserslist": {
"production": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-undef */
import get from 'lodash.get';
import each from '@ndhoule/each';
import { isDefined } from '@rudderstack/analytics-js-common/utilities/checks';
ItsSudip marked this conversation as resolved.
Show resolved Hide resolved
import { DISPLAY_NAME } from '@rudderstack/analytics-js-common/constants/integrations/AdobeAnalytics/constants';
import { path } from 'ramda';
import Logger from '../../utils/logger';
import {
toIso,
getHashFromArray,
isDefinedAndNotNullAndNotEmpty,
isDefined,
} from '../../utils/commonUtils';
import { toIso, getHashFromArray, isDefinedAndNotNullAndNotEmpty } from '../../utils/commonUtils';


const get = (context, value) => path(value.split('.'), context);
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
ItsSudip marked this conversation as resolved.
Show resolved Hide resolved
const logger = new Logger(DISPLAY_NAME);

let dynamicKeys = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable class-methods-use-this */
import isEqual from 'lodash.isequal';
import { isEmpty } from 'ramda';
import { isEmpty, equals } from 'ramda';
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
import {
NAME,
DISPLAY_NAME,
Expand Down Expand Up @@ -199,17 +198,17 @@ class Braze {

if (email && email !== prevEmail) setEmail();
if (phone && phone !== prevPhone) setPhone();
if (birthday && !isEqual(birthday, prevBirthday)) setBirthday();
if (birthday && !equals(birthday, prevBirthday)) setBirthday();
if (firstName && firstName !== prevFirstname) setFirstName();
if (lastName && lastName !== prevLastname) setLastName();
if (gender && formatGender(gender) !== formatGender(prevGender))
setGender(formatGender(gender));
if (address && !isEqual(address, prevAddress)) setAddress();
if (address && !equals(address, prevAddress)) setAddress();
if (isObject(traits)) {
Object.keys(traits)
.filter(key => reserved.indexOf(key) === -1)
.forEach(key => {
if (!prevTraits[key] || !isEqual(prevTraits[key], traits[key])) {
if (!prevTraits[key] || !equals(prevTraits[key], traits[key])) {
window.braze.getUser().setCustomUserAttribute(key, traits[key]);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/FacebookPixel/constants';
import { isDefined } from '@rudderstack/analytics-js-common/utilities/checks';
import Logger from '../../utils/logger';
import { isDefined } from '../../utils/commonUtils';

const logger = new Logger(DISPLAY_NAME);

Expand Down
Loading