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(composables): more interceptors #1342

Merged
merged 7 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions api/composables.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,16 @@ export interface IInterceptorCallbackFunction {
// @beta
export const INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_TO_WISHLIST: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
ORDER_PLACE: string;
SESSION_SET_CURRENCY: string;
SESSION_SET_PAYMENT_METHOD: string;
SESSION_SET_SHIPPING_METHOD: string;
USER_LOGOUT: string;
USER_LOGIN: string;
USER_REGISTER: string;
};

// @beta
Expand Down Expand Up @@ -233,6 +240,10 @@ export interface IUseCheckout {
guestOrderParams: Ref<Readonly<Partial<GuestOrderParams>>>;
isGuestOrder: Readonly<Ref<boolean>>;
// (undocumented)
onOrderPlace: (fn: (params: {
order: Order;
}) => void) => void;
// (undocumented)
paymentMethods: Readonly<Ref<readonly PaymentMethod[]>>;
// (undocumented)
shippingAddress: Readonly<Ref<ShippingAddress | undefined>>;
Expand Down Expand Up @@ -347,6 +358,18 @@ export interface IUseSessionContext {
// (undocumented)
currency: Readonly<Ref<Currency | null>>;
// (undocumented)
onCurrencyChange: (fn: (params: {
currency: Currency;
}) => void) => void;
// (undocumented)
onPaymentMethodChange: (fn: (params: {
paymentMethod: PaymentMethod;
}) => void) => void;
// (undocumented)
onShippingMethodChange: (fn: (params: {
shippingMethod: ShippingMethod;
}) => void) => void;
// (undocumented)
paymentMethod: Readonly<Ref<PaymentMethod | null>>;
// (undocumented)
refreshSessionContext: () => Promise<void>;
Expand Down Expand Up @@ -406,6 +429,12 @@ export interface IUseUser {
}) => Promise<string | boolean>;
onLogout: (fn: () => void) => void;
// (undocumented)
onUserLogin: (fn: (params: {
customer: Customer;
}) => void) => void;
// (undocumented)
onUserRegister: (fn: () => void) => void;
// (undocumented)
orders: Ref<Order[] | null>;
// (undocumented)
refreshUser: () => Promise<void>;
Expand Down Expand Up @@ -438,6 +467,10 @@ export interface IUseWishlist {
// (undocumented)
items: Ref<string[]>;
// (undocumented)
onAddToWishlist: (fn: (params: {
product: Product;
}) => void) => void;
// (undocumented)
removeFromWishlist: (id: string) => void;
}

Expand Down
7 changes: 7 additions & 0 deletions docs/landing/resources/api/composables.interceptor_keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ Keys used accross composables with the description of incommint parameters.
```typescript
INTERCEPTOR_KEYS: {
ADD_TO_CART: string;
ADD_TO_WISHLIST: string;
ADD_PROMOTION_CODE: string;
ERROR: string;
ORDER_PLACE: string;
SESSION_SET_CURRENCY: string;
SESSION_SET_PAYMENT_METHOD: string;
SESSION_SET_SHIPPING_METHOD: string;
USER_LOGOUT: string;
USER_LOGIN: string;
USER_REGISTER: string;
}
```
1 change: 1 addition & 0 deletions docs/landing/resources/api/composables.iusecheckout.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface IUseCheckout
| [getShippingMethods](./composables.iusecheckout.getshippingmethods.md) | (options?: { forceReload: boolean; }) =&gt; Promise&lt;Readonly&lt;Ref&lt;readonly ShippingMethod\[\]&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [guestOrderParams](./composables.iusecheckout.guestorderparams.md) | Ref&lt;Readonly&lt;Partial&lt;GuestOrderParams&gt;&gt;&gt; | <b><i>(BETA)</i></b> |
| [isGuestOrder](./composables.iusecheckout.isguestorder.md) | Readonly&lt;Ref&lt;boolean&gt;&gt; | <b><i>(BETA)</i></b> Flag isGuestOrder is true when user is not logged in |
| [onOrderPlace](./composables.iusecheckout.onorderplace.md) | (fn: (params: { order: Order; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [paymentMethods](./composables.iusecheckout.paymentmethods.md) | Readonly&lt;Ref&lt;readonly PaymentMethod\[\]&gt;&gt; | <b><i>(BETA)</i></b> |
| [shippingAddress](./composables.iusecheckout.shippingaddress.md) | Readonly&lt;Ref&lt;ShippingAddress \| undefined&gt;&gt; | <b><i>(BETA)</i></b> |
| [shippingMethods](./composables.iusecheckout.shippingmethods.md) | Readonly&lt;Ref&lt;readonly ShippingMethod\[\]&gt;&gt; | <b><i>(BETA)</i></b> |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseCheckout](./composables.iusecheckout.md) &gt; [onOrderPlace](./composables.iusecheckout.onorderplace.md)

## IUseCheckout.onOrderPlace 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
onOrderPlace: (fn: (params: {
order: Order;
}) => void) => void;
```
3 changes: 3 additions & 0 deletions docs/landing/resources/api/composables.iusesessioncontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SessionContext contain all related data like user, currency, country, shippingMe
| [activeBillingAddress](./composables.iusesessioncontext.activebillingaddress.md) | Readonly&lt;Ref&lt;BillingAddress \| null&gt;&gt; | <b><i>(BETA)</i></b> |
| [activeShippingAddress](./composables.iusesessioncontext.activeshippingaddress.md) | Readonly&lt;Ref&lt;ShippingAddress \| null&gt;&gt; | <b><i>(BETA)</i></b> |
| [currency](./composables.iusesessioncontext.currency.md) | Readonly&lt;Ref&lt;Currency \| null&gt;&gt; | <b><i>(BETA)</i></b> |
| [onCurrencyChange](./composables.iusesessioncontext.oncurrencychange.md) | (fn: (params: { currency: Currency; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [onPaymentMethodChange](./composables.iusesessioncontext.onpaymentmethodchange.md) | (fn: (params: { paymentMethod: PaymentMethod; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [onShippingMethodChange](./composables.iusesessioncontext.onshippingmethodchange.md) | (fn: (params: { shippingMethod: ShippingMethod; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [paymentMethod](./composables.iusesessioncontext.paymentmethod.md) | Readonly&lt;Ref&lt;PaymentMethod \| null&gt;&gt; | <b><i>(BETA)</i></b> |
| [refreshSessionContext](./composables.iusesessioncontext.refreshsessioncontext.md) | () =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [sessionContext](./composables.iusesessioncontext.sessioncontext.md) | Readonly&lt;Ref&lt;SessionContext \| null&gt;&gt; | <b><i>(BETA)</i></b> |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseSessionContext](./composables.iusesessioncontext.md) &gt; [onCurrencyChange](./composables.iusesessioncontext.oncurrencychange.md)

## IUseSessionContext.onCurrencyChange 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
onCurrencyChange: (fn: (params: {
currency: Currency;
}) => void) => void;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseSessionContext](./composables.iusesessioncontext.md) &gt; [onPaymentMethodChange](./composables.iusesessioncontext.onpaymentmethodchange.md)

## IUseSessionContext.onPaymentMethodChange 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
onPaymentMethodChange: (fn: (params: {
paymentMethod: PaymentMethod;
}) => void) => void;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseSessionContext](./composables.iusesessioncontext.md) &gt; [onShippingMethodChange](./composables.iusesessioncontext.onshippingmethodchange.md)

## IUseSessionContext.onShippingMethodChange 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
onShippingMethodChange: (fn: (params: {
shippingMethod: ShippingMethod;
}) => void) => void;
```
2 changes: 2 additions & 0 deletions docs/landing/resources/api/composables.iuseuser.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface IUseUser
| [logout](./composables.iuseuser.logout.md) | () =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [markAddressAsDefault](./composables.iuseuser.markaddressasdefault.md) | ({ addressId, type, }: { addressId?: string; type?: AddressType; }) =&gt; Promise&lt;string \| boolean&gt; | <b><i>(BETA)</i></b> |
| [onLogout](./composables.iuseuser.onlogout.md) | (fn: () =&gt; void) =&gt; void | <b><i>(BETA)</i></b> React on user logout |
| [onUserLogin](./composables.iuseuser.onuserlogin.md) | (fn: (params: { customer: Customer; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [onUserRegister](./composables.iuseuser.onuserregister.md) | (fn: () =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [orders](./composables.iuseuser.orders.md) | Ref&lt;Order\[\] \| null&gt; | <b><i>(BETA)</i></b> |
| [refreshUser](./composables.iuseuser.refreshuser.md) | () =&gt; Promise&lt;void&gt; | <b><i>(BETA)</i></b> |
| [register](./composables.iuseuser.register.md) | ({}: CustomerRegistrationParams) =&gt; Promise&lt;boolean&gt; | <b><i>(BETA)</i></b> |
Expand Down
16 changes: 16 additions & 0 deletions docs/landing/resources/api/composables.iuseuser.onuserlogin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseUser](./composables.iuseuser.md) &gt; [onUserLogin](./composables.iuseuser.onuserlogin.md)

## IUseUser.onUserLogin 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
onUserLogin: (fn: (params: {
customer: Customer;
}) => void) => void;
```
14 changes: 14 additions & 0 deletions docs/landing/resources/api/composables.iuseuser.onuserregister.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; [IUseUser](./composables.iuseuser.md) &gt; [onUserRegister](./composables.iuseuser.onuserregister.md)

## IUseUser.onUserRegister 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
onUserRegister: (fn: () => void) => void;
```
1 change: 1 addition & 0 deletions docs/landing/resources/api/composables.iusewishlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export interface IUseWishlist
| [count](./composables.iusewishlist.count.md) | Ref&lt;number&gt; | <b><i>(BETA)</i></b> |
| [isInWishlist](./composables.iusewishlist.isinwishlist.md) | Ref&lt;boolean&gt; | <b><i>(BETA)</i></b> |
| [items](./composables.iusewishlist.items.md) | Ref&lt;string\[\]&gt; | <b><i>(BETA)</i></b> |
| [onAddToWishlist](./composables.iusewishlist.onaddtowishlist.md) | (fn: (params: { product: Product; }) =&gt; void) =&gt; void | <b><i>(BETA)</i></b> |
| [removeFromWishlist](./composables.iusewishlist.removefromwishlist.md) | (id: string) =&gt; void | <b><i>(BETA)</i></b> |

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/composables](./composables.md) &gt; [IUseWishlist](./composables.iusewishlist.md) &gt; [onAddToWishlist](./composables.iusewishlist.onaddtowishlist.md)

## IUseWishlist.onAddToWishlist 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
onAddToWishlist: (fn: (params: {
product: Product;
}) => void) => void;
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Tag } from "../../system/tag/Tag";
import { CustomField } from "../../common/CustomField";

/**
* @alpha
* @beta
*/
export interface Customer {
id: string;
Expand Down
41 changes: 28 additions & 13 deletions packages/composables/__tests__/useCheckout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe("Composables - useCheckout", () => {
},
$shopwareApiInstance: jest.fn(),
};

const interceptMock = jest.fn();
const broadcastMock = jest.fn();
const refreshCartMock = jest.fn(async () => {});
beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -57,6 +58,12 @@ describe("Composables - useCheckout", () => {
sessionContext: sessionContextMock,
} as any;
});
mockedComposables.useIntercept.mockImplementation(() => {
return {
broadcast: broadcastMock,
intercept: interceptMock,
} as any;
});
stateContext.value = null;
consoleErrorSpy.mockImplementationOnce(() => {});
});
Expand Down Expand Up @@ -375,12 +382,11 @@ describe("Composables - useCheckout", () => {
await expect(createOrder()).rejects.toEqual({
message: "some error",
});
expect(consoleErrorSpy).toBeCalledWith(
"[useCheckout][createOrder] isGuest:false",
{
message: "some error",
}
);
expect(broadcastMock).toBeCalledWith("error", {
error: { message: "some error" },
inputParams: {},
methodName: "[useCheckout][createOrder]",
});
});
});

Expand Down Expand Up @@ -425,14 +431,23 @@ describe("Composables - useCheckout", () => {
await expect(createOrder()).rejects.toEqual({
message: "some guest error",
});
expect(consoleErrorSpy).toBeCalledWith(
"[useCheckout][createOrder] isGuest:true",
{
message: "some guest error",
}
);
expect(broadcastMock).toBeCalledWith("error", {
error: { message: "some guest error" },
inputParams: {},
methodName: "[useCheckout][createOrder]",
});
});
});
});
describe("onOrderPlace", () => {
it("should add interceptor method", () => {
const { onOrderPlace } = useCheckout(rootContextMock);
onOrderPlace(() => {});
expect(interceptMock).toHaveBeenCalledWith(
"onOrderPlace",
expect.any(Function)
);
});
});
});
});
Loading