This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): Add tests for CartService
- Loading branch information
Showing
6 changed files
with
212 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
32 changes: 19 additions & 13 deletions
32
packages/shopware-6-client/__tests__/services/CartService/getCart.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |