|
| 1 | +/* global describe, test, expect, jest */ |
| 2 | +import { renderHook } from '@testing-library/react'; |
| 3 | +import { usePaymentConfig } from './usePaymentConfig'; |
| 4 | +import { |
| 5 | + CardFields, |
| 6 | + CreditDebitCards, |
| 7 | + DigitalWallets, |
| 8 | +} from '../Components/PaymentOptions'; |
| 9 | + |
| 10 | +jest.mock( '../../../../data/onboarding', () => ( { |
| 11 | + hooks: {}, |
| 12 | + selectors: {}, |
| 13 | + STORE_NAME: 'test/store', |
| 14 | + initStore: jest.fn().mockReturnValue( true ), |
| 15 | +} ) ); |
| 16 | + |
| 17 | +jest.mock( '../../../../data', () => ( { |
| 18 | + initStores: jest.fn(), |
| 19 | +} ) ); |
| 20 | + |
| 21 | +jest.mock( '@wordpress/i18n', () => ( { |
| 22 | + __: ( text ) => text, |
| 23 | +} ) ); |
| 24 | + |
| 25 | +jest.mock( '@wordpress/element', () => ( { |
| 26 | + useMemo: ( callback ) => callback(), |
| 27 | + useEffect: ( callback ) => callback(), |
| 28 | + useState: ( initialState ) => [ initialState, jest.fn() ], |
| 29 | + createContext: () => ( { |
| 30 | + Provider: ( { children } ) => children, |
| 31 | + Consumer: ( { children } ) => children, |
| 32 | + } ), |
| 33 | + forwardRef: ( Component ) => Component, |
| 34 | + Fragment: ( { children } ) => children, |
| 35 | +} ) ); |
| 36 | + |
| 37 | +jest.mock( '@wordpress/data', () => ( { |
| 38 | + useDispatch: () => ( {} ), |
| 39 | + useSelect: () => ( {} ), |
| 40 | + createReduxStore: () => ( {} ), |
| 41 | + register: () => {}, |
| 42 | + select: () => ( {} ), |
| 43 | +} ) ); |
| 44 | + |
| 45 | +jest.mock( '@wordpress/compose', () => ( { |
| 46 | + createHigherOrderComponent: () => ( Component ) => Component, |
| 47 | + withInstanceId: ( Component ) => Component, |
| 48 | +} ) ); |
| 49 | + |
| 50 | +jest.mock( '@wordpress/components', () => ( { |
| 51 | + Button: ( { children, ...props } ) => ( |
| 52 | + <button { ...props }>{ children }</button> |
| 53 | + ), |
| 54 | +} ) ); |
| 55 | + |
| 56 | +jest.mock( '@wordpress/primitives', () => ( { |
| 57 | + SVG: ( { children, ...props } ) => <svg { ...props }>{ children }</svg>, |
| 58 | + Path: ( props ) => <path { ...props } />, |
| 59 | +} ) ); |
| 60 | + |
| 61 | +jest.mock( '../../../../utils/countryInfoLinks', () => ( { |
| 62 | + learnMoreLinks: { |
| 63 | + US: { PayWithPayPal: 'https://example.com' }, |
| 64 | + GB: { PayWithPayPal: 'https://example.com' }, |
| 65 | + AU: { PayWithPayPal: 'https://example.com' }, |
| 66 | + MX: { PayWithPayPal: 'https://example.com' }, |
| 67 | + }, |
| 68 | +} ) ); |
| 69 | + |
| 70 | +const EXPECTED_PAYMENT_METHODS = [ |
| 71 | + [ |
| 72 | + 'US', |
| 73 | + [ 'PayWithPayPal', 'PayLater', 'Venmo', 'Crypto' ], |
| 74 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 75 | + ], |
| 76 | + [ |
| 77 | + 'GB', |
| 78 | + [ 'PayWithPayPal', 'PayInThree' ], |
| 79 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 80 | + ], |
| 81 | + [ |
| 82 | + 'AU', |
| 83 | + [ 'PayWithPayPal', 'PayLater' ], |
| 84 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 85 | + ], |
| 86 | + [ 'MX', [ 'PayWithPayPal', 'PayLater' ], [ 'CreditDebitCards', 'APMs' ] ], |
| 87 | +]; |
| 88 | + |
| 89 | +describe( 'usePaymentConfig hook', () => { |
| 90 | + describe( 'Payment Methods for countries', () => { |
| 91 | + test.each( EXPECTED_PAYMENT_METHODS )( |
| 92 | + 'Country %s should have valid methods', |
| 93 | + ( country, includedMethods, optionalMethods ) => { |
| 94 | + const { result } = renderHook( () => |
| 95 | + usePaymentConfig( country, true, true, false ) |
| 96 | + ); |
| 97 | + |
| 98 | + expect( result.current.includedMethods ).toHaveLength( |
| 99 | + includedMethods.length |
| 100 | + ); |
| 101 | + expect( |
| 102 | + result.current.includedMethods.map( |
| 103 | + ( method ) => method.name |
| 104 | + ) |
| 105 | + ).toEqual( includedMethods ); |
| 106 | + |
| 107 | + expect( |
| 108 | + result.current.optionalMethods.map( |
| 109 | + ( method ) => method.name |
| 110 | + ) |
| 111 | + ).toEqual( optionalMethods ); |
| 112 | + } |
| 113 | + ); |
| 114 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 115 | + 'Country %s should contain Fastlane method if hasFastlane is true', |
| 116 | + ( country ) => { |
| 117 | + const { result } = renderHook( () => |
| 118 | + usePaymentConfig( country, true, true, false ) |
| 119 | + ); |
| 120 | + const methodNames = result.current.optionalMethods.map( |
| 121 | + ( method ) => method.name |
| 122 | + ); |
| 123 | + expect( methodNames ).toContain( 'Fastlane' ); |
| 124 | + } |
| 125 | + ); |
| 126 | + |
| 127 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 128 | + 'Country %s should NOT contain Fastlane method if hasFastlane is false', |
| 129 | + ( country ) => { |
| 130 | + const { result } = renderHook( () => |
| 131 | + usePaymentConfig( country, true, false, false ) |
| 132 | + ); |
| 133 | + const methodNames = result.current.optionalMethods.map( |
| 134 | + ( method ) => method.name |
| 135 | + ); |
| 136 | + expect( methodNames ).not.toContain( 'Fastlane' ); |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 141 | + 'Country %s should contain only ACDC methods when canUseCardPayments is false', |
| 142 | + ( country ) => { |
| 143 | + const { result } = renderHook( () => |
| 144 | + usePaymentConfig( country, false, false, false ) |
| 145 | + ); |
| 146 | + const methodNames = result.current.optionalMethods.map( |
| 147 | + ( method ) => method.name |
| 148 | + ); |
| 149 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 154 | + 'Country %s should NOT contain ACDC methods when canUseCardPayments is true', |
| 155 | + ( country ) => { |
| 156 | + const { result } = renderHook( () => |
| 157 | + usePaymentConfig( country, true, false, false ) |
| 158 | + ); |
| 159 | + const methodNames = result.current.optionalMethods.map( |
| 160 | + ( method ) => method.name |
| 161 | + ); |
| 162 | + expect( methodNames ).not.toContain( 'CreditDebitCards' ); |
| 163 | + } |
| 164 | + ); |
| 165 | + |
| 166 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 167 | + 'Country %s should contain only OwnBrand methods when ownBrandOnly is true', |
| 168 | + ( country ) => { |
| 169 | + const { result } = renderHook( () => |
| 170 | + usePaymentConfig( country, true, true, true ) |
| 171 | + ); |
| 172 | + |
| 173 | + expect( |
| 174 | + result.current.optionalMethods.map( |
| 175 | + ( method ) => method.name |
| 176 | + ) |
| 177 | + ).toEqual( [ 'APMs' ] ); |
| 178 | + } |
| 179 | + ); |
| 180 | + |
| 181 | + test( 'Country MX should contain non ACDC methods when canUseCardPayments is true', () => { |
| 182 | + const { result } = renderHook( () => |
| 183 | + usePaymentConfig( 'MX', true, false, false ) |
| 184 | + ); |
| 185 | + const methodNames = result.current.optionalMethods.map( |
| 186 | + ( method ) => method.name |
| 187 | + ); |
| 188 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 189 | + expect( methodNames ).toContain( 'APMs' ); |
| 190 | + } ); |
| 191 | + |
| 192 | + test( 'Country MX should contain non ACDC methods when canUseCardPayments is false', () => { |
| 193 | + const { result } = renderHook( () => |
| 194 | + usePaymentConfig( 'MX', false, false, false ) |
| 195 | + ); |
| 196 | + const methodNames = result.current.optionalMethods.map( |
| 197 | + ( method ) => method.name |
| 198 | + ); |
| 199 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 200 | + expect( methodNames ).toContain( 'APMs' ); |
| 201 | + } ); |
| 202 | + } ); |
| 203 | +} ); |
0 commit comments