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

Commit

Permalink
feat(tests): Add tests for CartService
Browse files Browse the repository at this point in the history
  • Loading branch information
rmakara committed Oct 21, 2019
1 parent fa40785 commit 63325aa
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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);
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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);
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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);
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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);
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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);
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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

0 comments on commit 63325aa

Please sign in to comment.