2020-03-30 18:32:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import TestRenderer, { act } from 'react-test-renderer';
|
|
|
|
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useCheckoutSubmit } from '../use-checkout-submit';
|
|
|
|
|
|
|
|
const mockUseCheckoutContext = {
|
|
|
|
onSubmit: jest.fn(),
|
|
|
|
};
|
2021-06-16 12:44:40 +00:00
|
|
|
const mockUsePaymentMethodDataContext = {
|
|
|
|
activePaymentMethod: '',
|
|
|
|
currentStatus: {
|
|
|
|
isDoingExpressPayment: false,
|
|
|
|
},
|
|
|
|
};
|
2021-04-08 12:31:12 +00:00
|
|
|
|
|
|
|
jest.mock( '../../providers/cart-checkout/checkout-state', () => ( {
|
2020-03-30 18:32:28 +00:00
|
|
|
useCheckoutContext: () => mockUseCheckoutContext,
|
2021-04-08 12:31:12 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
jest.mock( '../../providers/cart-checkout/payment-methods', () => ( {
|
2020-04-30 09:52:36 +00:00
|
|
|
usePaymentMethodDataContext: () => mockUsePaymentMethodDataContext,
|
|
|
|
} ) );
|
2020-03-30 18:32:28 +00:00
|
|
|
|
|
|
|
describe( 'useCheckoutSubmit', () => {
|
|
|
|
let registry, renderer;
|
|
|
|
|
|
|
|
const getWrappedComponents = ( Component ) => (
|
|
|
|
<RegistryProvider value={ registry }>
|
|
|
|
<Component />
|
|
|
|
</RegistryProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
const getTestComponent = () => () => {
|
|
|
|
const data = useCheckoutSubmit();
|
|
|
|
return <div { ...data } />;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach( () => {
|
|
|
|
registry = createRegistry();
|
|
|
|
renderer = null;
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'onSubmit calls the correct action in the checkout context', () => {
|
|
|
|
const TestComponent = getTestComponent();
|
|
|
|
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponents( TestComponent )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
//eslint-disable-next-line testing-library/await-async-query
|
2020-03-30 18:32:28 +00:00
|
|
|
const { onSubmit } = renderer.root.findByType( 'div' ).props;
|
|
|
|
|
|
|
|
onSubmit();
|
|
|
|
|
|
|
|
expect( mockUseCheckoutContext.onSubmit ).toHaveBeenCalledTimes( 1 );
|
|
|
|
} );
|
|
|
|
} );
|