Skip to content

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

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

Merged
merged 20 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
1,217 changes: 616 additions & 601 deletions package-lock.json

Large diffs are not rendered by default.

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();
});
});
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([]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,28 @@ afterAll(() => {
describe('constructor', () => {
it('should set the log level if provided', () => {
const config = { appKey: 'APP_KEY', logLevel: 'debug' };
const analytics = {};
const analytics = {
logLevel: '0',
};
const destinationInfo = {};
const braze = new Braze(config, analytics, destinationInfo);
expect(braze.trackAnonymousUser).toEqual(undefined);
expect(braze.enableBrazeLogging).toEqual(false);
expect(braze.allowUserSuppliedJavascript).toEqual(false);
expect(braze.appKey).toEqual('APP_KEY');
});

it('should set the log level if provided', () => {
const config = { logLevel: 'debug', dataCenter: 'eu' };
const analytics = {
logLevel: '0',
};
const destinationInfo = {};
const braze = new Braze(config, analytics, destinationInfo);
expect(braze.trackAnonymousUser).toEqual(undefined);
expect(braze.enableBrazeLogging).toEqual(false);
expect(braze.allowUserSuppliedJavascript).toEqual(false);
expect(braze.appKey).toEqual('');
});
// Add more tests for the constructor if needed
});

Expand All @@ -94,8 +107,11 @@ describe('init', () => {
enableBrazeLogging: false,
dataCenter: 'US-03',
allowUserSuppliedJavascript: false,
enablePushNotification: true,
};
const analytics = {
userId: '1234',
};
const analytics = {};
const destinationInfo = {};

const braze = new Braze(config, analytics, destinationInfo);
Expand All @@ -113,6 +129,30 @@ describe('init', () => {
// Add more tests for the init method if needed
});

describe('isLoaded', () => {
it('should get false value with isLoaded', () => {
const config = {};
const analytics = {};
const destinationInfo = {};

const braze = new Braze(config, analytics, destinationInfo);
const isLoaded = braze.isLoaded();
expect(isLoaded).toBe(false);
});
});

describe('isLoaded', () => {
it('should get false value with isReady', () => {
const config = {};
const analytics = {};
const destinationInfo = {};

const braze = new Braze(config, analytics, destinationInfo);
const isLoaded = braze.isReady();
expect(isLoaded).toBe(false);
});
});

describe('identify', () => {
it('should call the necessary Braze methods to set user attributes', () => {
const config = {
Expand All @@ -139,7 +179,7 @@ describe('identify', () => {
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
gender: 'male',
gender: 1,
phone: '1234567890',
address: {
country: 'USA',
Expand Down Expand Up @@ -169,7 +209,7 @@ describe('identify', () => {
expect(window.braze.getUser().setEmail).toHaveBeenCalledWith('[email protected]');
expect(window.braze.getUser().setFirstName).toHaveBeenCalledWith('John');
expect(window.braze.getUser().setLastName).toHaveBeenCalledWith('Doe');
expect(window.braze.getUser().setGender).toHaveBeenCalledWith('m');
expect(window.braze.getUser().setGender).toHaveBeenCalledWith(undefined);
expect(window.braze.getUser().setPhoneNumber).toHaveBeenCalledWith('1234567890');
expect(window.braze.getUser().setCountry).toHaveBeenCalledWith('USA');
expect(window.braze.getUser().setHomeCity).toHaveBeenCalledWith('New York');
Expand Down Expand Up @@ -201,6 +241,7 @@ describe('identify', () => {
userId: 'user123',
context: {
traits: {
customTrait: 'random data',
email: '[email protected]',
firstName: 'David',
lastName: 'Doe',
Expand Down Expand Up @@ -244,6 +285,7 @@ describe('identify', () => {
jest.spyOn(window.braze.getUser(), 'setCountry');
jest.spyOn(window.braze.getUser(), 'setHomeCity');
jest.spyOn(window.braze.getUser(), 'setDateOfBirth');
jest.spyOn(window.braze.getUser(), 'setCustomUserAttribute');

// Call the identify method
braze.identify(rudderElement);
Expand All @@ -256,6 +298,10 @@ describe('identify', () => {
expect(window.braze.getUser().setCountry).toHaveBeenCalledWith('USA');
expect(window.braze.getUser().setHomeCity).toHaveBeenCalledWith('Austin');
expect(window.braze.getUser().setDateOfBirth).not.toHaveBeenCalled();
expect(window.braze.getUser().setCustomUserAttribute).toHaveBeenCalledWith(
'customTrait',
'random data',
);
// Expect any other necessary Braze methods to be called

// Expect Storage.setItem to be called with the updated payload
Expand All @@ -273,6 +319,7 @@ describe('identify', () => {
city: 'Austin',
},
birthday: '1990-01-01',
customTrait: 'random data',
},
},
});
Expand Down Expand Up @@ -968,7 +1015,7 @@ describe('hybrid mode', () => {
dataCenter: 'US-03',
enableHtmlInAppMessages: false,
allowUserSuppliedJavascript: false,
connectionMode: 'hybrid'
connectionMode: 'hybrid',
};
const analytics = {};
const destinationInfo = {};
Expand All @@ -982,11 +1029,11 @@ describe('hybrid mode', () => {
const rudderElement = {
message: {
userId: 'user123',
type: "page",
name: "Home",
type: 'page',
name: 'Home',
properties: {
title: "Home | RudderStack",
url: "http://www.rudderstack.com"
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
},
},
};
Expand All @@ -1006,7 +1053,7 @@ describe('hybrid mode', () => {
dataCenter: 'US-03',
enableHtmlInAppMessages: false,
allowUserSuppliedJavascript: false,
connectionMode: 'hybrid'
connectionMode: 'hybrid',
};
const analytics = {};
const destinationInfo = {};
Expand All @@ -1020,11 +1067,11 @@ describe('hybrid mode', () => {
const rudderElement = {
message: {
userId: 'user123',
type: "page",
name: "Home",
type: 'page',
name: 'Home',
properties: {
title: "Home | RudderStack",
url: "http://www.rudderstack.com"
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
},
},
};
Expand All @@ -1044,7 +1091,7 @@ describe('hybrid mode', () => {
dataCenter: 'US-03',
enableHtmlInAppMessages: false,
allowUserSuppliedJavascript: false,
connectionMode: 'hybrid'
connectionMode: 'hybrid',
};
const analytics = {};
const destinationInfo = {};
Expand All @@ -1058,11 +1105,11 @@ describe('hybrid mode', () => {
const rudderElement = {
message: {
userId: 'user123',
type: "page",
name: "Home",
type: 'page',
name: 'Home',
properties: {
title: "Home | RudderStack",
url: "http://www.rudderstack.com"
title: 'Home | RudderStack',
url: 'http://www.rudderstack.com',
},
},
};
Expand All @@ -1073,5 +1120,4 @@ describe('hybrid mode', () => {
// Expect the necessary Braze methods to be called with the correct values
expect(window.braze.changeUser).toBeCalledTimes(0);
});

});
Loading