Skip to content

Commit

Permalink
chore: updates to facilitate utils mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Sai Sankeerth committed Nov 7, 2024
1 parent 594f275 commit 19ba143
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 28 deletions.
22 changes: 8 additions & 14 deletions src/v0/sources/shopify/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
const { v5 } = require('uuid');
const sha256 = require('sha256');
const { TransformationError } = require('@rudderstack/integrations-lib');
const { TransformationError, isDefinedAndNotNull } = require('@rudderstack/integrations-lib');
const stats = require('../../../util/stats');
const {
constructPayload,
extractCustomFields,
flattenJson,
generateUUID,
isDefinedAndNotNull,
} = require('../../util');
const utils = require('../../util');
const { RedisDB } = require('../../../util/redis/redisConnector');
const {
lineItemsMappingJSON,
Expand Down Expand Up @@ -92,8 +86,8 @@ const getProductsListFromLineItems = (lineItems) => {
}
const products = [];
lineItems.forEach((lineItem) => {
const product = constructPayload(lineItem, lineItemsMappingJSON);
extractCustomFields(lineItem, product, 'root', LINE_ITEM_EXCLUSION_FIELDS);
const product = utils.constructPayload(lineItem, lineItemsMappingJSON);
utils.extractCustomFields(lineItem, product, 'root', LINE_ITEM_EXCLUSION_FIELDS);
product.variant = getVariantString(lineItem);
products.push(product);
});
Expand All @@ -103,14 +97,14 @@ const getProductsListFromLineItems = (lineItems) => {
const createPropertiesForEcomEvent = (message) => {
const { line_items: lineItems } = message;
const productsList = getProductsListFromLineItems(lineItems);
const mappedPayload = constructPayload(message, productMappingJSON);
extractCustomFields(message, mappedPayload, 'root', PRODUCT_MAPPING_EXCLUSION_FIELDS);
const mappedPayload = utils.constructPayload(message, productMappingJSON);
utils.extractCustomFields(message, mappedPayload, 'root', PRODUCT_MAPPING_EXCLUSION_FIELDS);
mappedPayload.products = productsList;
return mappedPayload;
};

const extractEmailFromPayload = (event) => {
const flattenedPayload = flattenJson(event);
const flattenedPayload = utils.flattenJson(event);
let email;
const regex_email = /\bemail\b/i;
Object.entries(flattenedPayload).some(([key, value]) => {
Expand Down Expand Up @@ -182,7 +176,7 @@ const getAnonymousIdAndSessionId = async (message, metricMetadata, redisData = n
return { anonymousId, sessionId };
}
return {
anonymousId: isDefinedAndNotNull(anonymousId) ? anonymousId : generateUUID(),
anonymousId: isDefinedAndNotNull(anonymousId) ? anonymousId : utils.generateUUID(),
sessionId,
};
}
Expand Down
6 changes: 4 additions & 2 deletions test/integrations/sources/shopify/data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { skip } from 'node:test';
import { pixelCheckoutEventsTestScenarios } from './pixelTestScenarios/CheckoutEventsTests';
import { pixelCheckoutStepsScenarios } from './pixelTestScenarios/CheckoutStepsTests';
import { pixelEventsTestScenarios } from './pixelTestScenarios/ProductEventsTests';
import { checkoutEventsTestScenarios } from './webhookTestScenarios/CheckoutEventsTests';
import { genericTrackTestScenarios } from './webhookTestScenarios/GenericTrackTests';
import { identityTestScenarios } from './webhookTestScenarios/IdentifyTests';
import { mockFns } from './mocks';

const serverSideEventsScenarios = [
{
Expand Down Expand Up @@ -1424,6 +1424,7 @@ const serverSideEventsScenarios = [
verifiedEmail: true,
},
type: 'track',
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
userId: '115310627314723950',
},
],
Expand All @@ -1432,15 +1433,16 @@ const serverSideEventsScenarios = [
],
},
},
mockFns,
},
];

export const data = [
...serverSideEventsScenarios,
...checkoutEventsTestScenarios,
...genericTrackTestScenarios,
...identityTestScenarios,
...pixelCheckoutEventsTestScenarios,
...pixelCheckoutStepsScenarios,
...pixelEventsTestScenarios,
...serverSideEventsScenarios,
];
5 changes: 5 additions & 0 deletions test/integrations/sources/shopify/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import utils from '../../../../src/v0/util';

export const mockFns = (_) => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097');
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// This file contains the test scenarios for the server-side events from the Shopify GraphQL API for
// the v1 transformation flow
import utils from '../../../../../src/v0/util';
const mockFns = (_) => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097');
};
import { mockFns } from '../mocks';
import { dummySourceConfig } from '../constants';

export const checkoutEventsTestScenarios = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// This file contains the test scenarios for the server-side events from the Shopify GraphQL API for
// the v1 transformation flow
import utils from '../../../../../src/v0/util';
const mockFns = (_) => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097');
};
import { mockFns } from '../mocks';
import { dummySourceConfig } from '../constants';

export const genericTrackTestScenarios = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import utils from '../../../../../src/v0/util';
const mockFns = (_) => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097');
};
import { mockFns } from '../mocks';
import { dummySourceConfig } from '../constants';

export const identityTestScenarios = [
Expand Down

0 comments on commit 19ba143

Please sign in to comment.