|
| 1 | +/** |
| 2 | + * Jest environment configuration |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Internal dependencies |
| 7 | + */ |
| 8 | +import { getCardBrands } from '../card-brands'; |
| 9 | +import { getUPEConfig } from 'wcpay/utils/checkout'; |
| 10 | + |
| 11 | +// Mock the getUPEConfig function |
| 12 | +jest.mock( 'wcpay/utils/checkout', () => ( { |
| 13 | + getUPEConfig: jest.fn(), |
| 14 | +} ) ); |
| 15 | + |
| 16 | +// Mock the global wcpaySettings will be set in each test |
| 17 | + |
| 18 | +describe( 'getCardBrands', () => { |
| 19 | + test( 'returns base brands for non-France merchants', () => { |
| 20 | + global.window.wcpaySettings = { |
| 21 | + accountStatus: { |
| 22 | + country: 'US', |
| 23 | + }, |
| 24 | + }; |
| 25 | + // Mock getUPEConfig to return null |
| 26 | + getUPEConfig.mockReturnValue( null ); |
| 27 | + |
| 28 | + const brands = getCardBrands(); |
| 29 | + |
| 30 | + // Should include the 4 base brands plus JCB and CUP |
| 31 | + expect( brands ).toHaveLength( 6 ); |
| 32 | + expect( brands.map( ( b ) => b.name ) ).toEqual( [ |
| 33 | + 'visa', |
| 34 | + 'mastercard', |
| 35 | + 'amex', |
| 36 | + 'discover', |
| 37 | + 'jcb', |
| 38 | + 'unionpay', |
| 39 | + ] ); |
| 40 | + } ); |
| 41 | + |
| 42 | + test( 'includes CB for France merchants', () => { |
| 43 | + global.window.wcpaySettings = { |
| 44 | + accountStatus: { |
| 45 | + country: 'FR', |
| 46 | + }, |
| 47 | + }; |
| 48 | + // Mock getUPEConfig to return France |
| 49 | + getUPEConfig.mockReturnValue( 'FR' ); |
| 50 | + |
| 51 | + const brands = getCardBrands(); |
| 52 | + |
| 53 | + // Should include all brands including CB |
| 54 | + expect( brands ).toHaveLength( 7 ); |
| 55 | + expect( brands.map( ( b ) => b.name ) ).toEqual( [ |
| 56 | + 'visa', |
| 57 | + 'mastercard', |
| 58 | + 'amex', |
| 59 | + 'discover', |
| 60 | + 'jcb', |
| 61 | + 'unionpay', |
| 62 | + 'cartes_bancaires', |
| 63 | + ] ); |
| 64 | + } ); |
| 65 | + |
| 66 | + test( 'handles missing account status gracefully', () => { |
| 67 | + global.window.wcpaySettings = {}; |
| 68 | + // Mock getUPEConfig to return null |
| 69 | + getUPEConfig.mockReturnValue( null ); |
| 70 | + |
| 71 | + const brands = getCardBrands(); |
| 72 | + |
| 73 | + // Should still return base brands without CB |
| 74 | + expect( brands ).toHaveLength( 6 ); |
| 75 | + expect( brands.map( ( b ) => b.name ) ).not.toContain( |
| 76 | + 'cartes_bancaires' |
| 77 | + ); |
| 78 | + } ); |
| 79 | + |
| 80 | + test( 'handles missing wcpaySettings gracefully', () => { |
| 81 | + global.window.wcpaySettings = undefined; |
| 82 | + // Mock getUPEConfig to return null |
| 83 | + getUPEConfig.mockReturnValue( null ); |
| 84 | + |
| 85 | + const brands = getCardBrands(); |
| 86 | + |
| 87 | + // Should still return base brands without CB |
| 88 | + expect( brands ).toHaveLength( 6 ); |
| 89 | + expect( brands.map( ( b ) => b.name ) ).not.toContain( |
| 90 | + 'cartes_bancaires' |
| 91 | + ); |
| 92 | + } ); |
| 93 | + |
| 94 | + test( 'uses UPE config storeCountry when wcpaySettings is not available', () => { |
| 95 | + global.window.wcpaySettings = undefined; |
| 96 | + // Mock getUPEConfig to return France for storeCountry |
| 97 | + getUPEConfig.mockImplementation( ( key ) => { |
| 98 | + if ( key === 'storeCountry' ) { |
| 99 | + return 'FR'; |
| 100 | + } |
| 101 | + return null; |
| 102 | + } ); |
| 103 | + |
| 104 | + const brands = getCardBrands(); |
| 105 | + |
| 106 | + // Check if getUPEConfig was called |
| 107 | + expect( getUPEConfig ).toHaveBeenCalledWith( 'storeCountry' ); |
| 108 | + |
| 109 | + // Should include CB for France |
| 110 | + expect( brands ).toHaveLength( 7 ); |
| 111 | + expect( brands.map( ( b ) => b.name ) ).toContain( 'cartes_bancaires' ); |
| 112 | + } ); |
| 113 | +} ); |
0 commit comments