Merge pull request #1688 from Safelite/feature/skiener/promo-unit-tests

Promo Unit tests - promotions-helper.js
This commit is contained in:
scottkiener 2024-01-17 10:04:18 -05:00 committed by GitHub
commit bec96fb4ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1245 additions and 15 deletions

View file

@ -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

View file

@ -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;
}

File diff suppressed because it is too large Load diff