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)"], 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

View file

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

File diff suppressed because it is too large Load diff