From 63325aaba706892a1cdf3984035ebefb38a77d48 Mon Sep 17 00:00:00 2001 From: Rafal Makara Date: Mon, 21 Oct 2019 13:42:05 +0200 Subject: [PATCH] feat(tests): Add tests for CartService --- .../services/CartService/addCode.spec.ts | 32 +++++++++++++ .../services/CartService/addLineItem.spec.ts | 43 +++++++++++++++++ .../services/CartService/addProduct.spec.ts | 48 +++++++++++++++++++ .../services/CartService/getCart.spec.ts | 32 ++++++++----- .../CartService/removeLineItem.spec.ts | 30 ++++++++++++ .../CartService/updateLineItem.spec.ts | 40 ++++++++++++++++ 6 files changed, 212 insertions(+), 13 deletions(-) diff --git a/packages/shopware-6-client/__tests__/services/CartService/addCode.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/addCode.spec.ts index e69de29bb..b2addb042 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/addCode.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/addCode.spec.ts @@ -0,0 +1,32 @@ +import { addCode } from "@shopware-pwa/shopware-6-client"; +import { apiService } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("CartService - addCode", () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it("should call valid endpoint and return a cart", async () => { + mockedAxios.post.mockResolvedValueOnce({ + data: { + name: "3a64e872ca404522a2c5d43ebc751e6b", + token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", + price: { + netPrice: 150 + } + } + }); + + let discountCode = "your_secret_discount_code_239kfdu24"; + + const result = await addCode(discountCode); + expect(mockedAxios.post).toBeCalledTimes(1); + expect(mockedAxios.post).toBeCalledWith( + "/checkout/cart/code/your_secret_discount_code_239kfdu24" + ); + expect(result.price.netPrice).toEqual(150); + }); +}); diff --git a/packages/shopware-6-client/__tests__/services/CartService/addLineItem.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/addLineItem.spec.ts index e69de29bb..458f710b2 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/addLineItem.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/addLineItem.spec.ts @@ -0,0 +1,43 @@ +import { addLineItem } from "@shopware-pwa/shopware-6-client"; +import { apiService } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("CartService - addLineItem", () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it("should call valid endpoint and return a cart", async () => { + mockedAxios.post.mockResolvedValueOnce({ + data: { + name: "3a64e872ca404522a2c5d43ebc751e6b", + token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", + lineItems: [ + { + id: "geawq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 5, + payload: { + productNumber: "aaaa190a54ab4f06803909c3ee8063ef" + } + } + ] + } + }); + + let lineItemId = "geawq90a5dab4206843d0vc3sa8wefdf"; + + const result = await addLineItem(lineItemId, 3); + expect(mockedAxios.post).toBeCalledTimes(1); + expect(mockedAxios.post).toBeCalledWith( + "/checkout/cart/line-item/geawq90a5dab4206843d0vc3sa8wefdf", + { + type: "product", + quantity: 3 + } + ); + expect(result.lineItems[0].quantity).toEqual(5); + }); +}); diff --git a/packages/shopware-6-client/__tests__/services/CartService/addProduct.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/addProduct.spec.ts index e69de29bb..985473bf1 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/addProduct.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/addProduct.spec.ts @@ -0,0 +1,48 @@ +import { addProduct } from "@shopware-pwa/shopware-6-client"; +import { apiService } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("CartService - addProduct", () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it("should call valid endpoint and return a cart", async () => { + mockedAxios.post.mockResolvedValueOnce({ + data: { + name: "3a64e872ca404522a2c5d43ebc751e6b", + token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", + lineItems: [ + { + id: "geawq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 5, + payload: { + productNumber: "aaaa190a54ab4f06803909c3ee8063ef" + } + }, + { + id: "bbbwq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 5, + payload: { + productNumber: "zzza190a54ab4f06803909c3ee8063ef" + } + } + ] + } + }); + + let productId = "044a190a54ab4f06803909c3ee8063ef"; + + const result = await addProduct(productId, 1); + expect(mockedAxios.post).toBeCalledTimes(1); + expect(mockedAxios.post).toBeCalledWith( + "/checkout/cart/044a190a54ab4f06803909c3ee8063ef", + { quantity: 1 } + ); + expect(result.lineItems).toHaveLength(2); + }); +}); diff --git a/packages/shopware-6-client/__tests__/services/CartService/getCart.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/getCart.spec.ts index 8eda0bcd4..7074a1991 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/getCart.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/getCart.spec.ts @@ -1,33 +1,39 @@ -import { getCart, update } from "@shopware-pwa/shopware-6-client"; +import { getCart } from "@shopware-pwa/shopware-6-client"; import { apiService } from "../../../src/apiService"; jest.mock("../../../src/apiService"); const mockedAxios = apiService as jest.Mocked; describe("CartService - getCart", () => { - beforeEach(() => { - update({ contextToken: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" }); - jest.resetAllMocks(); - }); - it("should return existing cart object", async () => { mockedAxios.get.mockResolvedValueOnce({ data: { name: "3a64e872ca404522a2c5d43ebc751e6b", token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", - lineItems: { - label: "Red T-Shirt", - quantity: 2 - } + lineItems: [ + { + id: "geawq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 5, + payload: { + productNumber: "aaaa190a54ab4f06803909c3ee8063ef" + } + }, + { + id: "bbbwq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 5, + payload: { + productNumber: "zzza190a54ab4f06803909c3ee8063ef" + } + } + ] } }); - let cartName = "3a64e872ca404522a2c5d43ebc751e6b"; - const result = await getCart(); expect(mockedAxios.get).toBeCalledTimes(1); expect(mockedAxios.get).toBeCalledWith("/checkout/cart"); expect(result.lineItems).not.toBeNull(); - expect(result.name).toEqual(cartName); }); }); diff --git a/packages/shopware-6-client/__tests__/services/CartService/removeLineItem.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/removeLineItem.spec.ts index e69de29bb..fbc8f7b26 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/removeLineItem.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/removeLineItem.spec.ts @@ -0,0 +1,30 @@ +import { removeLineItem } from "@shopware-pwa/shopware-6-client"; +import { apiService } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("CartService - removeLineItem", () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it("should call valid endpoint and return cart with no items", async () => { + mockedAxios.delete.mockResolvedValueOnce({ + data: { + name: "3a64e872ca404522a2c5d43ebc751e6b", + token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", + lineItems: [] + } + }); + + let lineItemId = "geawq90a5dab4206843d0vc3sa8wefdf"; + + const result = await removeLineItem(lineItemId); + expect(mockedAxios.delete).toBeCalledTimes(1); + expect(mockedAxios.delete).toBeCalledWith( + "/checkout/cart/line-item/geawq90a5dab4206843d0vc3sa8wefdf" + ); + expect(result.lineItems).toHaveLength(0); + }); +}); diff --git a/packages/shopware-6-client/__tests__/services/CartService/updateLineItem.spec.ts b/packages/shopware-6-client/__tests__/services/CartService/updateLineItem.spec.ts index e69de29bb..a13fe89a1 100644 --- a/packages/shopware-6-client/__tests__/services/CartService/updateLineItem.spec.ts +++ b/packages/shopware-6-client/__tests__/services/CartService/updateLineItem.spec.ts @@ -0,0 +1,40 @@ +import { updateLineItem } from "@shopware-pwa/shopware-6-client"; +import { apiService } from "../../../src/apiService"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("CartService - addLineItem", () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it("should call valid endpoint and return cart with no items", async () => { + mockedAxios.patch.mockResolvedValueOnce({ + data: { + name: "3a64e872ca404522a2c5d43ebc751e6b", + token: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6", + lineItems: [ + { + id: "geawq90a5dab4206843d0vc3sa8wefdf", + label: "Red T-Shirt", + quantity: 3, + payload: { + productNumber: "aaaa190a54ab4f06803909c3ee8063ef" + } + } + ] + } + }); + + let lineItemId = "geawq90a5dab4206843d0vc3sa8wefdf"; + + const result = await updateLineItem(lineItemId, 3); + expect(mockedAxios.patch).toBeCalledTimes(1); + expect(mockedAxios.patch).toBeCalledWith( + "/checkout/cart/line-item/geawq90a5dab4206843d0vc3sa8wefdf", + { quantity: 3 } + ); + expect(result.lineItems[0].quantity).toEqual(3); + }); +});