|
| 1 | +import { |
| 2 | + CheckoutSelectors, |
| 3 | + CheckoutService, |
| 4 | + createCheckoutService, |
| 5 | + createLanguageService, |
| 6 | + PaymentMethod, |
| 7 | +} from '@bigcommerce/checkout-sdk'; |
| 8 | +import { mount } from 'enzyme'; |
| 9 | +import { Formik } from 'formik'; |
| 10 | +import { noop } from 'lodash'; |
| 11 | +import React, { FunctionComponent } from 'react'; |
| 12 | + |
| 13 | +import { HostedWidgetPaymentComponent } from '@bigcommerce/checkout/hosted-widget-integration'; |
| 14 | +import { |
| 15 | + createLocaleContext, |
| 16 | + LocaleContext, |
| 17 | + LocaleContextType, |
| 18 | +} from '@bigcommerce/checkout/locale'; |
| 19 | +import { |
| 20 | + CheckoutProvider, |
| 21 | + PaymentMethodId, |
| 22 | + PaymentMethodProps, |
| 23 | +} from '@bigcommerce/checkout/payment-integration-api'; |
| 24 | +import { |
| 25 | + getCheckout, |
| 26 | + getCustomer, |
| 27 | + getPaymentFormServiceMock, |
| 28 | + getPaymentMethod, |
| 29 | + getStoreConfig, |
| 30 | +} from '@bigcommerce/checkout/test-mocks'; |
| 31 | + |
| 32 | +import KlarnaPaymentMethod from './KlarnaPaymentMethod'; |
| 33 | + |
| 34 | +describe('when using Klarna payment', () => { |
| 35 | + let method: PaymentMethod; |
| 36 | + let checkoutService: CheckoutService; |
| 37 | + let checkoutState: CheckoutSelectors; |
| 38 | + let defaultProps: PaymentMethodProps; |
| 39 | + let localeContext: LocaleContextType; |
| 40 | + |
| 41 | + let PaymentMethodTest: FunctionComponent<PaymentMethodProps>; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + checkoutService = createCheckoutService(); |
| 45 | + checkoutState = checkoutService.getState(); |
| 46 | + localeContext = createLocaleContext(getStoreConfig()); |
| 47 | + method = { ...getPaymentMethod(), id: PaymentMethodId.Klarna }; |
| 48 | + |
| 49 | + jest.spyOn(checkoutState.data, 'getConfig').mockReturnValue(getStoreConfig()); |
| 50 | + |
| 51 | + jest.spyOn(checkoutService, 'deinitializePayment').mockResolvedValue(checkoutState); |
| 52 | + |
| 53 | + jest.spyOn(checkoutService, 'initializePayment').mockResolvedValue(checkoutState); |
| 54 | + |
| 55 | + jest.spyOn(checkoutState.data, 'getCheckout').mockReturnValue(getCheckout()); |
| 56 | + |
| 57 | + jest.spyOn(checkoutState.data, 'isPaymentDataRequired').mockReturnValue(true); |
| 58 | + |
| 59 | + defaultProps = { |
| 60 | + method, |
| 61 | + checkoutService, |
| 62 | + checkoutState, |
| 63 | + paymentForm: getPaymentFormServiceMock(), |
| 64 | + language: createLanguageService(), |
| 65 | + onUnhandledError: jest.fn(), |
| 66 | + }; |
| 67 | + |
| 68 | + PaymentMethodTest = (props) => ( |
| 69 | + <CheckoutProvider checkoutService={checkoutService}> |
| 70 | + <LocaleContext.Provider value={localeContext}> |
| 71 | + <Formik initialValues={{}} onSubmit={noop}> |
| 72 | + <KlarnaPaymentMethod {...props} /> |
| 73 | + </Formik> |
| 74 | + </LocaleContext.Provider> |
| 75 | + </CheckoutProvider> |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + jest.clearAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('renders as hosted widget component', () => { |
| 84 | + const container = mount(<PaymentMethodTest {...defaultProps} method={method} />); |
| 85 | + const component = container.find(HostedWidgetPaymentComponent); |
| 86 | + |
| 87 | + expect(component.props()).toEqual( |
| 88 | + expect.objectContaining({ |
| 89 | + containerId: `${method.id}Widget`, |
| 90 | + deinitializePayment: expect.any(Function), |
| 91 | + initializePayment: expect.any(Function), |
| 92 | + method, |
| 93 | + }), |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('initializes method with required config when no instruments', () => { |
| 98 | + jest.spyOn(checkoutState.data, 'getInstruments').mockReturnValue(undefined); |
| 99 | + |
| 100 | + const container = mount(<PaymentMethodTest {...defaultProps} method={method} />); |
| 101 | + const component = container.find(HostedWidgetPaymentComponent); |
| 102 | + |
| 103 | + expect(component.props()).toEqual( |
| 104 | + expect.objectContaining({ |
| 105 | + containerId: `${method.id}Widget`, |
| 106 | + deinitializePayment: expect.any(Function), |
| 107 | + initializePayment: expect.any(Function), |
| 108 | + method, |
| 109 | + }), |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('initializes method with required config when customer is defined', () => { |
| 114 | + jest.spyOn(checkoutState.data, 'getCustomer').mockReturnValue(getCustomer()); |
| 115 | + |
| 116 | + const container = mount(<PaymentMethodTest {...defaultProps} method={method} />); |
| 117 | + const component = container.find(HostedWidgetPaymentComponent); |
| 118 | + |
| 119 | + expect(component.props()).toEqual( |
| 120 | + expect.objectContaining({ |
| 121 | + containerId: `${method.id}Widget`, |
| 122 | + deinitializePayment: expect.any(Function), |
| 123 | + initializePayment: expect.any(Function), |
| 124 | + method, |
| 125 | + }), |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('initializes method with required config', () => { |
| 130 | + mount(<PaymentMethodTest {...defaultProps} method={method} />); |
| 131 | + |
| 132 | + expect(checkoutService.initializePayment).toHaveBeenCalledWith( |
| 133 | + expect.objectContaining({ |
| 134 | + methodId: method.id, |
| 135 | + gatewayId: method.gateway, |
| 136 | + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 137 | + [`${method.id}`]: { |
| 138 | + container: `#${method.id}Widget`, |
| 139 | + }, |
| 140 | + }), |
| 141 | + ); |
| 142 | + }); |
| 143 | +}); |
0 commit comments