Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(default-theme): promotion code functionality #1155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1774853
Merge pull request #1 from DivanteLtd/master
niklaswolf Sep 21, 2020
e62deee
Merge pull request #2 from DivanteLtd/master
niklaswolf Sep 22, 2020
1196cec
Merge remote-tracking branch 'upstream/master'
niklaswolf Sep 28, 2020
250bab4
Merge remote-tracking branch 'upstream/master'
niklaswolf Oct 5, 2020
c472945
feat: added functionality to promo code input
niklaswolf Oct 5, 2020
2ca922a
test: added promotion code tests
niklaswolf Oct 5, 2020
f25c9da
docs: added promotion codes to feature list
niklaswolf Oct 5, 2020
1b98965
docs: updated composables.api.md
niklaswolf Oct 5, 2020
f4a3d15
Merge remote-tracking branch 'upstream/master' into 1154-promotion-co…
niklaswolf Oct 6, 2020
ccb06bd
feat: added toast notifications when adding coupon code
niklaswolf Oct 8, 2020
04130c0
Merge remote-tracking branch 'upstream/master' into 1154-promotion-co…
niklaswolf Oct 8, 2020
7597f97
Merge branch 'master' into 1154-promotion-code-functionality
niklaswolf Oct 9, 2020
352f393
Merge branch 'master' into 1154-promotion-code-functionality
niklaswolf Oct 9, 2020
499012a
Update typings
niklaswolf Oct 9, 2020
be85189
Update typings
niklaswolf Oct 9, 2020
37bee8f
feat: refactoring to trigger notification through interceptor
niklaswolf Oct 12, 2020
7b68155
Merge branch 'master' into pr/niklaswolf/1155
patzick Oct 12, 2020
7c12f93
chore: generate docs with composable api file
patzick Oct 12, 2020
6b9b7ef
Merge branch 'master' into pr/niklaswolf/1155
patzick Oct 12, 2020
a2b093d
refactor: interfaces after cr
patzick Oct 12, 2020
e82b924
chore: docs after cr changes
patzick Oct 12, 2020
17f16c9
chore: remove passing method as prop
patzick Oct 12, 2020
ec90bc2
fix: errors during manual tests
patzick Oct 13, 2020
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
25 changes: 16 additions & 9 deletions api/composables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function getDefaultApiParams(): {
// @beta
export const INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
USER_LOGOUT: string;
};
Expand All @@ -164,33 +165,39 @@ export interface IUseAddToCart {
// @beta
export interface IUseCart {
// (undocumented)
addProduct: ({ id, quantity }: {
addProduct: ({ id, quantity, }: {
id: string;
quantity?: number;
}) => void;
}) => Promise<void>;
// (undocumented)
addPromotionCode: (promotionCode: string) => Promise<void>;
// (undocumented)
cart: Readonly<Ref<Readonly<Cart>>>;
appliedPromotionCodes: ComputedRef<LineItem[]>;
// (undocumented)
cartItems: Readonly<Ref<Readonly<LineItem[]>>>;
cart: ComputedRef<Cart>;
// (undocumented)
cartItems: ComputedRef<LineItem[]>;
// (undocumented)
changeProductQuantity: ({ id, quantity, }: {
id: string;
quantity: number;
}) => void;
// (undocumented)
count: Readonly<Ref<Readonly<number>>>;
count: ComputedRef<number>;
// (undocumented)
error: Readonly<Ref<Readonly<string>>>;
error: ComputedRef<string>;
// (undocumented)
loading: Readonly<Ref<Readonly<boolean>>>;
loading: ComputedRef<boolean>;
// (undocumented)
refreshCart: () => void;
// (undocumented)
removeItem: ({ id }: LineItem) => Promise<void>;
// @deprecated (undocumented)
removeProduct: ({ id }: Partial<Product>) => void;
// (undocumented)
subtotal: Readonly<Ref<Readonly<number>>>;
subtotal: ComputedRef<number>;
// (undocumented)
totalPrice: Readonly<Ref<Readonly<number>>>;
totalPrice: ComputedRef<number>;
}

// @beta
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/FEATURELIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ sidebar: auto
* Go to Checkout button

### Checkout / Payment

* Add/Remove promotion codes
* Select shipping address
* Select billing address
* Select payment & shipping method
Expand Down
1 change: 1 addition & 0 deletions docs/landing/resources/api/composables.interceptor_keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Keys used accross composables with the description of incommint parameters.
```typescript
INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
USER_LOGOUT: string;
}
Expand Down
4 changes: 2 additions & 2 deletions docs/landing/resources/api/composables.iusecart.addproduct.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<b>Signature:</b>

```typescript
addProduct: ({ id, quantity }: {
addProduct: ({ id, quantity, }: {
id: string;
quantity?: number;
}) => void;
}) => Promise<void>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [addPromotionCode](./composables.iusecart.addpromotioncode.md)

## IUseCart.addPromotionCode property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>

<b>Signature:</b>

```typescript
addPromotionCode: (promotionCode: string) => Promise<void>;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [appliedPromotionCodes](./composables.iusecart.appliedpromotioncodes.md)

## IUseCart.appliedPromotionCodes property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>

<b>Signature:</b>

```typescript
appliedPromotionCodes: ComputedRef<LineItem[]>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
cart: Readonly<Ref<Readonly<Cart>>>;
cart: ComputedRef<Cart>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
cartItems: Readonly<Ref<Readonly<LineItem[]>>>;
cartItems: ComputedRef<LineItem[]>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.count.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
count: Readonly<Ref<Readonly<number>>>;
count: ComputedRef<number>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.error.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
error: Readonly<Ref<Readonly<string>>>;
error: ComputedRef<string>;
```
2 changes: 1 addition & 1 deletion docs/landing/resources/api/composables.iusecart.loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
loading: Readonly<Ref<Readonly<boolean>>>;
loading: ComputedRef<boolean>;
```
19 changes: 11 additions & 8 deletions docs/landing/resources/api/composables.iusecart.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ export interface IUseCart

| Property | Type | Description |
| --- | --- | --- |
| [addProduct](./composables.iusecart.addproduct.md) | ({ id, quantity }: { id: string; quantity?: number; }) =&gt; void | <b><i>(BETA)</i></b> |
| [cart](./composables.iusecart.cart.md) | Readonly&lt;Ref&lt;Readonly&lt;Cart&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [cartItems](./composables.iusecart.cartitems.md) | Readonly&lt;Ref&lt;Readonly&lt;LineItem\[\]&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [addProduct](./composables.iusecart.addproduct.md) | ({ id, quantity, }: { id: string; quantity?: number; }) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [addPromotionCode](./composables.iusecart.addpromotioncode.md) | (promotionCode: string) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [appliedPromotionCodes](./composables.iusecart.appliedpromotioncodes.md) | ComputedRef&lt;LineItem\[\]&gt; | <b><i>(BETA)</i></b> |
| [cart](./composables.iusecart.cart.md) | ComputedRef&lt;Cart&gt; | <b><i>(BETA)</i></b> |
| [cartItems](./composables.iusecart.cartitems.md) | ComputedRef&lt;LineItem\[\]&gt; | <b><i>(BETA)</i></b> |
| [changeProductQuantity](./composables.iusecart.changeproductquantity.md) | ({ id, quantity, }: { id: string; quantity: number; }) =&gt; void | <b><i>(BETA)</i></b> |
| [count](./composables.iusecart.count.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [error](./composables.iusecart.error.md) | Readonly&lt;Ref&lt;Readonly&lt;string&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [loading](./composables.iusecart.loading.md) | Readonly&lt;Ref&lt;Readonly&lt;boolean&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [count](./composables.iusecart.count.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |
| [error](./composables.iusecart.error.md) | ComputedRef&lt;string&gt; | <b><i>(BETA)</i></b> |
| [loading](./composables.iusecart.loading.md) | ComputedRef&lt;boolean&gt; | <b><i>(BETA)</i></b> |
| [refreshCart](./composables.iusecart.refreshcart.md) | () =&gt; void | <b><i>(BETA)</i></b> |
| [removeItem](./composables.iusecart.removeitem.md) | ({ id }: LineItem) =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [removeProduct](./composables.iusecart.removeproduct.md) | ({ id }: Partial&lt;Product&gt;) =&gt; void | <b><i>(BETA)</i></b> |
| [subtotal](./composables.iusecart.subtotal.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [totalPrice](./composables.iusecart.totalprice.md) | Readonly&lt;Ref&lt;Readonly&lt;number&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [subtotal](./composables.iusecart.subtotal.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |
| [totalPrice](./composables.iusecart.totalprice.md) | ComputedRef&lt;number&gt; | <b><i>(BETA)</i></b> |

14 changes: 14 additions & 0 deletions docs/landing/resources/api/composables.iusecart.removeitem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCart](./composables.iusecart.md) &gt; [removeItem](./composables.iusecart.removeitem.md)

## IUseCart.removeItem property

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>

<b>Signature:</b>

```typescript
removeItem: ({ id }: LineItem) => Promise<void>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>

> Warning: This API is now obsolete.
>
> use removeItem method instead
>

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
subtotal: Readonly<Ref<Readonly<number>>>;
subtotal: ComputedRef<number>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
<b>Signature:</b>

```typescript
totalPrice: Readonly<Ref<Readonly<number>>>;
totalPrice: ComputedRef<number>;
```
3 changes: 3 additions & 0 deletions packages/commons/interfaces/cart/CartItemType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @deprecated use LineItemType instead
*/
export enum CartItemType {
PRODUCT = "product",
CREDIT = "credit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { PriceDefinitionInterface } from "../price/PriceDefinitionInterface";
import { QuantityInformation } from "./QuantityInformation";

/**
* @alpha
* @beta
*/
export type LineItemType = "product" | "promotion" | "custom" | "credit";

/**
* @beta
*/
export interface LineItem {
id: string;
referencedId: string | null;
label: string | null;
quantity: number;
type: string;
type: LineItemType;
payload: [];
priceDefinition: PriceDefinitionInterface | null;
price: CalculatedPrice | null;
Expand Down
65 changes: 65 additions & 0 deletions packages/composables/__tests__/useCart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import VueCompositionApi, {
} from "@vue/composition-api";
Vue.use(VueCompositionApi);

import { LineItem } from "@shopware-pwa/commons/interfaces/models/checkout/cart/line-item/LineItem";
import { useCart } from "@shopware-pwa/composables";
import * as shopwareClient from "@shopware-pwa/shopware-6-client";

Expand Down Expand Up @@ -139,6 +140,31 @@ describe("Composables - useCart", () => {
expect(subtotal.value).toEqual(123);
});
});
describe("appliedPromotionCodes", () => {
it("should be empty array on not loaded cart", () => {
stateCart.value = null;
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value).toEqual([]);
});

it("should return an array", () => {
stateCart.value = {};
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value).toEqual([]);
});

it("should return only promotion items", () => {
stateCart.value = {
lineItems: [
{ quantity: 2, type: "product" },
{ quantity: 3, type: "product" },
{ quantity: 1, type: "promotion" },
],
};
const { appliedPromotionCodes } = useCart(rootContextMock);
expect(appliedPromotionCodes.value.length).toEqual(1);
});
});
});

describe("methods", () => {
Expand Down Expand Up @@ -212,6 +238,45 @@ describe("Composables - useCart", () => {
});
});

describe("addPromotionCode", () => {
it("should add promotion code to cart", async () => {
const { appliedPromotionCodes, addPromotionCode } = useCart(
rootContextMock
);
expect(appliedPromotionCodes.value).toEqual([]);
mockedShopwareClient.addPromotionCode.mockResolvedValueOnce({
lineItems: [{ quantity: 1, type: "promotion" }],
} as any);
await addPromotionCode("test-code");
expect(appliedPromotionCodes.value.length).toEqual(1);
});
});

describe("removeItem", () => {
it("should remove promotion code from cart", async () => {
const { appliedPromotionCodes, removeItem } = useCart(rootContextMock);
stateCart.value = {
lineItems: [{ quantity: 1, type: "promotion" }],
};
expect(appliedPromotionCodes.value.length).toEqual(1);
mockedShopwareClient.removeCartItem.mockResolvedValueOnce({
lineItems: [],
} as any);
await removeItem({ id: "qwe" } as LineItem);
expect(appliedPromotionCodes.value.length).toEqual(0);
});

it("should invoke client with correct params", async () => {
const { removeProduct } = useCart(rootContextMock);
mockedShopwareClient.removeCartItem.mockResolvedValueOnce({} as any);
await removeProduct({ id: "qwe" });
expect(mockedShopwareClient.removeCartItem).toBeCalledWith(
"qwe",
rootContextMock.$shopwareApiInstance
);
});
});

describe("changeProductQuantity", () => {
it("should change product quantity in cart", async () => {
const { count, changeProductQuantity } = useCart(rootContextMock);
Expand Down
Loading