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';
|
2023-05-17 01:54:31 +00:00
|
|
|
import { isWCPaySupported } from '../../task-lists/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(
|
2023-05-17 01:54:31 +00:00
|
|
|
'../../task-lists/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( () => {
|
Update the monorepo to node v20 (#45148)
* test bumping node to v18
* remove community contributor condition from review assignment
* Add changefile(s) from automation for the following project(s): @woocommerce/tracks, @woocommerce/onboarding, @woocommerce/number, @woocommerce/notices, @woocommerce/navigation, @woocommerce/internal-js-tests, @woocommerce/extend-cart-checkout-block, @woocommerce/expression-evaluation, @woocommerce/explat, @woocommerce/experimental, @woocommerce/eslint-plugin, @woocommerce/dependency-extraction-webpack-plugin, @woocommerce/date, @woocommerce/data, @woocommerce/customer-effort-score, @woocommerce/currency, @woocommerce/csv-export, @woocommerce/create-woo-extension, @woocommerce/create-product-editor-block, @woocommerce/components, @woocommerce/api, @woocommerce/admin-e2e-tests, woocommerce-blocks, woocommerce-beta-tester, woocommerce, woo-ai
* bump node to v20
* Add changefile(s) from automation for the following project(s): @woocommerce/tracks, @woocommerce/onboarding, @woocommerce/number, @woocommerce/notices, @woocommerce/navigation, @woocommerce/internal-js-tests, @woocommerce/extend-cart-checkout-block, @woocommerce/expression-evaluation, @woocommerce/explat, @woocommerce/experimental, @woocommerce/eslint-plugin, @woocommerce/dependency-extraction-webpack-plugin, @woocommerce/date, @woocommerce/data, @woocommerce/customer-effort-score, @woocommerce/currency, @woocommerce/csv-export, @woocommerce/create-woo-extension, @woocommerce/create-product-editor-block, @woocommerce/components, @woocommerce/api, @woocommerce/admin-e2e-tests, packages/php/remote-specs-validation, woocommerce-blocks, woocommerce-beta-tester, woocommerce, woo-ai
* Add changefile(s) from automation for the following project(s): @woocommerce/tracks, @woocommerce/onboarding, @woocommerce/number, @woocommerce/notices, @woocommerce/navigation, @woocommerce/internal-js-tests, @woocommerce/extend-cart-checkout-block, @woocommerce/expression-evaluation, @woocommerce/explat, @woocommerce/experimental, @woocommerce/eslint-plugin, @woocommerce/dependency-extraction-webpack-plugin, @woocommerce/date, @woocommerce/data, @woocommerce/customer-effort-score, @woocommerce/currency, @woocommerce/csv-export, @woocommerce/create-woo-extension, @woocommerce/create-product-editor-block, @woocommerce/components, @woocommerce/api, @woocommerce/admin-e2e-tests, woocommerce-blocks, woocommerce-beta-tester, woocommerce, woo-ai
* add blocks eslint-plugin-woocommerce to project workspace
* add e2e-environment as a peer to e2e-utils
* restore version on @woocommerce/api
* update lock file
* move e2e-environment to devDependencies
* add undefined location check to admin js test
---------
Co-authored-by: Ron Rennick <ronald.rennick@automattic.com>
Co-authored-by: github-actions <github-actions@github.com>
2024-04-12 16:49:55 +00:00
|
|
|
if ( oldLocation !== undefined ) {
|
|
|
|
Object.defineProperty( global.window, 'location', oldLocation );
|
|
|
|
}
|
2021-04-20 17:17:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
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
|
|
|
} );
|
|
|
|
} );
|