|
| 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 | +}); |
0 commit comments