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';
|
2022-06-10 16:33:15 +00:00
|
|
|
import {
|
|
|
|
CHECKOUT_STORE_KEY,
|
|
|
|
config as checkoutStoreConfig,
|
|
|
|
} from '../../../../data/checkout';
|
2022-08-22 13:56:44 +00:00
|
|
|
import {
|
2022-10-06 12:46:46 +00:00
|
|
|
PAYMENT_STORE_KEY,
|
2022-08-22 13:56:44 +00:00
|
|
|
config as paymentDataStoreConfig,
|
2022-10-05 12:01:56 +00:00
|
|
|
} from '../../../../data/payment';
|
2020-03-30 18:32:28 +00:00
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
jest.mock( '../../providers/cart-checkout/checkout-events', () => {
|
|
|
|
const original = jest.requireActual(
|
|
|
|
'../../providers/cart-checkout/checkout-events'
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
...original,
|
|
|
|
useCheckoutEventsContext: () => {
|
|
|
|
return { onSubmit: jest.fn() };
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
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( () => {
|
2022-06-10 16:33:15 +00:00
|
|
|
registry = createRegistry( {
|
|
|
|
[ CHECKOUT_STORE_KEY ]: checkoutStoreConfig,
|
2022-10-06 12:46:46 +00:00
|
|
|
[ PAYMENT_STORE_KEY ]: paymentDataStoreConfig,
|
2022-06-10 16:33:15 +00:00
|
|
|
} );
|
2020-03-30 18:32:28 +00:00
|
|
|
renderer = null;
|
|
|
|
} );
|
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
it( 'onSubmit calls the correct action in the checkout events context', () => {
|
2020-03-30 18:32:28 +00:00
|
|
|
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();
|
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
expect( onSubmit ).toHaveBeenCalledTimes( 1 );
|
2020-03-30 18:32:28 +00:00
|
|
|
} );
|
|
|
|
} );
|