|
| 1 | +import { createClient as createPaymentClient } from '@bigcommerce/bigpay-client'; |
| 2 | +import { createAction } from '@bigcommerce/data-store'; |
| 3 | +import { createRequestSender } from '@bigcommerce/request-sender'; |
| 4 | +import { createScriptLoader } from '@bigcommerce/script-loader'; |
| 5 | +import { merge } from 'lodash'; |
| 6 | +import { of, Observable } from 'rxjs'; |
| 7 | + |
| 8 | +import { createCheckoutStore, CheckoutRequestSender, CheckoutStore, CheckoutValidator } from '../../../checkout'; |
| 9 | +import { getCheckoutStoreState } from '../../../checkout/checkouts.mock'; |
| 10 | +import { FinalizeOrderAction, OrderActionCreator, OrderActionType, OrderRequestBody, OrderRequestSender, SubmitOrderAction } from '../../../order'; |
| 11 | +import { OrderFinalizationNotRequiredError } from '../../../order/errors'; |
| 12 | +import { getIncompleteOrder, getOrderRequestBody, getSubmittedOrder } from '../../../order/internal-orders.mock'; |
| 13 | +import { getOrder } from '../../../order/orders.mock'; |
| 14 | +import { createSpamProtection, SpamProtectionActionCreator } from '../../../order/spam-protection'; |
| 15 | +import PaymentActionCreator from '../../payment-action-creator'; |
| 16 | +import { InitializeOffsitePaymentAction, PaymentActionType, SubmitPaymentAction } from '../../payment-actions'; |
| 17 | +import { PaymentRequestOptions } from '../../payment-request-options'; |
| 18 | +import PaymentRequestSender from '../../payment-request-sender'; |
| 19 | +import PaymentRequestTransformer from '../../payment-request-transformer'; |
| 20 | +import * as paymentStatusTypes from '../../payment-status-types'; |
| 21 | +import { getVaultedInstrument } from '../../payments.mock'; |
| 22 | + |
| 23 | +import BarclaycardPaymentStrategy from './barclaycard-payment-strategy'; |
| 24 | + |
| 25 | +describe('BarclaycardPaymentStrategy', () => { |
| 26 | + let finalizeOrderAction: Observable<FinalizeOrderAction>; |
| 27 | + let initializeOffsitePaymentAction: Observable<InitializeOffsitePaymentAction>; |
| 28 | + let orderActionCreator: OrderActionCreator; |
| 29 | + let paymentActionCreator: PaymentActionCreator; |
| 30 | + let options: PaymentRequestOptions; |
| 31 | + let payload: OrderRequestBody; |
| 32 | + let store: CheckoutStore; |
| 33 | + let strategy: BarclaycardPaymentStrategy; |
| 34 | + let submitOrderAction: Observable<SubmitOrderAction>; |
| 35 | + let submitPaymentAction: Observable<SubmitPaymentAction>; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + store = createCheckoutStore(getCheckoutStoreState()); |
| 39 | + orderActionCreator = new OrderActionCreator( |
| 40 | + new OrderRequestSender(createRequestSender()), |
| 41 | + new CheckoutValidator(new CheckoutRequestSender(createRequestSender())), |
| 42 | + new SpamProtectionActionCreator(createSpamProtection(createScriptLoader())) |
| 43 | + ); |
| 44 | + paymentActionCreator = new PaymentActionCreator( |
| 45 | + new PaymentRequestSender(createPaymentClient()), |
| 46 | + orderActionCreator, |
| 47 | + new PaymentRequestTransformer() |
| 48 | + ); |
| 49 | + finalizeOrderAction = of(createAction(OrderActionType.FinalizeOrderRequested)); |
| 50 | + initializeOffsitePaymentAction = of(createAction(PaymentActionType.InitializeOffsitePaymentRequested)); |
| 51 | + submitOrderAction = of(createAction(OrderActionType.SubmitOrderRequested)); |
| 52 | + submitPaymentAction = of(createAction(PaymentActionType.SubmitPaymentRequested)); |
| 53 | + |
| 54 | + options = { methodId: 'card', gatewayId: 'barclaycard' }; |
| 55 | + payload = merge(getOrderRequestBody(), { |
| 56 | + payment: { |
| 57 | + methodId: options.methodId, |
| 58 | + gatewayId: options.gatewayId, |
| 59 | + paymentData: { |
| 60 | + shouldSaveInstrument: false, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + jest.spyOn(store, 'dispatch'); |
| 66 | + |
| 67 | + jest.spyOn(orderActionCreator, 'finalizeOrder') |
| 68 | + .mockReturnValue(finalizeOrderAction); |
| 69 | + |
| 70 | + jest.spyOn(orderActionCreator, 'submitOrder') |
| 71 | + .mockReturnValue(submitOrderAction); |
| 72 | + |
| 73 | + jest.spyOn(paymentActionCreator, 'submitPayment') |
| 74 | + .mockReturnValue(submitPaymentAction); |
| 75 | + |
| 76 | + jest.spyOn(paymentActionCreator, 'initializeOffsitePayment') |
| 77 | + .mockReturnValue(initializeOffsitePaymentAction); |
| 78 | + |
| 79 | + strategy = new BarclaycardPaymentStrategy(store, orderActionCreator, paymentActionCreator); |
| 80 | + }); |
| 81 | + |
| 82 | + it('submits order with payment payload', async () => { |
| 83 | + await strategy.execute(payload, options); |
| 84 | + |
| 85 | + expect(orderActionCreator.submitOrder).toHaveBeenCalledWith(payload, options); |
| 86 | + expect(store.dispatch).toHaveBeenCalledWith(submitOrderAction); |
| 87 | + }); |
| 88 | + |
| 89 | + it('initializes offsite payment flow', async () => { |
| 90 | + await strategy.execute(payload, options); |
| 91 | + |
| 92 | + expect(paymentActionCreator.initializeOffsitePayment).toHaveBeenCalled(); |
| 93 | + expect(store.dispatch).toHaveBeenCalledWith(initializeOffsitePaymentAction); |
| 94 | + }); |
| 95 | + |
| 96 | + it('submits payment with a vaulted instrument', async () => { |
| 97 | + const payload = { |
| 98 | + ...getOrderRequestBody(), |
| 99 | + payment: { |
| 100 | + methodId: 'card', |
| 101 | + gatewayId: 'barclaycard', |
| 102 | + paymentData: getVaultedInstrument(), |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + await strategy.execute(payload, options); |
| 107 | + |
| 108 | + expect(orderActionCreator.submitOrder).toHaveBeenCalledWith(payload, options); |
| 109 | + expect(paymentActionCreator.submitPayment).toHaveBeenCalled(); |
| 110 | + expect(store.dispatch).toHaveBeenCalledWith(submitOrderAction); |
| 111 | + expect(store.dispatch).toHaveBeenCalledWith(submitPaymentAction); |
| 112 | + }); |
| 113 | + |
| 114 | + it('finalizes order if order is created and payment is acknowledged', async () => { |
| 115 | + const state = store.getState(); |
| 116 | + |
| 117 | + jest.spyOn(state.order, 'getOrder') |
| 118 | + .mockReturnValue(getOrder()); |
| 119 | + |
| 120 | + jest.spyOn(state.payment, 'getPaymentStatus') |
| 121 | + .mockReturnValue(paymentStatusTypes.ACKNOWLEDGE); |
| 122 | + |
| 123 | + await strategy.finalize(options); |
| 124 | + |
| 125 | + expect(orderActionCreator.finalizeOrder).toHaveBeenCalledWith(getOrder().orderId, options); |
| 126 | + expect(store.dispatch).toHaveBeenCalledWith(finalizeOrderAction); |
| 127 | + }); |
| 128 | + |
| 129 | + it('finalizes order if order is created and payment is finalized', async () => { |
| 130 | + const state = store.getState(); |
| 131 | + |
| 132 | + jest.spyOn(state.order, 'getOrder') |
| 133 | + .mockReturnValue(getOrder()); |
| 134 | + |
| 135 | + jest.spyOn(state.payment, 'getPaymentStatus') |
| 136 | + .mockReturnValue(paymentStatusTypes.FINALIZE); |
| 137 | + |
| 138 | + await strategy.finalize(options); |
| 139 | + |
| 140 | + expect(orderActionCreator.finalizeOrder).toHaveBeenCalledWith(getOrder().orderId, options); |
| 141 | + expect(store.dispatch).toHaveBeenCalledWith(finalizeOrderAction); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does not finalize order if order is not created', async () => { |
| 145 | + const state = store.getState(); |
| 146 | + |
| 147 | + jest.spyOn(state.order, 'getOrder').mockReturnValue(getIncompleteOrder()); |
| 148 | + |
| 149 | + try { |
| 150 | + await strategy.finalize(); |
| 151 | + } catch (error) { |
| 152 | + expect(error).toBeInstanceOf(OrderFinalizationNotRequiredError); |
| 153 | + expect(orderActionCreator.finalizeOrder).not.toHaveBeenCalled(); |
| 154 | + expect(store.dispatch).not.toHaveBeenCalledWith(finalizeOrderAction); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + it('does not finalize order if order is not finalized or acknowledged', async () => { |
| 159 | + const state = store.getState(); |
| 160 | + |
| 161 | + jest.spyOn(state.order, 'getOrder').mockReturnValue(merge({}, getSubmittedOrder(), { |
| 162 | + payment: { |
| 163 | + status: paymentStatusTypes.INITIALIZE, |
| 164 | + }, |
| 165 | + })); |
| 166 | + |
| 167 | + try { |
| 168 | + await strategy.finalize(); |
| 169 | + } catch (error) { |
| 170 | + expect(error).toBeInstanceOf(OrderFinalizationNotRequiredError); |
| 171 | + expect(orderActionCreator.finalizeOrder).not.toHaveBeenCalled(); |
| 172 | + expect(store.dispatch).not.toHaveBeenCalledWith(finalizeOrderAction); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + it('throws error if unable to finalize due to missing data', async () => { |
| 177 | + const state = store.getState(); |
| 178 | + |
| 179 | + jest.spyOn(state.order, 'getOrder') |
| 180 | + .mockReturnValue(null); |
| 181 | + |
| 182 | + try { |
| 183 | + await strategy.finalize(); |
| 184 | + } catch (error) { |
| 185 | + expect(error).toBeInstanceOf(OrderFinalizationNotRequiredError); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + it('returns checkout state', async () => { |
| 190 | + const output = await strategy.execute(getOrderRequestBody()); |
| 191 | + |
| 192 | + expect(output).toEqual(store.getState()); |
| 193 | + }); |
| 194 | +}); |
0 commit comments