Skip to content

Commit

Permalink
chore: fix codecov sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Feb 20, 2025
1 parent e35a628 commit 68b7f52
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
68 changes: 60 additions & 8 deletions src/v1/sources/shopify/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const {
updateAnonymousIdToUserIdInRedis,
} = require('./utils');
const { RedisDB } = require('../../../util/redis/redisConnector');
const stats = require('../../../util/stats');

jest.mock('../../../util/stats', () => ({
increment: jest.fn(),
}));

describe('Identifier Utils Tests', () => {
describe('test isIdentifierEvent', () => {
Expand All @@ -23,7 +28,7 @@ describe('Identifier Utils Tests', () => {
jest.clearAllMocks();
});

it('should set the anonymousId in redis and return NO_OPERATION_SUCCESS', async () => {
it('should set the cartToken mapping in redis and increment stats', async () => {
const setValSpy = jest.spyOn(RedisDB, 'setVal').mockResolvedValue('OK');
const event = {
cartToken: 'cartTokenTest1',
Expand All @@ -38,6 +43,34 @@ describe('Identifier Utils Tests', () => {
['anonymousId', 'anonymousIdTest1'],
43200,
);
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_cart_token_mapping', {
action: 'stitchCartTokenToAnonId',
operation: 'set',
});
expect(response).toEqual({
outputToSource: {
body: Buffer.from('OK').toString('base64'),
contentType: 'text/plain',
},
statusCode: 200,
});
});

it('should update the anonymousId to userId mapping in redis and increment stats', async () => {
const setValSpy = jest.spyOn(RedisDB, 'setVal').mockResolvedValue('OK');
const event = {
anonymousId: 'anonymousIdTest1',
userId: 'userIdTest1',
action: 'stitchUserIdToAnonId',
};

const response = await processIdentifierEvent(event);

expect(setValSpy).toHaveBeenCalled();
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_userid_mapping', {
action: 'stitchUserIdToAnonId',
operation: 'set',
});
expect(response).toEqual({
outputToSource: {
body: Buffer.from('OK').toString('base64'),
Expand All @@ -47,15 +80,17 @@ describe('Identifier Utils Tests', () => {
});
});

it('should handle redis errors', async () => {
jest.spyOn(RedisDB, 'setVal').mockRejectedValue(new Error('Redis connection failed'));
it('should handle redis errors and increment error stats', async () => {
const error = new Error('Redis connection failed');
jest.spyOn(RedisDB, 'setVal').mockRejectedValue(error);
const event = {
cartToken: 'cartTokenTest1',
anonymousId: 'anonymousIdTest1',
action: 'stitchCartTokenToAnonId',
};

await expect(processIdentifierEvent(event)).rejects.toThrow('Redis connection failed');
expect(stats.increment).not.toHaveBeenCalled();
});
});

Expand All @@ -64,33 +99,50 @@ describe('Identifier Utils Tests', () => {
jest.clearAllMocks();
});

it('should update the anonymousId to userId in redis', async () => {
it('should update the anonymousId to userId in redis and increment stats', async () => {
const setValSpy = jest.spyOn(RedisDB, 'setVal').mockResolvedValue('OK');
const event = {
cartToken: 'cartTokenTest1',
anonymousId: 'anonymousTest1',
userId: 'userIdTest1',
action: 'stitchUserIdToAnonId',
};

await updateAnonymousIdToUserIdInRedis(event.anonymousId, event.userId);

expect(setValSpy).toHaveBeenCalledWith(
'pixel:anonymousTest1',
['userId', 'userIdTest1'],
86400,
);
expect(stats.increment).toHaveBeenCalledWith('shopify_pixel_userid_mapping', {
action: 'stitchUserIdToAnonId',
operation: 'set',
});
});

it('should handle null values', async () => {
it('should handle redis errors in updateAnonymousIdToUserIdInRedis', async () => {
const error = new Error('Redis connection failed');
jest.spyOn(RedisDB, 'setVal').mockRejectedValue(error);
const event = {
anonymousId: 'anonymousTest1',
userId: 'userIdTest1',
};

await expect(
updateAnonymousIdToUserIdInRedis(event.anonymousId, event.userId),
).rejects.toThrow('Redis connection failed');
expect(stats.increment).not.toHaveBeenCalled();
});

it('should handle null values and not call Redis or stats', async () => {
const setValSpy = jest.spyOn(RedisDB, 'setVal').mockResolvedValue('OK');
const event = {
cartToken: 'cartTokenTest1',
anonymousId: null,
userId: null,
};

await updateAnonymousIdToUserIdInRedis(event.anonymousId, event.userId);
expect(setValSpy).not.toHaveBeenCalled();
expect(stats.increment).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file contains the test scenarios related to Shopify pixel events, emitted from web pixel on the browser.
import { mockFns } from '../mocks';
import {
dummyContext,
dummyContextwithCampaign,
Expand Down Expand Up @@ -76,6 +77,7 @@ export const pixelEventsTestScenarios = [
},
name: 'Page View',
type: 'page',
userId: 'test-user-id',
properties: {},
anonymousId: 'c7b3f99b-4d34-463b-835f-c879482a7750',
messageId: 'sh-f6b6f548-5FEF-4DAE-9CAB-39EE6F94E09B',
Expand Down Expand Up @@ -191,6 +193,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Product Viewed',
properties: {
product_id: '7234590834801',
Expand Down Expand Up @@ -360,6 +363,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Cart Viewed',
properties: {
products: [
Expand Down Expand Up @@ -590,6 +594,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Product List Viewed',
properties: {
cart_id: 'c7b3f99b-4d34-463b-835f-c879482a7750',
Expand Down Expand Up @@ -765,6 +770,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Product Added',
properties: {
image_url:
Expand Down Expand Up @@ -911,6 +917,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Product Removed',
properties: {
image_url:
Expand Down Expand Up @@ -1005,6 +1012,7 @@ export const pixelEventsTestScenarios = [
},
},
type: 'track',
userId: 'test-user-id',
event: 'Search Submitted',
properties: {
query: 'skate',
Expand Down Expand Up @@ -1070,4 +1078,4 @@ export const pixelEventsTestScenarios = [
},
},
},
];
].map((p1) => ({ ...p1, mockFns }));

0 comments on commit 68b7f52

Please sign in to comment.