Skip to content

Commit 0b06a90

Browse files
committed
chore: updated testcases and function
1 parent 41d9ea1 commit 0b06a90

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed

packages/analytics-js-integrations/__tests__/integrations/XPixel/browser.test.js

+96-2
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,113 @@ const basicConfig = {
1212
pixelId: '12567839',
1313
eventToEventIdMap: [
1414
{ from: 'Sign Up', to: '123' },
15-
{ to: 'Lead', from: '1234' },
15+
{ from: 'Custom', to: '987' },
1616
{ from: 'Page View', to: '456' },
1717
{ from: 'Page View', to: '467' },
1818
{ from: 'product_added', to: '789' },
1919
],
2020
};
2121

2222
describe('XPixel init tests', () => {
23+
beforeAll(() => {
24+
// Add a dummy script as it is required by the init script
25+
const scriptElement = document.createElement('script');
26+
scriptElement.type = 'text/javascript';
27+
scriptElement.id = 'dummyScript';
28+
const headElements = document.getElementsByTagName('head');
29+
headElements[0].insertBefore(scriptElement, headElements[0].firstChild);
30+
});
31+
2332
let xPixel;
2433

2534
test('Testing init call of XPixel', () => {
2635
xPixel = new XPixel(basicConfig, { loglevel: 'debug' }, destinationInfo);
2736
xPixel.init();
28-
expect(typeof window.twq).toBe('object');
37+
expect(typeof window.twq).toBe('function');
38+
});
39+
});
40+
41+
describe('xPixel page', () => {
42+
let xPixel;
43+
beforeEach(() => {
44+
xPixel = new XPixel(basicConfig, { loglevel: 'debug' });
45+
xPixel.init();
46+
window.twq = jest.fn();
47+
});
48+
49+
test('send pageview', () => {
50+
xPixel.page({
51+
message: {
52+
context: {},
53+
properties: {
54+
category: 'test cat',
55+
path: '/test',
56+
url: 'http://localhost',
57+
referrer: '',
58+
title: 'test page',
59+
testDimension: 'abc',
60+
value: 35.0,
61+
currency: 'GBP',
62+
},
63+
},
64+
});
65+
expect(window.twq.mock.calls[0]).toEqual(['event', '456', { currency: 'GBP', value: 35 }]);
66+
expect(window.twq.mock.calls[1]).toEqual(['event', '467', { currency: 'GBP', value: 35 }]);
67+
});
68+
});
69+
70+
describe('XPixel Track event', () => {
71+
let xPixel;
72+
beforeEach(() => {});
73+
test('Testing Track Simple Event', () => {
74+
xPixel = new XPixel(basicConfig, { loglevel: 'DEBUG' }, destinationInfo);
75+
xPixel.init();
76+
window.twq = jest.fn();
77+
xPixel.track({
78+
message: {
79+
context: {},
80+
event: 'Custom',
81+
properties: {
82+
customProp: 'testProp',
83+
order_id: 'transactionId',
84+
value: 35.0,
85+
coupon: 'APPARELSALE',
86+
currency: 'GBP',
87+
products: [
88+
{
89+
customPropProd: 'testPropProd',
90+
product_id: 'abc',
91+
category: 'Merch',
92+
name: 'Food/Drink',
93+
brand: '',
94+
variant: 'Extra topped',
95+
price: 3.0,
96+
quantity: 2,
97+
currency: 'GBP',
98+
position: 1,
99+
value: 6.0,
100+
typeOfProduct: 'Food',
101+
},
102+
],
103+
},
104+
},
105+
});
106+
expect(window.twq.mock.calls[0]).toEqual([
107+
'event',
108+
'987',
109+
{
110+
currency: 'GBP',
111+
value: 35,
112+
contents: [
113+
{
114+
content_type: 'product',
115+
content_id: 'abc',
116+
content_name: 'Food/Drink',
117+
price: 3,
118+
num_items: 2,
119+
},
120+
],
121+
},
122+
]);
29123
});
30124
});

packages/analytics-js-integrations/src/integrations/XPixel/browser.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,21 @@ class XPixel {
4444
return;
4545
}
4646
const properties = getTrackResponse(rudderElement.message);
47-
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventsToStandard);
47+
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventToEventIdMap);
4848
const eventIds = standardEventsMap[event?.toLowerCase()];
49-
eventIds.forEach(eventId => {
50-
window.twq('event', eventId, properties);
51-
});
49+
if (Array.isArray(eventIds)) {
50+
eventIds.forEach(eventId => {
51+
window.twq('event', eventId, properties);
52+
});
53+
} else {
54+
logger.error(`Event name (${event}) is not valid, must be mapped to atleast one Event ID`);
55+
}
5256
}
5357

5458
page(rudderElement) {
5559
const event = 'Page View';
5660
const properties = getTrackResponse(rudderElement.message);
57-
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventsToStandard);
61+
const standardEventsMap = getHashFromArrayWithDuplicate(this.eventToEventIdMap);
5862
const eventIds = standardEventsMap[event?.toLowerCase()];
5963
eventIds.forEach(eventId => {
6064
window.twq('event', eventId, properties);

0 commit comments

Comments
 (0)