|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { |
| 3 | + expressCheckoutElementAmazonPay, |
| 4 | + expressCheckoutElementApplePay, |
| 5 | + expressCheckoutElementGooglePay, |
| 6 | + expressCheckoutElementStripeLink, |
| 7 | +} from 'wcstripe/blocks/express-checkout'; |
| 8 | +import { PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT } from 'wcstripe/blocks/express-checkout/constants'; |
| 9 | +import { |
| 10 | + AmazonPayPreview, |
| 11 | + ApplePayPreview, |
| 12 | + GooglePayPreview, |
| 13 | + StripeLinkPreview, |
| 14 | +} from 'wcstripe/blocks/express-checkout/express-button-previews'; |
| 15 | +import { loadStripe } from 'wcstripe/blocks/load-stripe'; |
| 16 | +import { getBlocksConfiguration } from 'wcstripe/blocks/utils'; |
| 17 | +import { checkPaymentMethodIsAvailable } from 'wcstripe/express-checkout/utils/check-payment-method-availability'; |
| 18 | +import { ExpressCheckoutContainer } from 'wcstripe/blocks/express-checkout/express-checkout-container'; |
| 19 | +import { |
| 20 | + EXPRESS_PAYMENT_METHOD_SETTING_AMAZON_PAY, |
| 21 | + EXPRESS_PAYMENT_METHOD_SETTING_APPLE_PAY, |
| 22 | + EXPRESS_PAYMENT_METHOD_SETTING_GOOGLE_PAY, |
| 23 | + EXPRESS_PAYMENT_METHOD_SETTING_LINK, |
| 24 | +} from 'wcstripe/stripe-utils/constants'; |
| 25 | + |
| 26 | +jest.mock( 'wcstripe/blocks/load-stripe' ); |
| 27 | +jest.mock( 'wcstripe/blocks/utils' ); |
| 28 | +jest.mock( |
| 29 | + 'wcstripe/express-checkout/utils/check-payment-method-availability' |
| 30 | +); |
| 31 | + |
| 32 | +// Mock the express button previews |
| 33 | +jest.mock( 'wcstripe/blocks/express-checkout/express-button-previews', () => ( { |
| 34 | + AmazonPayPreview: jest.fn( () => <div /> ), |
| 35 | + ApplePayPreview: jest.fn( () => <div /> ), |
| 36 | + GooglePayPreview: jest.fn( () => <div /> ), |
| 37 | + StripeLinkPreview: jest.fn( () => <div /> ), |
| 38 | +} ) ); |
| 39 | + |
| 40 | +// Mock the ExpressCheckoutContainer component |
| 41 | +jest.mock( '../express-checkout/express-checkout-container', () => ( { |
| 42 | + ExpressCheckoutContainer: jest.fn( () => <div /> ), |
| 43 | +} ) ); |
| 44 | + |
| 45 | +describe( 'expressCheckoutElement', () => { |
| 46 | + const api = {}; |
| 47 | + |
| 48 | + beforeEach( () => { |
| 49 | + loadStripe.mockReturnValue( Promise.resolve( {} ) ); |
| 50 | + getBlocksConfiguration.mockReturnValue( { |
| 51 | + shouldShowExpressCheckoutButton: true, |
| 52 | + supports: [], |
| 53 | + isAdmin: false, |
| 54 | + } ); |
| 55 | + checkPaymentMethodIsAvailable.mockImplementation( |
| 56 | + ( _method, _api, _cart, resolve ) => { |
| 57 | + resolve( true ); |
| 58 | + } |
| 59 | + ); |
| 60 | + } ); |
| 61 | + |
| 62 | + const testCases = [ |
| 63 | + { |
| 64 | + fn: expressCheckoutElementAmazonPay, |
| 65 | + paymentMethod: EXPRESS_PAYMENT_METHOD_SETTING_AMAZON_PAY, |
| 66 | + editorElement: AmazonPayPreview, |
| 67 | + title: 'WooCommerce Stripe - Amazon Pay', |
| 68 | + }, |
| 69 | + { |
| 70 | + fn: expressCheckoutElementApplePay, |
| 71 | + paymentMethod: EXPRESS_PAYMENT_METHOD_SETTING_APPLE_PAY, |
| 72 | + editorElement: ApplePayPreview, |
| 73 | + title: 'WooCommerce Stripe - Apple Pay', |
| 74 | + }, |
| 75 | + { |
| 76 | + fn: expressCheckoutElementGooglePay, |
| 77 | + paymentMethod: EXPRESS_PAYMENT_METHOD_SETTING_GOOGLE_PAY, |
| 78 | + editorElement: GooglePayPreview, |
| 79 | + title: 'WooCommerce Stripe - Google Pay', |
| 80 | + }, |
| 81 | + { |
| 82 | + fn: expressCheckoutElementStripeLink, |
| 83 | + paymentMethod: EXPRESS_PAYMENT_METHOD_SETTING_LINK, |
| 84 | + editorElement: StripeLinkPreview, |
| 85 | + title: 'WooCommerce Stripe - Link by Stripe', |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + testCases.forEach( ( { fn, paymentMethod, editorElement, title } ) => { |
| 90 | + it( `should return the correct config for ${ paymentMethod }`, async () => { |
| 91 | + const config = fn( api ); |
| 92 | + |
| 93 | + expect( config.name ).toBe( |
| 94 | + `${ PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT }_${ paymentMethod }` |
| 95 | + ); |
| 96 | + expect( config.title ).toBe( title ); |
| 97 | + |
| 98 | + render( config.content ); |
| 99 | + expect( ExpressCheckoutContainer ).toHaveBeenCalledWith( |
| 100 | + expect.objectContaining( { |
| 101 | + expressPaymentMethod: paymentMethod, |
| 102 | + } ), |
| 103 | + {} |
| 104 | + ); |
| 105 | + |
| 106 | + render( config.edit ); |
| 107 | + expect( editorElement ).toHaveBeenCalledTimes( 1 ); |
| 108 | + |
| 109 | + expect( config.paymentMethodId ).toBe( |
| 110 | + PAYMENT_METHOD_EXPRESS_CHECKOUT_ELEMENT |
| 111 | + ); |
| 112 | + expect( config.gatewayId ).toBe( 'stripe' ); |
| 113 | + } ); |
| 114 | + } ); |
| 115 | +} ); |
0 commit comments