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