|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import React from 'react'; |
| 5 | +import { render, screen } from '@testing-library/react'; |
| 6 | +import userEvent from '@testing-library/user-event'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Internal dependencies |
| 10 | + */ |
| 11 | +import AmazonPaySettings from '../amazon-pay-settings'; |
| 12 | +import { useAmazonPayEnabledSettings, useAmazonPayLocations } from 'wcpay/data'; |
| 13 | +import WCPaySettingsContext from 'wcpay/settings/wcpay-settings-context'; |
| 14 | + |
| 15 | +jest.mock( 'wcpay/data', () => ( { |
| 16 | + useAmazonPayEnabledSettings: jest.fn(), |
| 17 | + useAmazonPayLocations: jest.fn(), |
| 18 | + usePaymentRequestButtonSize: jest.fn().mockReturnValue( [ 'medium' ] ), |
| 19 | + useWooPayEnabledSettings: jest.fn().mockReturnValue( [ false ] ), |
| 20 | + usePaymentRequestEnabledSettings: jest.fn().mockReturnValue( [ false ] ), |
| 21 | +} ) ); |
| 22 | + |
| 23 | +const renderWithSettingsProvider = ( ui ) => |
| 24 | + render( |
| 25 | + <WCPaySettingsContext.Provider |
| 26 | + value={ { featureFlags: { woopay: false, amazonPay: false } } } |
| 27 | + > |
| 28 | + { ui } |
| 29 | + </WCPaySettingsContext.Provider> |
| 30 | + ); |
| 31 | + |
| 32 | +describe( 'AmazonPaySettings', () => { |
| 33 | + beforeEach( () => { |
| 34 | + useAmazonPayEnabledSettings.mockReturnValue( [ true, jest.fn() ] ); |
| 35 | + useAmazonPayLocations.mockReturnValue( [ |
| 36 | + [ 'product', 'cart', 'checkout' ], |
| 37 | + jest.fn(), |
| 38 | + ] ); |
| 39 | + } ); |
| 40 | + |
| 41 | + it( 'triggers the update handler when the enable checkbox is clicked', async () => { |
| 42 | + const updateIsAmazonPayEnabledHandler = jest.fn(); |
| 43 | + useAmazonPayEnabledSettings.mockReturnValue( [ |
| 44 | + true, |
| 45 | + updateIsAmazonPayEnabledHandler, |
| 46 | + ] ); |
| 47 | + |
| 48 | + renderWithSettingsProvider( <AmazonPaySettings section="enable" /> ); |
| 49 | + |
| 50 | + expect( updateIsAmazonPayEnabledHandler ).not.toHaveBeenCalled(); |
| 51 | + |
| 52 | + expect( |
| 53 | + screen.getByLabelText( |
| 54 | + 'Enable Amazon Pay as an express payment button' |
| 55 | + ) |
| 56 | + ).toBeChecked(); |
| 57 | + |
| 58 | + userEvent.click( |
| 59 | + screen.getByLabelText( |
| 60 | + 'Enable Amazon Pay as an express payment button' |
| 61 | + ) |
| 62 | + ); |
| 63 | + |
| 64 | + expect( updateIsAmazonPayEnabledHandler ).toHaveBeenCalledWith( false ); |
| 65 | + } ); |
| 66 | + |
| 67 | + it( 'renders location checkboxes as checked when locations are enabled', () => { |
| 68 | + useAmazonPayLocations.mockReturnValue( [ |
| 69 | + [ 'product', 'cart', 'checkout' ], |
| 70 | + jest.fn(), |
| 71 | + ] ); |
| 72 | + |
| 73 | + renderWithSettingsProvider( <AmazonPaySettings section="enable" /> ); |
| 74 | + |
| 75 | + expect( |
| 76 | + screen.getByLabelText( 'Show on checkout page' ) |
| 77 | + ).toBeChecked(); |
| 78 | + expect( screen.getByLabelText( 'Show on product page' ) ).toBeChecked(); |
| 79 | + expect( screen.getByLabelText( 'Show on cart page' ) ).toBeChecked(); |
| 80 | + } ); |
| 81 | + |
| 82 | + it( 'renders location checkboxes as unchecked when locations are disabled', () => { |
| 83 | + useAmazonPayLocations.mockReturnValue( [ [], jest.fn() ] ); |
| 84 | + |
| 85 | + renderWithSettingsProvider( <AmazonPaySettings section="enable" /> ); |
| 86 | + |
| 87 | + expect( |
| 88 | + screen.getByLabelText( 'Show on checkout page' ) |
| 89 | + ).not.toBeChecked(); |
| 90 | + expect( |
| 91 | + screen.getByLabelText( 'Show on product page' ) |
| 92 | + ).not.toBeChecked(); |
| 93 | + expect( |
| 94 | + screen.getByLabelText( 'Show on cart page' ) |
| 95 | + ).not.toBeChecked(); |
| 96 | + } ); |
| 97 | + |
| 98 | + it( 'disables location checkboxes when Amazon Pay is disabled', () => { |
| 99 | + useAmazonPayEnabledSettings.mockReturnValue( [ false, jest.fn() ] ); |
| 100 | + |
| 101 | + renderWithSettingsProvider( <AmazonPaySettings section="enable" /> ); |
| 102 | + |
| 103 | + expect( |
| 104 | + screen.getByLabelText( |
| 105 | + 'Enable Amazon Pay as an express payment button' |
| 106 | + ) |
| 107 | + ).not.toBeChecked(); |
| 108 | + expect( |
| 109 | + screen.getByLabelText( 'Show on checkout page' ) |
| 110 | + ).toBeDisabled(); |
| 111 | + expect( |
| 112 | + screen.getByLabelText( 'Show on product page' ) |
| 113 | + ).toBeDisabled(); |
| 114 | + expect( screen.getByLabelText( 'Show on cart page' ) ).toBeDisabled(); |
| 115 | + } ); |
| 116 | + |
| 117 | + it( 'triggers the location update handler when location checkboxes are clicked', async () => { |
| 118 | + const updateAmazonPayLocationsHandler = jest.fn(); |
| 119 | + useAmazonPayLocations.mockReturnValue( [ |
| 120 | + [ 'product' ], |
| 121 | + updateAmazonPayLocationsHandler, |
| 122 | + ] ); |
| 123 | + |
| 124 | + renderWithSettingsProvider( <AmazonPaySettings section="enable" /> ); |
| 125 | + |
| 126 | + expect( updateAmazonPayLocationsHandler ).not.toHaveBeenCalled(); |
| 127 | + |
| 128 | + userEvent.click( screen.getByLabelText( 'Show on checkout page' ) ); |
| 129 | + expect( updateAmazonPayLocationsHandler ).toHaveBeenLastCalledWith( |
| 130 | + 'checkout', |
| 131 | + true |
| 132 | + ); |
| 133 | + |
| 134 | + userEvent.click( screen.getByLabelText( 'Show on product page' ) ); |
| 135 | + expect( updateAmazonPayLocationsHandler ).toHaveBeenLastCalledWith( |
| 136 | + 'product', |
| 137 | + false |
| 138 | + ); |
| 139 | + |
| 140 | + userEvent.click( screen.getByLabelText( 'Show on cart page' ) ); |
| 141 | + expect( updateAmazonPayLocationsHandler ).toHaveBeenLastCalledWith( |
| 142 | + 'cart', |
| 143 | + true |
| 144 | + ); |
| 145 | + } ); |
| 146 | + |
| 147 | + it( 'renders general settings section with button size options', () => { |
| 148 | + renderWithSettingsProvider( <AmazonPaySettings section="general" /> ); |
| 149 | + |
| 150 | + expect( screen.queryByText( 'Button size' ) ).toBeInTheDocument(); |
| 151 | + |
| 152 | + const radioButtons = screen.queryAllByRole( 'radio' ); |
| 153 | + expect( radioButtons ).toHaveLength( 3 ); |
| 154 | + } ); |
| 155 | +} ); |
0 commit comments