2020-10-12 12:43:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
|
|
|
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
|
|
|
|
2021-09-24 09:47:05 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import * as useStoreCartHook from '../../../../base/context/hooks/cart/use-store-cart';
|
|
|
|
|
|
|
|
// Somewhere in your test case or test suite
|
|
|
|
useStoreCartHook.useStoreCart = jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue( useStoreCartHook.defaultCartData );
|
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import PaymentMethods from '../payment-methods';
|
|
|
|
|
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', () => {
|
|
|
|
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-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();
|
|
|
|
} );
|
|
|
|
} );
|