2021-04-20 17:17:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, fireEvent, waitFor } from '@testing-library/react';
|
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-11-16 13:57:23 +00:00
|
|
|
import PaymentRecommendations from '../payment-recommendations';
|
2021-10-05 18:20:28 +00:00
|
|
|
import { isWCPaySupported } from '../../tasks/fills/PaymentGatewaySuggestions/components/WCPay';
|
2021-04-20 17:17:19 +00:00
|
|
|
import { createNoticesFromResponse } from '~/lib/notices';
|
|
|
|
|
|
|
|
jest.mock( '@woocommerce/tracks', () => ( { recordEvent: jest.fn() } ) );
|
|
|
|
|
|
|
|
jest.mock( '@wordpress/data', () => ( {
|
|
|
|
...jest.requireActual( '@wordpress/data' ),
|
|
|
|
useSelect: jest.fn(),
|
|
|
|
useDispatch: jest.fn().mockImplementation( () => ( {
|
|
|
|
updateOptions: jest.fn(),
|
|
|
|
installAndActivatePlugins: jest.fn(),
|
|
|
|
} ) ),
|
|
|
|
} ) );
|
|
|
|
jest.mock( '@woocommerce/components', () => ( {
|
|
|
|
EllipsisMenu: ( {
|
|
|
|
renderContent: Content,
|
|
|
|
}: {
|
|
|
|
renderContent: React.FunctionComponent;
|
|
|
|
} ) => <Content />,
|
|
|
|
List: ( {
|
|
|
|
items,
|
|
|
|
}: {
|
|
|
|
items: { key: string; title: string; after?: React.Component }[];
|
|
|
|
} ) => (
|
|
|
|
<div>
|
|
|
|
{ items.map( ( item ) => (
|
|
|
|
<div key={ item.key }>
|
|
|
|
<span>{ item.title }</span>
|
|
|
|
{ item.after }
|
|
|
|
</div>
|
|
|
|
) ) }
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
} ) );
|
2021-06-28 18:18:42 +00:00
|
|
|
jest.mock(
|
2021-10-05 18:20:28 +00:00
|
|
|
'../../tasks/fills/PaymentGatewaySuggestions/components/WCPay',
|
2021-06-28 18:18:42 +00:00
|
|
|
() => ( {
|
|
|
|
isWCPaySupported: jest.fn(),
|
|
|
|
} )
|
|
|
|
);
|
2021-04-20 17:17:19 +00:00
|
|
|
|
|
|
|
jest.mock( '../../lib/notices', () => ( {
|
|
|
|
createNoticesFromResponse: jest.fn().mockImplementation( () => {
|
|
|
|
// do nothing
|
|
|
|
} ),
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
describe( 'Payment recommendations', () => {
|
2022-03-07 13:15:06 +00:00
|
|
|
it( 'should render nothing with no paymentGatewaySuggestions and country not defined', () => {
|
2021-04-20 17:17:19 +00:00
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: undefined,
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
const { container } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( container.firstChild ).toBe( null );
|
|
|
|
} );
|
|
|
|
|
2022-03-07 13:15:06 +00:00
|
|
|
it( 'should render the list if displayable is true and has paymentGatewaySuggestions', () => {
|
2021-04-20 17:17:19 +00:00
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: [
|
2021-11-16 13:57:23 +00:00
|
|
|
{ title: 'test', id: 'test', plugins: [ 'test' ] },
|
|
|
|
],
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
const { container, getByText } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( container.firstChild ).not.toBeNull();
|
|
|
|
expect( getByText( 'test' ) ).toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should not trigger event payments_recommendations_pageview, when it is not rendered', () => {
|
|
|
|
( recordEvent as jest.Mock ).mockClear();
|
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
|
|
|
displayable: false,
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
const { container } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( container.firstChild ).toBeNull();
|
|
|
|
expect( recordEvent ).not.toHaveBeenCalledWith(
|
|
|
|
'settings_payments_recommendations_pageview',
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should trigger event payments_recommendations_pageview, when first rendered', () => {
|
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: [
|
2021-11-16 13:57:23 +00:00
|
|
|
{ title: 'test', id: 'test', plugins: [ 'test' ] },
|
2021-09-08 16:41:00 +00:00
|
|
|
],
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
const { container } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( container.firstChild ).not.toBeNull();
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'settings_payments_recommendations_pageview',
|
2021-09-08 16:41:00 +00:00
|
|
|
{
|
|
|
|
test_displayed: true,
|
|
|
|
woocommerce_payments_displayed: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should set woocommerce-payments-displayed prop to true if pre install wc pay promotion gateway is displayed', () => {
|
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: [
|
2021-11-16 13:57:23 +00:00
|
|
|
{ title: 'test', id: 'test', plugins: [ 'test' ] },
|
2021-09-08 16:41:00 +00:00
|
|
|
],
|
|
|
|
} );
|
|
|
|
const { container } = render(
|
|
|
|
<div>
|
|
|
|
<div data-gateway_id="pre_install_woocommerce_payments_promotion"></div>
|
|
|
|
<PaymentRecommendations />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( container.firstChild ).not.toBeNull();
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'settings_payments_recommendations_pageview',
|
|
|
|
{
|
|
|
|
test_displayed: true,
|
|
|
|
woocommerce_payments_displayed: false,
|
|
|
|
}
|
2021-04-20 17:17:19 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2022-03-07 13:15:06 +00:00
|
|
|
it( 'should not render if there are no paymentGatewaySuggestions', () => {
|
2021-04-20 17:17:19 +00:00
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: [],
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
const { container } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( container.firstChild ).toBeNull();
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'interactions', () => {
|
|
|
|
let oldLocation: Location;
|
|
|
|
const mockLocation = {
|
|
|
|
href: 'test',
|
|
|
|
} as Location;
|
|
|
|
const updateOptionsMock = jest.fn();
|
|
|
|
const installAndActivateMock = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation( () => Promise.resolve() );
|
|
|
|
beforeEach( () => {
|
|
|
|
( isWCPaySupported as jest.Mock ).mockReturnValue( true );
|
|
|
|
( useDispatch as jest.Mock ).mockReturnValue( {
|
|
|
|
updateOptions: updateOptionsMock,
|
|
|
|
installAndActivatePlugins: installAndActivateMock,
|
|
|
|
} );
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
2022-03-07 13:15:06 +00:00
|
|
|
installedPaymentGateways: {},
|
|
|
|
installedPaymentGateway: {
|
|
|
|
settings_url: 'https://test.ca/random-link',
|
|
|
|
},
|
|
|
|
paymentGatewaySuggestions: [
|
2021-04-20 17:17:19 +00:00
|
|
|
{
|
|
|
|
title: 'test',
|
2021-11-16 13:57:23 +00:00
|
|
|
id: 'test',
|
|
|
|
plugins: [ 'test-product' ],
|
2022-03-07 13:15:06 +00:00
|
|
|
actionText: 'install',
|
2021-04-20 17:17:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'another',
|
2021-11-16 13:57:23 +00:00
|
|
|
id: 'another',
|
|
|
|
plugins: [ 'another-product' ],
|
2022-03-07 13:15:06 +00:00
|
|
|
actionText: 'install2',
|
2021-04-20 17:17:19 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
|
|
|
|
oldLocation = global.window.location;
|
|
|
|
mockLocation.href = 'test';
|
|
|
|
Object.defineProperty( global.window, 'location', {
|
|
|
|
value: mockLocation,
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
|
|
Object.defineProperty( global.window, 'location', oldLocation );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should install plugin and trigger event and redirect when finished, when clicking the action button', async () => {
|
|
|
|
const { container, getByText } = render(
|
|
|
|
<PaymentRecommendations />
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( container.firstChild ).not.toBeNull();
|
|
|
|
fireEvent.click( getByText( 'install' ) );
|
|
|
|
expect( installAndActivateMock ).toHaveBeenCalledWith( [
|
|
|
|
'test-product',
|
|
|
|
] );
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'settings_payments_recommendations_setup',
|
|
|
|
{
|
|
|
|
extension_selected: 'test-product',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
expect( mockLocation.href ).toEqual(
|
|
|
|
'https://test.ca/random-link'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should call create notice if install and activate failed', async () => {
|
2022-03-07 13:15:06 +00:00
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
|
|
|
installedPaymentGateway: false,
|
|
|
|
installedPaymentGateways: {},
|
|
|
|
paymentGatewaySuggestions: [
|
|
|
|
{
|
|
|
|
title: 'test',
|
|
|
|
id: 'test',
|
|
|
|
plugins: [ 'test-product' ],
|
|
|
|
actionText: 'install',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
2021-04-20 17:17:19 +00:00
|
|
|
installAndActivateMock.mockClear();
|
|
|
|
installAndActivateMock.mockImplementation(
|
|
|
|
() =>
|
2021-07-14 20:38:57 +00:00
|
|
|
new Promise( () => {
|
2021-04-20 17:17:19 +00:00
|
|
|
throw {
|
|
|
|
code: 500,
|
|
|
|
message: 'failed to install plugin',
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
const { container, getByText } = render(
|
|
|
|
<PaymentRecommendations />
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( container.firstChild ).not.toBeNull();
|
|
|
|
fireEvent.click( getByText( 'install' ) );
|
|
|
|
expect( installAndActivateMock ).toHaveBeenCalledWith( [
|
|
|
|
'test-product',
|
|
|
|
] );
|
|
|
|
expect( recordEvent ).toHaveBeenCalledWith(
|
|
|
|
'settings_payments_recommendations_setup',
|
|
|
|
{
|
|
|
|
extension_selected: 'test-product',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await waitFor( () => {
|
|
|
|
expect( createNoticesFromResponse ).toHaveBeenCalled();
|
|
|
|
} );
|
|
|
|
expect( mockLocation.href ).toEqual( 'test' );
|
|
|
|
} );
|
2022-03-07 13:15:06 +00:00
|
|
|
|
|
|
|
it( 'should only show gateways that have not been installed', async () => {
|
|
|
|
( useSelect as jest.Mock ).mockReturnValue( {
|
|
|
|
installedPaymentGateway: false,
|
|
|
|
installedPaymentGateways: {
|
|
|
|
test: true,
|
|
|
|
},
|
|
|
|
paymentGatewaySuggestions: [
|
|
|
|
{
|
|
|
|
title: 'test',
|
|
|
|
id: 'test',
|
|
|
|
plugins: [ 'test-product' ],
|
|
|
|
actionText: 'install',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'another',
|
|
|
|
id: 'another',
|
|
|
|
plugins: [ 'another-product' ],
|
|
|
|
actionText: 'install2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { queryByText } = render( <PaymentRecommendations /> );
|
|
|
|
|
|
|
|
expect( queryByText( 'test' ) ).not.toBeInTheDocument();
|
|
|
|
expect( queryByText( 'another' ) ).toBeInTheDocument();
|
|
|
|
} );
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
} );
|