|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | + |
| 3 | +import { |
| 4 | + InvalidArgumentError, |
| 5 | + PaymentIntegrationService, |
| 6 | +} from '@bigcommerce/checkout-sdk/payment-integration-api'; |
| 7 | +import { PaymentIntegrationServiceMock } from '@bigcommerce/checkout-sdk/payment-integrations-test-utils'; |
| 8 | + |
| 9 | +import { StripeStringConstants } from '../stripe-upe/stripe-upe'; |
| 10 | +import StripeUPEScriptLoader from '../stripe-upe/stripe-upe-script-loader'; |
| 11 | +import { getStripeUPEJsMock } from '../stripe-upe/stripe-upe.mock'; |
| 12 | + |
| 13 | +import StripeLinkV2CustomerStrategy from './stripe-link-v2-customer-strategy'; |
| 14 | +import { |
| 15 | + StripeLinkV2Client, |
| 16 | + StripeLinkV2Element, |
| 17 | + StripeLinkV2ElementEvent, |
| 18 | + StripeLinkV2Elements, |
| 19 | +} from './types'; |
| 20 | + |
| 21 | +describe('StripeLinkV2CustomerStrategy', () => { |
| 22 | + let strategy: StripeLinkV2CustomerStrategy; |
| 23 | + let paymentIntegrationService: PaymentIntegrationService; |
| 24 | + let scriptLoader: jest.Mocked<StripeUPEScriptLoader>; |
| 25 | + let stripeClient: jest.Mocked<StripeLinkV2Client>; |
| 26 | + let elements: jest.Mocked<StripeLinkV2Elements>; |
| 27 | + let element: jest.Mocked<StripeLinkV2Element>; |
| 28 | + let stripeEventEmitter: EventEmitter; |
| 29 | + const isLoading = jest.fn(); |
| 30 | + const expressCheckoutOptionsMock = { |
| 31 | + allowedShippingCountries: ['AU', 'US', 'JP'], |
| 32 | + shippingAddressRequired: true, |
| 33 | + shippingRates: [{ id: '_', amount: 0, displayName: 'Pending rates' }], |
| 34 | + billingAddressRequired: true, |
| 35 | + emailRequired: true, |
| 36 | + phoneNumberRequired: true, |
| 37 | + paymentMethods: { |
| 38 | + link: StripeStringConstants.AUTO, |
| 39 | + applePay: StripeStringConstants.NEVER, |
| 40 | + googlePay: StripeStringConstants.NEVER, |
| 41 | + amazonPay: StripeStringConstants.NEVER, |
| 42 | + paypal: StripeStringConstants.NEVER, |
| 43 | + }, |
| 44 | + buttonHeight: 40, |
| 45 | + }; |
| 46 | + |
| 47 | + beforeEach(async () => { |
| 48 | + stripeEventEmitter = new EventEmitter(); |
| 49 | + paymentIntegrationService = new PaymentIntegrationServiceMock(); |
| 50 | + |
| 51 | + element = { |
| 52 | + mount: jest.fn(), |
| 53 | + on: jest.fn((eventName, callback) => { |
| 54 | + stripeEventEmitter.on(eventName, callback); |
| 55 | + }), |
| 56 | + } as any; |
| 57 | + |
| 58 | + elements = { |
| 59 | + create: jest.fn().mockReturnValue(element), |
| 60 | + update: jest.fn().mockReturnValue(element), |
| 61 | + } as any; |
| 62 | + |
| 63 | + stripeClient = { |
| 64 | + ...getStripeUPEJsMock(), |
| 65 | + elements: jest.fn().mockReturnValue(elements), |
| 66 | + confirmPayment: jest.fn(), |
| 67 | + } as any; |
| 68 | + |
| 69 | + scriptLoader = { |
| 70 | + getStripeLinkV2Client: jest.fn().mockResolvedValue(stripeClient), |
| 71 | + } as any; |
| 72 | + |
| 73 | + jest.spyOn(paymentIntegrationService, 'updateShippingAddress').mockReturnValue( |
| 74 | + Promise.resolve(paymentIntegrationService.getState()), |
| 75 | + ); |
| 76 | + |
| 77 | + strategy = new StripeLinkV2CustomerStrategy(paymentIntegrationService, scriptLoader); |
| 78 | + await strategy.initialize({ |
| 79 | + methodId: 'card', |
| 80 | + stripe_link_v2: { |
| 81 | + container: 'checkout-button', |
| 82 | + isLoading, |
| 83 | + }, |
| 84 | + } as any); |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(() => { |
| 88 | + jest.clearAllMocks(); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('#initialize()', () => { |
| 92 | + it('throws if stripe_link_v2 option is missing', async () => { |
| 93 | + await expect( |
| 94 | + strategy.initialize({ methodId: 'card', stripe_link_v2: undefined } as any), |
| 95 | + ).rejects.toThrow(InvalidArgumentError); |
| 96 | + }); |
| 97 | + |
| 98 | + it('throws if required stripe_link_v2 fields are missing', async () => { |
| 99 | + await expect( |
| 100 | + strategy.initialize({ |
| 101 | + methodId: 'card', |
| 102 | + stripe_link_v2: { |
| 103 | + container: '', |
| 104 | + isLoading: undefined, |
| 105 | + }, |
| 106 | + } as any), |
| 107 | + ).rejects.toThrow(InvalidArgumentError); |
| 108 | + }); |
| 109 | + |
| 110 | + it('loads Stripe client and mounts element successfully', () => { |
| 111 | + // TODO remove mock id below |
| 112 | + expect(scriptLoader.getStripeLinkV2Client).toHaveBeenCalledWith( |
| 113 | + 'pk_test_iyRKkVUt0YWpJ3Lq7mfsw3VW008KiFDH4s', |
| 114 | + ); |
| 115 | + expect(elements.create).toHaveBeenCalled(); |
| 116 | + expect(element.mount).toHaveBeenCalledWith('#checkout-button'); |
| 117 | + expect(isLoading).toHaveBeenCalledWith(false); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('Stripe Link V2 Element mounting', () => { |
| 122 | + it('calls mountExpressCheckoutElement during initialize()', () => { |
| 123 | + expect(elements.create).toHaveBeenCalledWith( |
| 124 | + 'expressCheckout', |
| 125 | + expressCheckoutOptionsMock, |
| 126 | + ); |
| 127 | + expect(element.mount).toHaveBeenCalledWith('#checkout-button'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('Stripe Events', () => { |
| 132 | + it('calls onShippingAddressChange callback if event was triggered', async () => { |
| 133 | + stripeEventEmitter.emit(StripeLinkV2ElementEvent.SHIPPING_ADDRESS_CHANGE, { |
| 134 | + address: { |
| 135 | + city: 'London', |
| 136 | + country: 'UK', |
| 137 | + postal_code: '091-22', |
| 138 | + state: 'CA', |
| 139 | + }, |
| 140 | + }); |
| 141 | + await new Promise((resolve) => process.nextTick(resolve)); |
| 142 | + |
| 143 | + expect(paymentIntegrationService.updateShippingAddress).toHaveBeenCalled(); |
| 144 | + expect(paymentIntegrationService.loadShippingCountries).toHaveBeenCalled(); |
| 145 | + expect(paymentIntegrationService.getState).toHaveBeenCalledTimes(5); |
| 146 | + }); |
| 147 | + |
| 148 | + it('calls onShippingRateChange callback if event was triggered', async () => { |
| 149 | + stripeEventEmitter.emit(StripeLinkV2ElementEvent.SHIPPING_RATE_CHANGE, { |
| 150 | + shippingRate: { |
| 151 | + id: '123', |
| 152 | + }, |
| 153 | + }); |
| 154 | + await new Promise((resolve) => process.nextTick(resolve)); |
| 155 | + |
| 156 | + expect(paymentIntegrationService.selectShippingOption).toHaveBeenCalled(); |
| 157 | + expect(paymentIntegrationService.getState).toHaveBeenCalledTimes(4); |
| 158 | + }); |
| 159 | + // TODO add onConfirm tests coverage after it will be implemented |
| 160 | + }); |
| 161 | +}); |
0 commit comments