Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix(default-theme): coupon code submission no longer results in error (
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaswolf authored Mar 9, 2021
1 parent f0b8016 commit 780759d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
16 changes: 15 additions & 1 deletion packages/composables/__tests__/useCart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,21 @@ describe("Composables - useCart", () => {
);
});
});

describe("submitPromotionCode", () => {
it("should execute the addPromotionCode method if promotion code is not falsy", async () => {
const { addPromotionCode } = useCart(rootContextMock);
await addPromotionCode("PROMO-CODE-123!");
mockedShopwareClient.addPromotionCode.mockResolvedValueOnce({
lineItems: [{ quantity: 1, type: "promotion" }],
} as any);
expect(mockedShopwareClient.addPromotionCode).toBeCalledTimes(1);
});
it("should not execute the addPromotionCode method if promotion code is undefined or null", async () => {
const { addPromotionCode } = useCart(rootContextMock);
await addPromotionCode(undefined as any);
expect(mockedShopwareClient.addPromotionCode).toBeCalledTimes(0);
});
});
describe("addPromotionCode", () => {
it("should add promotion code to cart", async () => {
const { appliedPromotionCodes, addPromotionCode } = useCart(
Expand Down
18 changes: 10 additions & 8 deletions packages/composables/src/hooks/useCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ export const useCart = (rootContext: ApplicationVueContext): IUseCart => {
vuexStore.commit("SET_CART", result);
}

async function sumbitPromotionCode(promotionCode: string) {
const result = await addPromotionCode(promotionCode, apiInstance);
vuexStore.commit("SET_CART", result);
broadcast(INTERCEPTOR_KEYS.ADD_PROMOTION_CODE, {
result,
promotionCode,
});
async function submitPromotionCode(promotionCode: string) {
if (promotionCode) {
const result = await addPromotionCode(promotionCode, apiInstance);
vuexStore.commit("SET_CART", result);
broadcast(INTERCEPTOR_KEYS.ADD_PROMOTION_CODE, {
result,
promotionCode,
});
}
}

const appliedPromotionCodes = computed(() => {
Expand Down Expand Up @@ -167,7 +169,7 @@ export const useCart = (rootContext: ApplicationVueContext): IUseCart => {

return {
addProduct,
addPromotionCode: sumbitPromotionCode,
addPromotionCode: submitPromotionCode,
appliedPromotionCodes,
cart,
cartItems,
Expand Down
12 changes: 7 additions & 5 deletions packages/default-theme/src/logic/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export const addToCartNotification = (payload, rootContext) => {
)
}

export const addPromotionCodeNotification = (result, rootContext) => {
export const addPromotionCodeNotification = (payload, rootContext) => {
const { pushSuccess, pushError } = useNotifications(rootContext)
// It's strange that success also ends up as an error in the API response
const { result } = payload

if (!result.errors || !result.errors.length) {
return pushSuccess(rootContext.$t("Promotion code added successfully"))
}

const err = Object.values(result.errors)[0]
if (err) {
switch (err.messageKey) {
case "promotion-discount-added":
pushSuccess(rootContext.$t("Promotion code added successfully"))
break
case "promotion-not-found":
pushError(rootContext.$t("Promotion code does not exist"))
break
Expand Down

1 comment on commit 780759d

@vercel
Copy link

@vercel vercel bot commented on 780759d Mar 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.