Skip to content

Commit efac3da

Browse files
committed
feat(payment): Stripe Shipping Form component migrated to functional
1 parent 0529684 commit efac3da

File tree

3 files changed

+194
-213
lines changed

3 files changed

+194
-213
lines changed

packages/core/src/app/shipping/stripeUPE/StripeShipping.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useCheckout } from '@bigcommerce/checkout/payment-integration-api';
88
import { AddressFormSkeleton } from '@bigcommerce/checkout/ui';
99

1010
import type CheckoutStepStatus from '../../checkout/CheckoutStepStatus';
11-
import { EMPTY_ARRAY } from '../../common/utility';
1211
import ShippingHeader from '../ShippingHeader';
1312

1413
import StripeShippingForm, { type SingleShippingFormValues } from './StripeShippingForm';
@@ -48,25 +47,17 @@ const StripeShipping = ({
4847
isShippingMethodLoading,
4948
...shippingFormProps
5049
}: StripeShippingProps): ReactNode => {
51-
const { checkoutService, checkoutState } = useCheckout();
50+
const { checkoutState } = useCheckout();
5251

5352
const {
5453
data: {
5554
getCheckout,
5655
getCustomer,
57-
getConsignments,
5856
getShippingAddressFields,
59-
getShippingCountries,
6057
},
6158
} = checkoutState;
6259
const checkout = getCheckout();
63-
const consignments = getConsignments() || [];
6460
const customer = getCustomer();
65-
66-
const initialize = checkoutService.initializeShipping;
67-
const deinitialize = checkoutService.deinitializeShipping;
68-
69-
const countries = getShippingCountries() || EMPTY_ARRAY;
7061
const getFields = getShippingAddressFields;
7162

7263
const [isStripeLoading, setIsStripeLoading] = useState(true);
@@ -101,13 +92,9 @@ const StripeShipping = ({
10192
shouldShowMultiShipping={shouldShowMultiShipping}
10293
/>
10394
<StripeShippingForm
104-
consignments={consignments}
105-
countries={countries}
10695
customerMessage={customerMessage}
10796
getFields={getFields}
10897
{...shippingFormProps}
109-
deinitialize={deinitialize}
110-
initialize={initialize}
11198
isBillingSameAsShipping={isBillingSameAsShipping}
11299
isLoading={isLoading}
113100
isMultiShippingMode={isMultiShippingMode}

packages/core/src/app/shipping/stripeUPE/StripeShippingForm.test.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { createCheckoutService, type StripeShippingEvent } from '@bigcommerce/checkout-sdk';
1+
import {
2+
type CheckoutSelectors,
3+
createCheckoutService,
4+
type StripeShippingEvent,
5+
} from '@bigcommerce/checkout-sdk';
26
import userEvent from '@testing-library/user-event';
37
import React, { act } from 'react';
48

@@ -14,6 +18,8 @@ import { getStoreConfig } from '../../config/config.mock';
1418
import { getShippingAddress } from '../shipping-addresses.mock';
1519

1620
import StripeShippingForm from './StripeShippingForm';
21+
import { getCustomer } from '../../customer/customers.mock';
22+
import { getCheckout } from '../../checkout/checkouts.mock';
1723

1824
let hasSelectedShippingOptionsReturn = false;
1925

@@ -28,6 +34,10 @@ describe('StripeShippingForm', () => {
2834
const errorLogger = new ConsoleErrorLogger();
2935
const { customFields, ...rest } = getShippingAddress();
3036
const localeContext = createLocaleContext(getStoreConfig());
37+
let checkoutState: CheckoutSelectors;
38+
39+
const initialize = jest.fn();
40+
checkoutService.initializeShipping = initialize;
3141

3242
const defaultProps = {
3343
isShippingMethodLoading: false,
@@ -42,7 +52,6 @@ describe('StripeShippingForm', () => {
4252
isBillingSameAsShipping: false,
4353
isInitialValueLoaded: false,
4454
isMultiShippingMode: false,
45-
countries: [],
4655
countriesWithAutocomplete: [],
4756
shippingAddress: rest,
4857
customerMessage: '',
@@ -55,9 +64,7 @@ describe('StripeShippingForm', () => {
5564
onSubmit: jest.fn(),
5665
getFields: jest.fn(() => addressFormFields),
5766
onUnhandledError: jest.fn(),
58-
deinitialize: jest.fn(),
5967
signOut: jest.fn(),
60-
initialize: jest.fn(),
6168
updateAddress: jest.fn(),
6269
deleteConsignments: jest.fn(),
6370
};
@@ -72,14 +79,20 @@ describe('StripeShippingForm', () => {
7279
</CheckoutProvider>
7380
);
7481

82+
beforeEach(() => {
83+
checkoutState = checkoutService.getState();
84+
jest.spyOn(checkoutState.data, 'getCustomer').mockReturnValue(getCustomer());
85+
jest.spyOn(checkoutState.data, 'getCheckout').mockReturnValue(getCheckout());
86+
})
87+
7588
afterEach(() => {
7689
jest.clearAllMocks();
7790
})
7891

7992
it('renders form with a correct parameters', async () => {
8093
const { container } = renderContainer({ isLoading: false });
8194

82-
expect(defaultProps.initialize).toHaveBeenCalled();
95+
expect(initialize).toHaveBeenCalled();
8396
expect(defaultProps.getFields).toHaveBeenCalledTimes(2);
8497
expect(defaultProps.getFields).toHaveBeenCalledWith("US");
8598
// eslint-disable-next-line testing-library/no-node-access,testing-library/no-container
@@ -159,7 +172,7 @@ describe('StripeShippingForm', () => {
159172
renderContainer({ isLoading: false });
160173

161174
await act(async () => {
162-
const { stripeupe } = defaultProps.initialize.mock.calls[0][0];
175+
const { stripeupe } = initialize.mock.calls[0][0];
163176

164177
await stripeupe.onChangeShipping(shippingChangeEvent);
165178
});
@@ -210,7 +223,7 @@ describe('StripeShippingForm', () => {
210223
renderContainer({ isLoading: false });
211224

212225
await act(async () => {
213-
const { stripeupe } = defaultProps.initialize.mock.calls[0][0];
226+
const { stripeupe } = initialize.mock.calls[0][0];
214227

215228
await stripeupe.onChangeShipping(shippingChangeEvent);
216229
});

0 commit comments

Comments
 (0)