Merge pull request #1688 from Safelite/feature/skiener/promo-unit-tests
Promo Unit tests - promotions-helper.js
This commit is contained in:
commit
bec96fb4ca
3 changed files with 1245 additions and 15 deletions
|
|
@ -29,7 +29,7 @@ module.exports = {
|
||||||
testMatch: ["**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"],
|
testMatch: ["**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"],
|
||||||
coverageThreshold: {
|
coverageThreshold: {
|
||||||
global: {
|
global: {
|
||||||
statements: 73,
|
statements: 76,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Uncomment this to avoid the massive amount of warnings we are getting for onSubmit and onInvalidSubmit
|
// Uncomment this to avoid the massive amount of warnings we are getting for onSubmit and onInvalidSubmit
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ const stackingPromoErrorCodes = [
|
||||||
promoErrorCodes.SIMILAR_PROMO_ALREADY_ON_ORDER,
|
promoErrorCodes.SIMILAR_PROMO_ALREADY_ON_ORDER,
|
||||||
];
|
];
|
||||||
|
|
||||||
const excludeFromInactivePromoErrorCodes = [
|
export const excludeFromInactivePromoErrorCodes = [
|
||||||
promoErrorCodes.UNKNOWN,
|
promoErrorCodes.UNKNOWN,
|
||||||
promoErrorCodes.PROMO_NOT_YET_IN_USE,
|
promoErrorCodes.PROMO_NOT_YET_IN_USE,
|
||||||
promoErrorCodes.PROMO_USAGE_COUNT_EXCEEDED,
|
promoErrorCodes.PROMO_USAGE_COUNT_EXCEEDED,
|
||||||
|
|
@ -45,7 +45,7 @@ const excludeFromInactivePromoErrorCodes = [
|
||||||
export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"];
|
export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"];
|
||||||
|
|
||||||
export function getPromosWithAddableVaps(promoList) {
|
export function getPromosWithAddableVaps(promoList) {
|
||||||
if (!promoList.length) {
|
if (promoList == null || !promoList.length) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,8 +53,14 @@ export function getPromosWithAddableVaps(promoList) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLineItemsThatMatchPromos(promos, availableLineItems) {
|
export function getLineItemsThatMatchPromos(promos, availableLineItems) {
|
||||||
|
if (promos == null || availableLineItems == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const matchingLineItems = [];
|
const matchingLineItems = [];
|
||||||
promos.forEach((promo) => {
|
promos.forEach((promo) => {
|
||||||
|
if (promo.discountedLineItemIds == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
promo.discountedLineItemIds.forEach((discountedLineItemId) => {
|
promo.discountedLineItemIds.forEach((discountedLineItemId) => {
|
||||||
matchingLineItems.push(
|
matchingLineItems.push(
|
||||||
...availableLineItems.filter((lineItem) => lineItem.id === discountedLineItemId)
|
...availableLineItems.filter((lineItem) => lineItem.id === discountedLineItemId)
|
||||||
|
|
@ -180,6 +186,9 @@ export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
|
||||||
oldActivePromos = [],
|
oldActivePromos = [],
|
||||||
oldInactivePromos = []
|
oldInactivePromos = []
|
||||||
) {
|
) {
|
||||||
|
if (promoResponse == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const alerts = [];
|
const alerts = [];
|
||||||
// Revalidate always has an "errors" array
|
// Revalidate always has an "errors" array
|
||||||
if (promoResponse.errors) {
|
if (promoResponse.errors) {
|
||||||
|
|
@ -213,19 +222,24 @@ export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
|
||||||
);
|
);
|
||||||
alerts.push(createPromoSuccessAlert(promoCodeToDisplay));
|
alerts.push(createPromoSuccessAlert(promoCodeToDisplay));
|
||||||
} else {
|
} else {
|
||||||
alerts.push(
|
if (promoResponse.promoCode) {
|
||||||
createPromoErrorAlert(
|
alerts.push(
|
||||||
promoResponse.promoCode,
|
createPromoErrorAlert(
|
||||||
promoResponse.errorCode,
|
promoResponse.promoCode,
|
||||||
promoResponse.additionalInfo
|
promoResponse.errorCode,
|
||||||
)
|
promoResponse.additionalInfo
|
||||||
);
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return alerts;
|
return alerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPromosThatMatchLineItemsOnOrder(promos, lineItemsOnOrder) {
|
export function getPromosThatMatchLineItemsOnOrder(promos, lineItemsOnOrder) {
|
||||||
|
if (promos == null || lineItemsOnOrder == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
const matchingPromoCodes = [];
|
const matchingPromoCodes = [];
|
||||||
const matchingPromos = [];
|
const matchingPromos = [];
|
||||||
const consolidatedPromosWithIds = {};
|
const consolidatedPromosWithIds = {};
|
||||||
|
|
@ -266,7 +280,7 @@ export function getVapsThatNeedToBeAddedToSatisfyPromos(
|
||||||
availableLineItems,
|
availableLineItems,
|
||||||
lineItemsOnOrder
|
lineItemsOnOrder
|
||||||
) {
|
) {
|
||||||
const clonedVaps = deepClone(lineItemsOnOrder.vaps ?? []);
|
const clonedVaps = deepClone(lineItemsOnOrder?.vaps ?? []);
|
||||||
const promosWithAddableVaps = getPromosWithAddableVaps(promos);
|
const promosWithAddableVaps = getPromosWithAddableVaps(promos);
|
||||||
if (promosWithAddableVaps.length) {
|
if (promosWithAddableVaps.length) {
|
||||||
const matchingLineItems = getLineItemsThatMatchPromos(
|
const matchingLineItems = getLineItemsThatMatchPromos(
|
||||||
|
|
@ -311,7 +325,7 @@ export function getNewlyInactivatedPromos(oldInactivePromos, newInactivePromos)
|
||||||
const inactivePromoCodesFromResponse =
|
const inactivePromoCodesFromResponse =
|
||||||
getPromoCodesFromPromoObjectsWithoutDuplicates(newInactivePromos);
|
getPromoCodesFromPromoObjectsWithoutDuplicates(newInactivePromos);
|
||||||
return inactivePromoCodesFromResponse.filter(
|
return inactivePromoCodesFromResponse.filter(
|
||||||
(errorPromoCode) => !oldInactivePromos.includes(errorPromoCode)
|
(errorPromoCode) => !oldInactivePromos?.includes(errorPromoCode)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -365,8 +379,9 @@ export function getPromoCodesFromPromoObjectsWithoutDuplicates(promos) {
|
||||||
|
|
||||||
// private methods
|
// private methods
|
||||||
function findLineItemsWithPartType(typeToFind, itemsToSearch) {
|
function findLineItemsWithPartType(typeToFind, itemsToSearch) {
|
||||||
const partTypeMatches = itemsToSearch?.filter(
|
const partTypeMatches =
|
||||||
(lineItem) => lineItem.partType.toUpperCase() === typeToFind.toUpperCase()
|
itemsToSearch?.filter(
|
||||||
);
|
(lineItem) => lineItem.partType.toUpperCase() === typeToFind.toUpperCase()
|
||||||
|
) ?? [];
|
||||||
return partTypeMatches;
|
return partTypeMatches;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1215
src/helpers/promotions-helper.spec.js
Normal file
1215
src/helpers/promotions-helper.spec.js
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue