|
| 1 | +/* global describe, test, expect, jest */ |
| 2 | +import { renderHook } from '@testing-library/react'; |
| 3 | +import { usePaymentConfig } from './usePaymentConfig'; |
| 4 | + |
| 5 | +jest.mock( '../../../../data', () => ( { |
| 6 | + initStores: jest.fn(), |
| 7 | +} ) ); |
| 8 | + |
| 9 | +const EXPECTED_PAYMENT_METHODS = [ |
| 10 | + [ |
| 11 | + 'US', |
| 12 | + [ 'PayWithPayPal', 'PayLater', 'Venmo', 'Crypto' ], |
| 13 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 14 | + ], |
| 15 | + [ |
| 16 | + 'GB', |
| 17 | + [ 'PayWithPayPal', 'PayInThree' ], |
| 18 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 19 | + ], |
| 20 | + [ |
| 21 | + 'AU', |
| 22 | + [ 'PayWithPayPal', 'PayLater' ], |
| 23 | + [ 'CardFields', 'DigitalWallets', 'APMs', 'Fastlane' ], |
| 24 | + ], |
| 25 | + [ 'MX', [ 'PayWithPayPal', 'PayLater' ], [ 'CreditDebitCards', 'APMs' ] ], |
| 26 | +]; |
| 27 | + |
| 28 | +describe( 'usePaymentConfig hook', () => { |
| 29 | + describe( 'Payment Methods for countries', () => { |
| 30 | + test.each( EXPECTED_PAYMENT_METHODS )( |
| 31 | + 'Country %s should have valid methods', |
| 32 | + ( country, includedMethods, optionalMethods ) => { |
| 33 | + const { result } = renderHook( () => |
| 34 | + usePaymentConfig( country, true, true, false ) |
| 35 | + ); |
| 36 | + |
| 37 | + expect( result.current.includedMethods ).toHaveLength( |
| 38 | + includedMethods.length |
| 39 | + ); |
| 40 | + expect( |
| 41 | + result.current.includedMethods.map( |
| 42 | + ( method ) => method.name |
| 43 | + ) |
| 44 | + ).toEqual( includedMethods ); |
| 45 | + |
| 46 | + expect( |
| 47 | + result.current.optionalMethods.map( |
| 48 | + ( method ) => method.name |
| 49 | + ) |
| 50 | + ).toEqual( optionalMethods ); |
| 51 | + } |
| 52 | + ); |
| 53 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 54 | + 'Country %s should contain Fastlane method if hasFastlane is true', |
| 55 | + ( country ) => { |
| 56 | + const { result } = renderHook( () => |
| 57 | + usePaymentConfig( country, true, true, false ) |
| 58 | + ); |
| 59 | + const methodNames = result.current.optionalMethods.map( |
| 60 | + ( method ) => method.name |
| 61 | + ); |
| 62 | + expect( methodNames ).toContain( 'Fastlane' ); |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 67 | + 'Country %s should NOT contain Fastlane method if hasFastlane is false', |
| 68 | + ( country ) => { |
| 69 | + const { result } = renderHook( () => |
| 70 | + usePaymentConfig( country, true, false, false ) |
| 71 | + ); |
| 72 | + const methodNames = result.current.optionalMethods.map( |
| 73 | + ( method ) => method.name |
| 74 | + ); |
| 75 | + expect( methodNames ).not.toContain( 'Fastlane' ); |
| 76 | + } |
| 77 | + ); |
| 78 | + |
| 79 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 80 | + 'Country %s should contain only ACDC methods when canUseCardPayments is false', |
| 81 | + ( country ) => { |
| 82 | + const { result } = renderHook( () => |
| 83 | + usePaymentConfig( country, false, false, false ) |
| 84 | + ); |
| 85 | + const methodNames = result.current.optionalMethods.map( |
| 86 | + ( method ) => method.name |
| 87 | + ); |
| 88 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 93 | + 'Country %s should NOT contain ACDC methods when canUseCardPayments is true', |
| 94 | + ( country ) => { |
| 95 | + const { result } = renderHook( () => |
| 96 | + usePaymentConfig( country, true, false, false ) |
| 97 | + ); |
| 98 | + const methodNames = result.current.optionalMethods.map( |
| 99 | + ( method ) => method.name |
| 100 | + ); |
| 101 | + expect( methodNames ).not.toContain( 'CreditDebitCards' ); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + test.each( [ 'US', 'GB', 'AU' ] )( |
| 106 | + 'Country %s should contain only OwnBrand methods when ownBrandOnly is true', |
| 107 | + ( country ) => { |
| 108 | + const { result } = renderHook( () => |
| 109 | + usePaymentConfig( country, true, true, true ) |
| 110 | + ); |
| 111 | + |
| 112 | + expect( |
| 113 | + result.current.optionalMethods.map( |
| 114 | + ( method ) => method.name |
| 115 | + ) |
| 116 | + ).toEqual( [ 'APMs' ] ); |
| 117 | + } |
| 118 | + ); |
| 119 | + |
| 120 | + test( 'Country MX should contain non ACDC methods when canUseCardPayments is true', () => { |
| 121 | + const { result } = renderHook( () => |
| 122 | + usePaymentConfig( 'MX', true, false, false ) |
| 123 | + ); |
| 124 | + const methodNames = result.current.optionalMethods.map( |
| 125 | + ( method ) => method.name |
| 126 | + ); |
| 127 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 128 | + expect( methodNames ).toContain( 'APMs' ); |
| 129 | + } ); |
| 130 | + |
| 131 | + test( 'Country MX should contain non ACDC methods when canUseCardPayments is false', () => { |
| 132 | + const { result } = renderHook( () => |
| 133 | + usePaymentConfig( 'MX', false, false, false ) |
| 134 | + ); |
| 135 | + const methodNames = result.current.optionalMethods.map( |
| 136 | + ( method ) => method.name |
| 137 | + ); |
| 138 | + expect( methodNames ).toContain( 'CreditDebitCards' ); |
| 139 | + expect( methodNames ).toContain( 'APMs' ); |
| 140 | + } ); |
| 141 | + } ); |
| 142 | +} ); |
0 commit comments