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

Commit

Permalink
feat(CartService): Add CartService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmakara committed Oct 22, 2019
1 parent b2661c4 commit 97d6a76
Show file tree
Hide file tree
Showing 10 changed files with 10,653 additions and 5 deletions.
10,486 changes: 10,486 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"chalk": "^2.4.2",
"coveralls": "^3.0.6",
"execa": "^3.0.0",
"faker": "^4.1.0",
"fs-extra": "^8.1.0",
"jest": "^24.9.0",
"lerna": "^3.16.4",
Expand All @@ -55,5 +56,8 @@
},
"peerDependencies": {
"axios": "^0.19.0"
},
"dependencies": {
"@types/faker": "^4.1.6"
}
}
2 changes: 1 addition & 1 deletion packages/shopware-6-client/__tests__/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe("endpoints", () => {
describe("getCheckoutCartProductEndpoint", () => {
it("should return Shopware checkout-cart-product endpoint", async () => {
const result = getCheckoutCartProductEndpoint(sampleProductId);
expect(result).toEqual("/checkout/cart/" + sampleProductId);
expect(result).toEqual("/checkout/cart/product/" + sampleProductId);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ describe("CartService - addCode", () => {
);
expect(result.price.netPrice).toEqual(150);
});

it("should throw unhandled 404 error when empty promotion code given", async () => {
mockedAxios.post.mockRejectedValueOnce(new Error("404: Not Found"));

let discountCode = "";

expect(addCode(discountCode)).rejects.toThrow("404: Not Found");
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith("/checkout/cart/code/");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,57 @@ describe("CartService - addLineItem", () => {
);
expect(result.lineItems[0].quantity).toEqual(5);
});

it("should throw unhandled 500 error when non-existing lineItemId given", async () => {
mockedAxios.post.mockRejectedValueOnce(
new Error("500: FRAMEWORK__INCONSISTENT_CRITERIA_IDS")
);

let lineItemId = "someNonExistingLineItemId";

expect(addLineItem(lineItemId, 1)).rejects.toThrow(
"500: FRAMEWORK__INCONSISTENT_CRITERIA_IDS"
);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(
"/checkout/cart/line-item/someNonExistingLineItemId",
{
quantity: 1,
type: "product"
}
);
});

it("should throw unhandled 400 error when negative quantity given", async () => {
mockedAxios.post.mockRejectedValueOnce(
new Error("400: CHECKOUT__CART_INVALID_LINEITEM_QUANTITY")
);

let lineItemId = "geawq90a5dab4206843d0vc3sa8wefdf";

expect(addLineItem(lineItemId, -2)).rejects.toThrow(
"400: CHECKOUT__CART_INVALID_LINEITEM_QUANTITY"
);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(
"/checkout/cart/line-item/geawq90a5dab4206843d0vc3sa8wefdf",
{
quantity: -2,
type: "product"
}
);
});

it("should throw unhandled 404 error when empty lineItemId given", async () => {
mockedAxios.post.mockRejectedValueOnce(new Error("404: Not Found"));

let lineItemId = "";

expect(addLineItem(lineItemId, 2)).rejects.toThrow("404: Not Found");
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith("/checkout/cart/line-item/", {
quantity: 2,
type: "product"
});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { addProduct } from "@shopware-pwa/shopware-6-client";
import { addProduct, update, config } from "@shopware-pwa/shopware-6-client";
import { apiService } from "../../../src/apiService";
import { random } from "faker";

jest.mock("../../../src/apiService");
const mockedAxios = apiService as jest.Mocked<typeof apiService>;

describe("CartService - addProduct", () => {
let contextToken: string;

beforeEach(() => {
jest.resetAllMocks();
contextToken = random.uuid();
update({ contextToken });
});

afterEach(() => {
expect(config.contextToken).toEqual(contextToken);
});

it("should call valid endpoint and return a cart", async () => {
Expand Down Expand Up @@ -40,10 +49,39 @@ describe("CartService - addProduct", () => {
const result = await addProduct(productId, 1);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(
"/checkout/cart/044a190a54ab4f06803909c3ee8063ef",
"/checkout/cart/product/044a190a54ab4f06803909c3ee8063ef",
{ quantity: 1 }
);

expect(result.lineItems).toHaveLength(2);
});

it("should throw unhandled 400 error when non-existing productID given", async () => {
mockedAxios.post.mockRejectedValueOnce(
new Error("400: FRAMEWORK__INVALID_UUID")
);

let productId = "someNonExistingProductId";

expect(addProduct(productId, 1)).rejects.toThrow(
"400: FRAMEWORK__INVALID_UUID"
);
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith(
"/checkout/cart/product/someNonExistingProductId",
{ quantity: 1 }
);
});

it("should throw unhandled 404 error when empty productId given", async () => {
mockedAxios.post.mockRejectedValueOnce(new Error("404: Not Found"));

let productId = "";

expect(addProduct(productId, 2)).rejects.toThrow("404: Not Found");
expect(mockedAxios.post).toBeCalledTimes(1);
expect(mockedAxios.post).toBeCalledWith("/checkout/cart/product/", {
quantity: 2
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("CartService - removeLineItem", () => {
jest.resetAllMocks();
});

it("should call valid endpoint and return cart with no items", async () => {
it("should call valid endpoint and return cart with no deleted item", async () => {
mockedAxios.delete.mockResolvedValueOnce({
data: {
name: "3a64e872ca404522a2c5d43ebc751e6b",
Expand All @@ -27,4 +27,20 @@ describe("CartService - removeLineItem", () => {
);
expect(result.lineItems).toHaveLength(0);
});

it("should throw unhandled 400 error when non-existing lineItemId given", async () => {
mockedAxios.delete.mockRejectedValueOnce(
new Error("400: CHECKOUT__CART_LINEITEM_NOT_FOUND")
);

let lineItemId = "someNonExistingLineItemId";

expect(removeLineItem(lineItemId)).rejects.toThrow(
"400: CHECKOUT__CART_LINEITEM_NOT_FOUND"
);
expect(mockedAxios.delete).toBeCalledTimes(1);
expect(mockedAxios.delete).toBeCalledWith(
"/checkout/cart/line-item/someNonExistingLineItemId"
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,40 @@ describe("CartService - addLineItem", () => {
);
expect(result.lineItems[0].quantity).toEqual(3);
});

it("should throw unhandled 400 error when non-existing lineItemId given", async () => {
mockedAxios.patch.mockRejectedValueOnce(
new Error("400: CHECKOUT__CART_LINEITEM_NOT_FOUND")
);

let lineItemId = "someNonExistingLineItemId";

expect(updateLineItem(lineItemId, 1)).rejects.toThrow(
"400: CHECKOUT__CART_LINEITEM_NOT_FOUND"
);
expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith(
"/checkout/cart/line-item/someNonExistingLineItemId",
{ quantity: 1 }
);
});

it("should throw unhandled 400 error when negative quantity given", async () => {
mockedAxios.patch.mockRejectedValueOnce(
new Error("400: CHECKOUT__CART_INVALID_LINEITEM_QUANTITY")
);

let lineItemId = "geawq90a5dab4206843d0vc3sa8wefdf";

expect(updateLineItem(lineItemId, -2)).rejects.toThrow(
"400: CHECKOUT__CART_INVALID_LINEITEM_QUANTITY"
);
expect(mockedAxios.patch).toBeCalledTimes(1);
expect(mockedAxios.patch).toBeCalledWith(
"/checkout/cart/line-item/geawq90a5dab4206843d0vc3sa8wefdf",
{
quantity: -2
}
);
});
});
2 changes: 1 addition & 1 deletion packages/shopware-6-client/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const getCustomerOrderEndpoint = () => `/customer/order`;
export const getCheckoutCartEndpoint = () => `/checkout/cart`;

export const getCheckoutCartProductEndpoint = (productId: string) =>
`/checkout/cart/${productId}`;
`/checkout/cart/product/${productId}`;

export const getCheckoutCartLineItemEndpoint = (lineItemId: string) =>
`/checkout/cart/line-item/${lineItemId}`;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,11 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=

faker@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/faker/-/faker-4.1.0.tgz#1e45bbbecc6774b3c195fad2835109c6d748cc3f"
integrity sha1-HkW7vsxndLPBlfrSg1EJxtdIzD8=

fast-deep-equal@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"
Expand Down

0 comments on commit 97d6a76

Please sign in to comment.