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

fix: only count products in cart #908 #909

Merged
merged 4 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/composables/__tests__/useCart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ describe("Composables - useCart", () => {

it("should show correct items quantity", () => {
stateCart.value = {
lineItems: [{ quantity: 2 }, { quantity: 3 }],
lineItems: [
{ quantity: 2, type: "product" },
{ quantity: 3, type: "product" },
{ quantity: 1, type: "promotion" },
],
};
const { count } = useCart(rootContextMock);
expect(count.value).toEqual(5);
Expand Down Expand Up @@ -143,7 +147,7 @@ describe("Composables - useCart", () => {
const { count, refreshCart } = useCart(rootContextMock);
expect(count.value).toEqual(0);
mockedShopwareClient.getCart.mockResolvedValueOnce({
lineItems: [{ quantity: 1 }],
lineItems: [{ quantity: 1, type: "product" }],
} as any);
await refreshCart();
expect(count.value).toEqual(1);
Expand All @@ -165,7 +169,7 @@ describe("Composables - useCart", () => {
const { count, addProduct } = useCart(rootContextMock);
expect(count.value).toEqual(0);
mockedShopwareClient.addProductToCart.mockResolvedValueOnce({
lineItems: [{ quantity: 1 }],
lineItems: [{ quantity: 1, type: "product" }],
} as any);
await addProduct({ id: "qwe" });
expect(count.value).toEqual(1);
Expand All @@ -187,7 +191,7 @@ describe("Composables - useCart", () => {
it("should add product to cart", async () => {
const { count, removeProduct } = useCart(rootContextMock);
stateCart.value = {
lineItems: [{ quantity: 3 }],
lineItems: [{ quantity: 3, type: "product" }],
};
expect(count.value).toEqual(3);
mockedShopwareClient.removeCartItem.mockResolvedValueOnce({
Expand All @@ -212,11 +216,11 @@ describe("Composables - useCart", () => {
it("should change product quantity in cart", async () => {
const { count, changeProductQuantity } = useCart(rootContextMock);
stateCart.value = {
lineItems: [{ quantity: 3 }],
lineItems: [{ quantity: 3, type: "product" }],
};
expect(count.value).toEqual(3);
mockedShopwareClient.changeCartItemQuantity.mockResolvedValueOnce({
lineItems: [{ quantity: 7 }],
lineItems: [{ quantity: 7, type: "product" }],
} as any);
await changeProductQuantity({ id: "qwer", quantity: 7 });
expect(count.value).toEqual(7);
Expand Down
4 changes: 3 additions & 1 deletion packages/composables/src/hooks/useCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ export const useCart = (rootContext: ApplicationVueContext): IUseCart => {
const count = computed(() => {
return cartItems.value.reduce(
(accumulator: number, lineItem: LineItem) =>
lineItem.quantity + accumulator,
lineItem.type === "product"
? lineItem.quantity + accumulator
: accumulator,
0
);
});
Expand Down