-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for identity stitching for shopify pixel flow (#3818)
- Loading branch information
1 parent
9174897
commit 1fccbcb
Showing
5 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
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
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
116 changes: 116 additions & 0 deletions
116
src/v1/sources/shopify/pixelTransform.redisCartToken.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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const { extractCartToken, handleCartTokenRedisOperations } = require('./pixelTransform'); | ||
const { RedisDB } = require('../../../util/redis/redisConnector'); | ||
const stats = require('../../../util/stats'); | ||
const logger = require('../../../logger'); | ||
const { pixelEventToCartTokenLocationMapping } = require('./config'); | ||
|
||
jest.mock('../../../util/redis/redisConnector', () => ({ | ||
RedisDB: { | ||
setVal: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock('../../../util/stats', () => ({ | ||
increment: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../logger', () => ({ | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
})); | ||
|
||
jest.mock('./config', () => ({ | ||
pixelEventToCartTokenLocationMapping: { cart_viewed: 'properties.cart_id' }, | ||
})); | ||
|
||
describe('extractCartToken', () => { | ||
it('should return undefined if cart token location is not found', () => { | ||
const inputEvent = { name: 'unknownEvent', query_parameters: { writeKey: 'testWriteKey' } }; | ||
|
||
const result = extractCartToken(inputEvent); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_cart_token_not_found', { | ||
event: 'unknownEvent', | ||
writeKey: 'testWriteKey', | ||
}); | ||
}); | ||
|
||
it('should return undefined if cart token is not a string', () => { | ||
const inputEvent = { | ||
name: 'cart_viewed', | ||
properties: { cart_id: 12345 }, | ||
query_parameters: { writeKey: 'testWriteKey' }, | ||
}; | ||
|
||
const result = extractCartToken(inputEvent); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(logger.error).toHaveBeenCalledWith('Cart token is not a string'); | ||
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_cart_token_not_found', { | ||
event: 'cart_viewed', | ||
writeKey: 'testWriteKey', | ||
}); | ||
}); | ||
|
||
it('should return the cart token if it is a valid string', () => { | ||
const inputEvent = { | ||
name: 'cart_viewed', | ||
properties: { cart_id: '/checkout/cn/1234' }, | ||
query_parameters: { writeKey: 'testWriteKey' }, | ||
}; | ||
|
||
const result = extractCartToken(inputEvent); | ||
|
||
expect(result).toBe('1234'); | ||
}); | ||
}); | ||
|
||
describe('handleCartTokenRedisOperations', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should handle undefined or null cart token gracefully', async () => { | ||
const inputEvent = { | ||
name: 'unknownEvent', | ||
query_parameters: { | ||
writeKey: 'testWriteKey', | ||
}, | ||
}; | ||
const clientId = 'testClientId'; | ||
|
||
await handleCartTokenRedisOperations(inputEvent, clientId); | ||
|
||
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_cart_token_not_found', { | ||
event: 'unknownEvent', | ||
writeKey: 'testWriteKey', | ||
}); | ||
}); | ||
|
||
it('should log error and increment stats when exception occurs', async () => { | ||
const inputEvent = { | ||
name: 'cart_viewed', | ||
properties: { | ||
cart_id: '/checkout/cn/1234', | ||
}, | ||
query_parameters: { | ||
writeKey: 'testWriteKey', | ||
}, | ||
}; | ||
const clientId = 'testClientId'; | ||
const error = new Error('Redis error'); | ||
RedisDB.setVal.mockRejectedValue(error); | ||
|
||
await handleCartTokenRedisOperations(inputEvent, clientId); | ||
|
||
expect(logger.error).toHaveBeenCalledWith( | ||
'Error handling Redis operations for event: cart_viewed', | ||
error, | ||
); | ||
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_cart_token_redis_error', { | ||
event: 'cart_viewed', | ||
writeKey: 'testWriteKey', | ||
}); | ||
}); | ||
}); |