-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: resolve conflicts and update testcase format to similar format
- Loading branch information
Showing
9 changed files
with
220 additions
and
114 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 160 additions & 37 deletions
197
packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,177 @@ | ||
import md5 from 'md5'; | ||
import { handleCommonFields } from '../../../src/integrations/Criteo/utils' | ||
import { handleCommonFields, handleProductAdded } from '../../../src/integrations/Criteo/utils'; | ||
|
||
describe('handleCommonFields', () => { | ||
const inputEvent = { | ||
message: { | ||
userId: 'u1', | ||
anonymousId: 'a1', | ||
properties: { | ||
email: '[email protected]' | ||
} | ||
} | ||
} | ||
email: '[email protected]', | ||
}, | ||
}, | ||
}; | ||
const defaultExpected = [ | ||
{ event: 'setCustomerId', id: md5('u1').toString() }, | ||
{ event: 'setRetailerVisitorId', id: md5('a1').toString() }, | ||
] | ||
const tcs = [ | ||
{ | ||
description: 'when properties.email is present, for md5 hashmethod the relevant mapping should be included', | ||
inputEvent, | ||
hashMethod: 'md5', | ||
expected: [{ | ||
]; | ||
it('when properties.email is present, for md5 hashmethod the relevant mapping should be included', () => { | ||
const out = handleCommonFields(inputEvent, 'md5'); | ||
expect(out).toEqual([ | ||
...defaultExpected, | ||
{ | ||
email: '3f009d72559f51e7e454b16e5d0687a1', | ||
hash_method: 'md5', | ||
event: 'setEmail' | ||
}] | ||
}, | ||
{ | ||
description: 'when properties.email is present, for sha256 hashmethod the relevant mapping should be included', | ||
inputEvent, | ||
hashMethod: 'sha256', | ||
expected: [{ | ||
event: 'setEmail', | ||
}, | ||
]); | ||
}); | ||
it('when properties.email is present, for sha256 hashmethod the relevant mapping should be included', () => { | ||
const output = handleCommonFields(inputEvent, 'sha256'); | ||
expect(output).toEqual([ | ||
...defaultExpected, | ||
{ | ||
email: '48ddb93f0b30c475423fe177832912c5bcdce3cc72872f8051627967ef278e08', | ||
hash_method: 'sha256', | ||
event: 'setEmail' | ||
}] | ||
}, | ||
{ | ||
description: 'when properties.email is present, for random hashmethod the relevant mapping should be included', | ||
inputEvent, | ||
hashMethod: 'random', | ||
expected: [{ | ||
event: 'setEmail', | ||
}, | ||
]); | ||
}); | ||
it('when properties.email is present, for random hashmethod the relevant mapping should be included', () => { | ||
const output = handleCommonFields(inputEvent, 'random'); | ||
expect(output).toEqual([ | ||
...defaultExpected, | ||
{ | ||
email: '[email protected]', | ||
hash_method: 'random', | ||
event: 'setEmail' | ||
}] | ||
}, | ||
] | ||
test.each(tcs)('$description', ({inputEvent, hashMethod, expected}) => { | ||
const out = handleCommonFields(inputEvent, hashMethod) | ||
expect(out).toEqual([...defaultExpected, ...expected]) | ||
}) | ||
}) | ||
event: 'setEmail', | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('handleProductAdded', () => { | ||
// The function correctly extracts the 'properties' object from the 'message' parameter. | ||
it('should correctly extract properties object from message parameter', () => { | ||
const message = { | ||
properties: { | ||
product_id: '123', | ||
price: '9.99', | ||
quantity: '5', | ||
currency: 'USD', | ||
}, | ||
}; | ||
const finalPayload = []; | ||
|
||
handleProductAdded(message, finalPayload); | ||
|
||
expect(finalPayload.length).toBe(1); | ||
expect(finalPayload[0].event).toBe('addToCart'); | ||
expect(finalPayload[0].currency).toBe('USD'); | ||
expect(finalPayload[0].item.length).toBe(1); | ||
expect(finalPayload[0].item[0].id).toBe('123'); | ||
expect(finalPayload[0].item[0].price).toBe(9.99); | ||
expect(finalPayload[0].item[0].quantity).toBe(5); | ||
}); | ||
|
||
// When the 'message' parameter is undefined, the function throws an error. | ||
it('should throw an error when message parameter is undefined', () => { | ||
const message = undefined; | ||
const finalPayload = []; | ||
|
||
expect(() => { | ||
handleProductAdded(message, finalPayload); | ||
}).toThrow(); | ||
}); | ||
|
||
// When the 'properties' object is valid, the function creates an 'addToCartObject' with the correct structure. | ||
it('should create addToCartObject with correct structure when properties object is valid', () => { | ||
const message = { | ||
properties: { | ||
product_id: '123', | ||
price: '9.99', | ||
quantity: '5', | ||
currency: 'USD', | ||
}, | ||
}; | ||
const finalPayload = []; | ||
|
||
handleProductAdded(message, finalPayload); | ||
|
||
expect(finalPayload.length).toBe(1); | ||
expect(finalPayload[0].event).toBe('addToCart'); | ||
expect(finalPayload[0].currency).toBe('USD'); | ||
expect(finalPayload[0].item.length).toBe(1); | ||
expect(finalPayload[0].item[0].id).toBe('123'); | ||
expect(finalPayload[0].item[0].price).toBe(9.99); | ||
expect(finalPayload[0].item[0].quantity).toBe(5); | ||
}); | ||
|
||
// When the product is valid, the function adds the product object to the 'item' property of the 'addToCartObject'. | ||
it("should add product object to 'item' property when product is valid", () => { | ||
const message = { | ||
properties: { | ||
product_id: '123', | ||
price: '9.99', | ||
quantity: '5', | ||
currency: 'USD', | ||
}, | ||
}; | ||
const finalPayload = []; | ||
|
||
handleProductAdded(message, finalPayload); | ||
|
||
expect(finalPayload.length).toBe(1); | ||
expect(finalPayload[0].event).toBe('addToCart'); | ||
expect(finalPayload[0].currency).toBe('USD'); | ||
expect(finalPayload[0].item.length).toBe(1); | ||
expect(finalPayload[0].item[0].id).toBe('123'); | ||
expect(finalPayload[0].item[0].price).toBe(9.99); | ||
expect(finalPayload[0].item[0].quantity).toBe(5); | ||
}); | ||
|
||
// The function correctly pushes the 'addToCartObject' to the 'finalPayload' array. | ||
it("should correctly push 'addToCartObject' to 'finalPayload' array", () => { | ||
const message = { | ||
properties: { | ||
product_id: '123', | ||
price: '9.99', | ||
quantity: '5', | ||
currency: 'USD', | ||
}, | ||
}; | ||
const finalPayload = []; | ||
|
||
handleProductAdded(message, finalPayload); | ||
|
||
expect(finalPayload.length).toBe(1); | ||
expect(finalPayload[0].event).toBe('addToCart'); | ||
expect(finalPayload[0].currency).toBe('USD'); | ||
expect(finalPayload[0].item.length).toBe(1); | ||
expect(finalPayload[0].item[0].id).toBe('123'); | ||
expect(finalPayload[0].item[0].price).toBe(9.99); | ||
expect(finalPayload[0].item[0].quantity).toBe(5); | ||
}); | ||
|
||
// When the 'properties' object is missing the 'currency' property, the function creates an 'addToCartObject' without the 'currency' property. | ||
it("should create 'addToCartObject' without 'currency' property when 'properties' object is missing 'currency'", () => { | ||
const message = { | ||
properties: { | ||
product_id: '123', | ||
price: '9.99', | ||
quantity: '5', | ||
}, | ||
}; | ||
const finalPayload = []; | ||
|
||
handleProductAdded(message, finalPayload); | ||
|
||
expect(finalPayload.length).toBe(1); | ||
expect(finalPayload[0].event).toBe('addToCart'); | ||
expect(finalPayload[0].currency).toBeUndefined(); | ||
expect(finalPayload[0].item.length).toBe(1); | ||
expect(finalPayload[0].item[0].id).toBe('123'); | ||
expect(finalPayload[0].item[0].price).toBe(9.99); | ||
expect(finalPayload[0].item[0].quantity).toBe(5); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.