Skip to content

Commit 8f23fb9

Browse files
refactor(payment): PAYPAL-2626 created PaypalPaymentsProPaymentMethod component as part of PaypalPaymentsPro migration from core to integration package (#2574)
1 parent 2433efc commit 8f23fb9

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
type CheckoutSelectors,
3+
type CheckoutService,
4+
createCheckoutService,
5+
createLanguageService,
6+
} from '@bigcommerce/checkout-sdk';
7+
import { screen } from '@testing-library/react';
8+
import React from 'react';
9+
10+
import type {
11+
PaymentFormService,
12+
PaymentMethodProps,
13+
} from '@bigcommerce/checkout/payment-integration-api';
14+
import {
15+
getCart,
16+
getCustomer,
17+
getPaymentFormServiceMock,
18+
getPaymentMethod,
19+
getStoreConfig,
20+
} from '@bigcommerce/checkout/test-mocks';
21+
import { render } from '@bigcommerce/checkout/test-utils';
22+
23+
import PaypalPaymentsProPaymentMethod from './PayPalPaymentsProPaymentMethod';
24+
25+
const hostedPaymentMethodText = 'Hosted Payment Component rendered successfully';
26+
const hostedCreditCardPaymentMethodText =
27+
'Hosted Credit Card Payment Component rendered successfully';
28+
29+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
30+
jest.mock('@bigcommerce/checkout/hosted-payment-integration', () => ({
31+
...jest.requireActual('@bigcommerce/checkout/hosted-payment-integration'),
32+
HostedPaymentComponent: () => <div>{hostedPaymentMethodText}</div>,
33+
}));
34+
35+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
36+
jest.mock('@bigcommerce/checkout/hosted-credit-card-integration', () => ({
37+
...jest.requireActual('@bigcommerce/checkout/hosted-credit-card-integration'),
38+
HostedCreditCardComponent: () => <div>{hostedCreditCardPaymentMethodText}</div>,
39+
}));
40+
41+
describe('PaypalPaymentsProPaymentMethod', () => {
42+
let checkoutService: CheckoutService;
43+
let checkoutState: CheckoutSelectors;
44+
let defaultProps: Omit<PaymentMethodProps, 'method'>;
45+
let paymentForm: PaymentFormService;
46+
let paymentMethod: PaymentMethodProps['method'];
47+
48+
beforeEach(() => {
49+
checkoutService = createCheckoutService();
50+
checkoutState = checkoutService.getState();
51+
paymentForm = getPaymentFormServiceMock();
52+
paymentMethod = {
53+
...getPaymentMethod(),
54+
id: 'paypalpaymentsprous',
55+
gateway: 'paypal',
56+
};
57+
58+
jest.spyOn(checkoutState.data, 'getConfig').mockReturnValue(getStoreConfig());
59+
jest.spyOn(checkoutState.data, 'getCart').mockReturnValue(getCart());
60+
jest.spyOn(checkoutState.data, 'getCustomer').mockReturnValue(getCustomer());
61+
jest.spyOn(checkoutService, 'deinitializePayment').mockResolvedValue(checkoutState);
62+
63+
defaultProps = {
64+
checkoutService,
65+
checkoutState,
66+
paymentForm,
67+
language: createLanguageService(),
68+
onUnhandledError: jest.fn(),
69+
};
70+
});
71+
72+
afterEach(() => {
73+
jest.clearAllMocks();
74+
});
75+
76+
it('renders HostedPaymentMethod when PaypalPaymentsPro payment method has PAYMENT_TYPE_HOSTED type', () => {
77+
const method = {
78+
...paymentMethod,
79+
type: 'PAYMENT_TYPE_HOSTED',
80+
};
81+
82+
render(<PaypalPaymentsProPaymentMethod {...defaultProps} method={method} />);
83+
84+
expect(screen.getByText(hostedPaymentMethodText)).toBeInTheDocument();
85+
});
86+
87+
it('renders HostedCreditCardPaymentMethod when PaypalPaymentsPro payment method has PAYMENT_TYPE_HOSTED type', () => {
88+
const method = {
89+
...paymentMethod,
90+
type: 'PAYMENT_TYPE_API',
91+
};
92+
93+
render(<PaypalPaymentsProPaymentMethod {...defaultProps} method={method} />);
94+
95+
expect(screen.getByText(hostedCreditCardPaymentMethodText)).toBeInTheDocument();
96+
});
97+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { type FunctionComponent } from 'react';
2+
3+
import { HostedCreditCardComponent } from '@bigcommerce/checkout/hosted-credit-card-integration';
4+
import { HostedPaymentComponent } from '@bigcommerce/checkout/hosted-payment-integration';
5+
import {
6+
type PaymentMethodProps,
7+
type PaymentMethodResolveId,
8+
toResolvableComponent,
9+
} from '@bigcommerce/checkout/payment-integration-api';
10+
11+
const PayPalPaymentsProPaymentMethod: FunctionComponent<PaymentMethodProps> = ({
12+
method,
13+
checkoutService,
14+
checkoutState,
15+
paymentForm,
16+
language,
17+
onUnhandledError,
18+
}) => {
19+
if (method.type === 'PAYMENT_TYPE_HOSTED') {
20+
return (
21+
<HostedPaymentComponent
22+
checkoutService={checkoutService}
23+
checkoutState={checkoutState}
24+
deinitializePayment={checkoutService.deinitializePayment}
25+
initializePayment={checkoutService.initializePayment}
26+
language={language}
27+
method={method}
28+
onUnhandledError={onUnhandledError}
29+
paymentForm={paymentForm}
30+
/>
31+
);
32+
}
33+
34+
return (
35+
<HostedCreditCardComponent
36+
checkoutService={checkoutService}
37+
checkoutState={checkoutState}
38+
language={language}
39+
method={method}
40+
onUnhandledError={onUnhandledError}
41+
paymentForm={paymentForm}
42+
/>
43+
);
44+
};
45+
46+
export default toResolvableComponent<PaymentMethodProps, PaymentMethodResolveId>(
47+
PayPalPaymentsProPaymentMethod,
48+
[{ id: 'paypal' }],
49+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as PayPalPaymentsProPaymentMethod } from './PayPalPaymentsProPaymentMethod';

0 commit comments

Comments
 (0)