Promo Unit tests - promotions-helper.js
Also made promotions-helper.js functions null safe
This commit is contained in:
parent
9954dfafd7
commit
f0534f2718
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)"],
|
||||
coverageThreshold: {
|
||||
global: {
|
||||
statements: 73,
|
||||
statements: 76,
|
||||
},
|
||||
},
|
||||
// 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,
|
||||
];
|
||||
|
||||
const excludeFromInactivePromoErrorCodes = [
|
||||
export const excludeFromInactivePromoErrorCodes = [
|
||||
promoErrorCodes.UNKNOWN,
|
||||
promoErrorCodes.PROMO_NOT_YET_IN_USE,
|
||||
promoErrorCodes.PROMO_USAGE_COUNT_EXCEEDED,
|
||||
|
|
@ -45,7 +45,7 @@ const excludeFromInactivePromoErrorCodes = [
|
|||
export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"];
|
||||
|
||||
export function getPromosWithAddableVaps(promoList) {
|
||||
if (!promoList.length) {
|
||||
if (promoList == null || !promoList.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
|
@ -53,8 +53,14 @@ export function getPromosWithAddableVaps(promoList) {
|
|||
}
|
||||
|
||||
export function getLineItemsThatMatchPromos(promos, availableLineItems) {
|
||||
if (promos == null || availableLineItems == null) {
|
||||
return [];
|
||||
}
|
||||
const matchingLineItems = [];
|
||||
promos.forEach((promo) => {
|
||||
if (promo.discountedLineItemIds == null) {
|
||||
return;
|
||||
}
|
||||
promo.discountedLineItemIds.forEach((discountedLineItemId) => {
|
||||
matchingLineItems.push(
|
||||
...availableLineItems.filter((lineItem) => lineItem.id === discountedLineItemId)
|
||||
|
|
@ -180,6 +186,9 @@ export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
|
|||
oldActivePromos = [],
|
||||
oldInactivePromos = []
|
||||
) {
|
||||
if (promoResponse == null) {
|
||||
return [];
|
||||
}
|
||||
const alerts = [];
|
||||
// Revalidate always has an "errors" array
|
||||
if (promoResponse.errors) {
|
||||
|
|
@ -213,19 +222,24 @@ export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
|
|||
);
|
||||
alerts.push(createPromoSuccessAlert(promoCodeToDisplay));
|
||||
} else {
|
||||
alerts.push(
|
||||
createPromoErrorAlert(
|
||||
promoResponse.promoCode,
|
||||
promoResponse.errorCode,
|
||||
promoResponse.additionalInfo
|
||||
)
|
||||
);
|
||||
if (promoResponse.promoCode) {
|
||||
alerts.push(
|
||||
createPromoErrorAlert(
|
||||
promoResponse.promoCode,
|
||||
promoResponse.errorCode,
|
||||
promoResponse.additionalInfo
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return alerts;
|
||||
}
|
||||
|
||||
export function getPromosThatMatchLineItemsOnOrder(promos, lineItemsOnOrder) {
|
||||
if (promos == null || lineItemsOnOrder == null) {
|
||||
return [];
|
||||
}
|
||||
const matchingPromoCodes = [];
|
||||
const matchingPromos = [];
|
||||
const consolidatedPromosWithIds = {};
|
||||
|
|
@ -266,7 +280,7 @@ export function getVapsThatNeedToBeAddedToSatisfyPromos(
|
|||
availableLineItems,
|
||||
lineItemsOnOrder
|
||||
) {
|
||||
const clonedVaps = deepClone(lineItemsOnOrder.vaps ?? []);
|
||||
const clonedVaps = deepClone(lineItemsOnOrder?.vaps ?? []);
|
||||
const promosWithAddableVaps = getPromosWithAddableVaps(promos);
|
||||
if (promosWithAddableVaps.length) {
|
||||
const matchingLineItems = getLineItemsThatMatchPromos(
|
||||
|
|
@ -311,7 +325,7 @@ export function getNewlyInactivatedPromos(oldInactivePromos, newInactivePromos)
|
|||
const inactivePromoCodesFromResponse =
|
||||
getPromoCodesFromPromoObjectsWithoutDuplicates(newInactivePromos);
|
||||
return inactivePromoCodesFromResponse.filter(
|
||||
(errorPromoCode) => !oldInactivePromos.includes(errorPromoCode)
|
||||
(errorPromoCode) => !oldInactivePromos?.includes(errorPromoCode)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -365,8 +379,9 @@ export function getPromoCodesFromPromoObjectsWithoutDuplicates(promos) {
|
|||
|
||||
// private methods
|
||||
function findLineItemsWithPartType(typeToFind, itemsToSearch) {
|
||||
const partTypeMatches = itemsToSearch?.filter(
|
||||
(lineItem) => lineItem.partType.toUpperCase() === typeToFind.toUpperCase()
|
||||
);
|
||||
const partTypeMatches =
|
||||
itemsToSearch?.filter(
|
||||
(lineItem) => lineItem.partType.toUpperCase() === typeToFind.toUpperCase()
|
||||
) ?? [];
|
||||
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