2022-04-20 07:29:29 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-08-17 03:33:24 +00:00
|
|
|
import { waitFor, render, fireEvent } from '@testing-library/react';
|
2022-04-20 07:29:29 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2022-08-17 03:33:24 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2022-04-20 07:29:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PaymentsBannerWrapper } from '../payment-settings-banner';
|
|
|
|
|
|
|
|
jest.mock( '@wordpress/data', () => ( {
|
|
|
|
...jest.requireActual( '@wordpress/data' ),
|
|
|
|
useSelect: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
jest.mock( '@woocommerce/explat' );
|
2022-08-17 03:33:24 +00:00
|
|
|
jest.mock( '@woocommerce/tracks', () => ( { recordEvent: jest.fn() } ) );
|
2022-06-24 05:06:11 +00:00
|
|
|
|
|
|
|
const paymentsBannerShouldBe = async ( status: 'hidden' | 'visible' ) => {
|
|
|
|
const { container } = render( <PaymentsBannerWrapper /> );
|
|
|
|
|
|
|
|
await waitFor( () => {
|
2024-04-25 13:24:05 +00:00
|
|
|
container.querySelector( '.woocommerce-recommended-payments-banner' );
|
2022-06-24 05:06:11 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
const banner = expect(
|
2024-04-25 13:24:05 +00:00
|
|
|
container.querySelector( '.woocommerce-recommended-payments-banner' )
|
2022-06-24 05:06:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return status === 'visible'
|
|
|
|
? banner.toBeInTheDocument()
|
|
|
|
: banner.not.toBeInTheDocument();
|
|
|
|
};
|
|
|
|
|
|
|
|
const whenWcPay = ( {
|
|
|
|
supported,
|
|
|
|
activated,
|
|
|
|
installed,
|
|
|
|
}: {
|
|
|
|
supported: boolean;
|
|
|
|
activated: boolean;
|
|
|
|
installed: boolean;
|
|
|
|
} ) => {
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
|
|
|
installedPaymentGateways: [
|
|
|
|
installed ? { id: 'woocommerce_payments', enabled: activated } : {},
|
|
|
|
],
|
|
|
|
paymentGatewaySuggestions: supported
|
|
|
|
? [ { id: 'woocommerce_payments:us' } ]
|
|
|
|
: [],
|
|
|
|
hasFinishedResolution: true,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-04-20 07:29:29 +00:00
|
|
|
describe( 'Payment Settings Banner', () => {
|
2022-06-24 05:06:11 +00:00
|
|
|
it( 'should render the banner if woocommerce payments is supported but setup not completed', async () => {
|
|
|
|
expect.assertions( 1 );
|
|
|
|
|
|
|
|
whenWcPay( { supported: true, activated: false, installed: true } );
|
|
|
|
|
|
|
|
await paymentsBannerShouldBe( 'visible' );
|
2022-04-20 07:29:29 +00:00
|
|
|
} );
|
|
|
|
|
2022-06-24 05:06:11 +00:00
|
|
|
it( 'should not render anything if woocommerce payments is not supported', async () => {
|
|
|
|
expect.assertions( 1 );
|
|
|
|
|
|
|
|
whenWcPay( { supported: false, activated: false, installed: false } );
|
|
|
|
|
|
|
|
await paymentsBannerShouldBe( 'hidden' );
|
2022-04-20 07:29:29 +00:00
|
|
|
} );
|
|
|
|
|
2022-06-24 05:06:11 +00:00
|
|
|
it( 'should not render anything if woocommerce payments is setup', async () => {
|
|
|
|
expect.assertions( 1 );
|
|
|
|
|
|
|
|
whenWcPay( { supported: true, activated: true, installed: true } );
|
|
|
|
|
|
|
|
await paymentsBannerShouldBe( 'hidden' );
|
2022-04-20 07:29:29 +00:00
|
|
|
} );
|
2022-08-17 03:33:24 +00:00
|
|
|
|
|
|
|
it( 'should record track when clicking the action button', async () => {
|
|
|
|
whenWcPay( { supported: true, activated: false, installed: true } );
|
|
|
|
|
|
|
|
const { getByText } = render( <PaymentsBannerWrapper /> );
|
2024-04-25 13:24:05 +00:00
|
|
|
fireEvent.click( getByText( 'Get started' ) );
|
2022-08-17 03:33:24 +00:00
|
|
|
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'settings_payments_banner_connect_click'
|
|
|
|
);
|
|
|
|
} );
|
2022-04-20 07:29:29 +00:00
|
|
|
} );
|