Skip to content
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
10 changes: 10 additions & 0 deletions modules/@shopify/checkout-sheet-kit/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export type CheckoutEvent =
| 'start'
| 'error'
| 'addressChangeStart'
| 'paymentMethodChangeStart'
| 'submitStart'
| 'geolocationRequest';

Expand All @@ -174,6 +175,9 @@ export type CheckoutStartEventCallback = (
export type CheckoutAddressChangeStartCallback = (
event: CheckoutAddressChangeStartEvent,
) => void;
export type CheckoutPaymentMethodChangeStartCallback = (
event: CheckoutPaymentMethodChangeStartEvent,
) => void;
export type CheckoutSubmitStartCallback = (
event: CheckoutSubmitStartEvent,
) => void;
Expand All @@ -185,6 +189,7 @@ export type CheckoutEventCallback =
| CheckoutStartEventCallback
| CheckoutAddressChangeStartCallback
| CheckoutSubmitStartCallback
| CheckoutPaymentMethodChangeStartCallback
| GeolocationRequestEventCallback;

/**
Expand Down Expand Up @@ -268,6 +273,11 @@ function addEventListener(
callback: CheckoutAddressChangeStartCallback,
): Maybe<EmitterSubscription>;

function addEventListener(
event: 'paymentMethodChangeStart',
callback: CheckoutPaymentMethodChangeStartCallback,
): Maybe<EmitterSubscription>;

function addEventListener(
event: 'submitStart',
callback: CheckoutSubmitStartCallback,
Expand Down
18 changes: 18 additions & 0 deletions modules/@shopify/checkout-sheet-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
let eventCallback;

switch (event) {
case 'complete':
eventCallback = this.parseEventData('complete', callback);
break;
case 'start':
eventCallback = this.parseEventData('start', callback);
break;
case 'addressChangeStart':
eventCallback = this.parseEventData('addressChangeStart', callback);
break;
case 'submitStart':
eventCallback = this.parseEventData('submitStart', callback);
break;
case 'paymentMethodChangeStart':
eventCallback = this.parseEventData(
'paymentMethodChangeStart',
callback,
);
break;
case 'error':
eventCallback = this.parseEventData(
event,
Expand Down
55 changes: 55 additions & 0 deletions modules/@shopify/checkout-sheet-kit/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,61 @@ describe('ShopifyCheckoutSheetKit', () => {
});
});

describe('Payment Method Change Start Event', () => {
it('parses paymentMethodChangeStart event data', () => {
const instance = new ShopifyCheckoutSheet();
const callback = jest.fn();
instance.addEventListener('paymentMethodChangeStart', callback);

eventEmitter.emit('paymentMethodChangeStart', {
id: 'test-event-id',
method: 'checkout.paymentMethodChangeStart',
cart: {id: 'test-cart-id'},
});

expect(callback).toHaveBeenCalledWith({
id: 'test-event-id',
method: 'checkout.paymentMethodChangeStart',
cart: {id: 'test-cart-id'},
});
});

it('parses paymentMethodChangeStart event string data as JSON', () => {
const instance = new ShopifyCheckoutSheet();
const callback = jest.fn();
instance.addEventListener('paymentMethodChangeStart', callback);

eventEmitter.emit(
'paymentMethodChangeStart',
JSON.stringify({
id: 'test-event-id',
method: 'checkout.paymentMethodChangeStart',
cart: {id: 'test-cart-id'},
}),
);

expect(callback).toHaveBeenCalledWith({
id: 'test-event-id',
method: 'checkout.paymentMethodChangeStart',
cart: {id: 'test-cart-id'},
});
});

it('prints an error if the paymentMethodChangeStart event data cannot be parsed', () => {
const mock = jest.spyOn(global.console, 'error');
const instance = new ShopifyCheckoutSheet();
const callback = jest.fn();
instance.addEventListener('paymentMethodChangeStart', callback);

eventEmitter.emit('paymentMethodChangeStart', 'INVALID JSON');

expect(mock).toHaveBeenCalledWith(
expect.any(LifecycleEventParseError),
'INVALID JSON',
);
});
});

describe('Error Event', () => {
const internalError = {
__typename: CheckoutNativeErrorType.InternalError,
Expand Down
11 changes: 11 additions & 0 deletions sample/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ function AppWithContext({children}: PropsWithChildren) {
},
);

const paymentMethodChangeStart = shopify.addEventListener(
'paymentMethodChangeStart',
event => {
console.log(
'[App] onPaymentMethodChangeStart event received from imperative API:',
event,
);
},
);

const error = shopify.addEventListener(
'error',
(error: CheckoutException) => {
Expand All @@ -247,6 +257,7 @@ function AppWithContext({children}: PropsWithChildren) {
completed?.remove();
started?.remove();
addressChangeStart?.remove();
paymentMethodChangeStart?.remove();
close?.remove();
error?.remove();
};
Expand Down
Loading