2020-10-12 12:43:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
2021-12-08 13:41:31 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { default as fetchMock } from 'jest-fetch-mock';
|
2020-10-12 12:43:52 +00:00
|
|
|
import {
|
|
|
|
registerPaymentMethod,
|
|
|
|
__experimentalDeRegisterPaymentMethod,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
2021-02-02 04:51:47 +00:00
|
|
|
import {
|
|
|
|
PaymentMethodDataProvider,
|
|
|
|
usePaymentMethodDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-10-12 12:43:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import PaymentMethods from '../payment-methods';
|
2021-12-08 13:41:31 +00:00
|
|
|
import { defaultCartState } from '../../../../data/default-states';
|
2020-10-12 12:43:52 +00:00
|
|
|
|
2021-02-02 04:51:47 +00:00
|
|
|
jest.mock( '../saved-payment-method-options', () => ( { onChange } ) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<span>Saved payment method options</span>
|
|
|
|
<button onClick={ () => onChange( '0' ) }>Select saved</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2021-02-19 15:16:39 +00:00
|
|
|
jest.mock(
|
|
|
|
'@woocommerce/base-components/radio-control-accordion',
|
|
|
|
() => ( { onChange } ) => (
|
|
|
|
<>
|
|
|
|
<span>Payment method options</span>
|
|
|
|
<button onClick={ () => onChange( 'stripe' ) }>
|
|
|
|
Select new payment
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
);
|
2020-10-12 12:43:52 +00:00
|
|
|
|
|
|
|
const registerMockPaymentMethods = () => {
|
2021-02-02 04:51:47 +00:00
|
|
|
[ 'stripe' ].forEach( ( name ) => {
|
2020-11-18 22:06:33 +00:00
|
|
|
registerPaymentMethod( {
|
|
|
|
name,
|
|
|
|
label: name,
|
|
|
|
content: <div>A payment method</div>,
|
|
|
|
edit: <div>A payment method</div>,
|
|
|
|
icons: null,
|
|
|
|
canMakePayment: () => true,
|
2021-01-29 06:28:44 +00:00
|
|
|
supports: {
|
2021-02-02 04:51:47 +00:00
|
|
|
showSavedCards: true,
|
|
|
|
showSaveOption: true,
|
2021-01-29 06:28:44 +00:00
|
|
|
features: [ 'products' ],
|
|
|
|
},
|
2021-02-02 04:51:47 +00:00
|
|
|
ariaLabel: name,
|
2020-11-18 22:06:33 +00:00
|
|
|
} );
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetMockPaymentMethods = () => {
|
2021-02-02 04:51:47 +00:00
|
|
|
[ 'stripe' ].forEach( ( name ) => {
|
2020-10-12 12:43:52 +00:00
|
|
|
__experimentalDeRegisterPaymentMethod( name );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'PaymentMethods', () => {
|
2021-12-08 13:41:31 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
|
|
|
if ( req.url.match( /wc\/store\/cart/ ) ) {
|
|
|
|
return Promise.resolve( JSON.stringify( previewCart ) );
|
|
|
|
}
|
|
|
|
return Promise.resolve( '' );
|
|
|
|
} );
|
|
|
|
// need to clear the store resolution state between tests.
|
|
|
|
dispatch( storeKey ).invalidateResolutionForStore();
|
|
|
|
dispatch( storeKey ).receiveCart( defaultCartState.cartData );
|
|
|
|
} );
|
|
|
|
|
|
|
|
afterEach( () => {
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
} );
|
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
test( 'should show no payment methods component when there are no payment methods', async () => {
|
|
|
|
render(
|
|
|
|
<PaymentMethodDataProvider>
|
|
|
|
<PaymentMethods />
|
|
|
|
</PaymentMethodDataProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor( () => {
|
2020-12-03 13:04:25 +00:00
|
|
|
const noPaymentMethods = screen.queryAllByText(
|
2020-10-12 12:43:52 +00:00
|
|
|
/no payment methods available/
|
|
|
|
);
|
2020-12-03 13:04:25 +00:00
|
|
|
// We might get more than one match because the `speak()` function
|
|
|
|
// creates an extra `div` with the notice contents used for a11y.
|
|
|
|
expect( noPaymentMethods.length ).toBeGreaterThanOrEqual( 1 );
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
2021-12-08 13:41:31 +00:00
|
|
|
// ["`select` control in `@wordpress/data-controls` is deprecated. Please use built-in `resolveSelect` control in `@wordpress/data` instead."]
|
|
|
|
expect( console ).toHaveWarned();
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
|
|
|
|
2021-02-02 04:51:47 +00:00
|
|
|
test( 'selecting new payment method', async () => {
|
|
|
|
const ShowActivePaymentMethod = () => {
|
|
|
|
const {
|
|
|
|
activePaymentMethod,
|
|
|
|
activeSavedToken,
|
|
|
|
} = usePaymentMethodDataContext();
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
{ 'Active Payment Method: ' + activePaymentMethod }
|
|
|
|
</div>
|
|
|
|
<div>{ 'Active Saved Token: ' + activeSavedToken }</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
registerMockPaymentMethods();
|
|
|
|
render(
|
|
|
|
<PaymentMethodDataProvider>
|
|
|
|
<PaymentMethods />
|
2021-02-02 04:51:47 +00:00
|
|
|
<ShowActivePaymentMethod />
|
2020-10-12 12:43:52 +00:00
|
|
|
</PaymentMethodDataProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor( () => {
|
|
|
|
const savedPaymentMethodOptions = screen.queryByText(
|
|
|
|
/Saved payment method options/
|
|
|
|
);
|
|
|
|
const paymentMethodOptions = screen.queryByText(
|
|
|
|
/Payment method options/
|
|
|
|
);
|
|
|
|
expect( savedPaymentMethodOptions ).not.toBeNull();
|
|
|
|
expect( paymentMethodOptions ).not.toBeNull();
|
2021-02-02 04:51:47 +00:00
|
|
|
const savedToken = screen.queryByText(
|
|
|
|
/Active Payment Method: stripe/
|
2020-10-12 12:43:52 +00:00
|
|
|
);
|
2021-02-02 04:51:47 +00:00
|
|
|
expect( savedToken ).toBeNull();
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
|
|
|
|
2021-02-02 04:51:47 +00:00
|
|
|
fireEvent.click( screen.getByText( 'Select new payment' ) );
|
2020-10-12 12:43:52 +00:00
|
|
|
|
|
|
|
await waitFor( () => {
|
2021-02-02 04:51:47 +00:00
|
|
|
const activePaymentMethod = screen.queryByText(
|
|
|
|
/Active Payment Method: stripe/
|
2020-10-12 12:43:52 +00:00
|
|
|
);
|
2021-02-02 04:51:47 +00:00
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
2021-02-02 04:51:47 +00:00
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
resetMockPaymentMethods();
|
|
|
|
} );
|
|
|
|
} );
|