2020-10-12 12:43:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-08-22 13:56:44 +00:00
|
|
|
import { act, render, screen, waitFor } from '@testing-library/react';
|
2021-12-08 13:41:31 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2022-08-22 13:56:44 +00:00
|
|
|
import * as wpDataFunctions from '@wordpress/data';
|
2022-10-06 12:46:46 +00:00
|
|
|
import { CART_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';
|
2021-12-08 13:41:31 +00:00
|
|
|
import { default as fetchMock } from 'jest-fetch-mock';
|
2020-10-12 12:43:52 +00:00
|
|
|
import {
|
|
|
|
registerPaymentMethod,
|
|
|
|
__experimentalDeRegisterPaymentMethod,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
2022-01-31 10:53:21 +00:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-08-22 13:56:44 +00:00
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
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',
|
2022-06-15 09:56:52 +00:00
|
|
|
() =>
|
|
|
|
( { onChange } ) =>
|
|
|
|
(
|
|
|
|
<>
|
|
|
|
<span>Payment method options</span>
|
|
|
|
<button onClick={ () => onChange( 'credit-card' ) }>
|
|
|
|
Select new payment
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)
|
2021-02-19 15:16:39 +00:00
|
|
|
);
|
2020-10-12 12:43:52 +00:00
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
const originalSelect = jest.requireActual( '@wordpress/data' ).select;
|
|
|
|
const selectMock = jest
|
|
|
|
.spyOn( wpDataFunctions, 'select' )
|
|
|
|
.mockImplementation( ( storeName ) => {
|
|
|
|
const originalStore = originalSelect( storeName );
|
2022-10-06 12:46:46 +00:00
|
|
|
if ( storeName === PAYMENT_STORE_KEY ) {
|
2022-08-22 13:56:44 +00:00
|
|
|
return {
|
|
|
|
...originalStore,
|
|
|
|
getState: () => {
|
|
|
|
const originalState = originalStore.getState();
|
|
|
|
return {
|
|
|
|
...originalState,
|
|
|
|
savedPaymentMethods: {},
|
|
|
|
availablePaymentMethods: {},
|
|
|
|
paymentMethodsInitialized: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
} );
|
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
const registerMockPaymentMethods = () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
[ 'cod', 'credit-card' ].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
|
|
|
} );
|
2022-10-20 15:02:43 +00:00
|
|
|
dispatch( PAYMENT_STORE_KEY ).__internalUpdateAvailablePaymentMethods();
|
2020-10-12 12:43:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const resetMockPaymentMethods = () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
[ 'cod', 'credit-card' ].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 ) => {
|
2022-02-23 12:00:45 +00:00
|
|
|
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
|
2021-12-08 13:41:31 +00:00
|
|
|
return Promise.resolve( JSON.stringify( previewCart ) );
|
|
|
|
}
|
|
|
|
return Promise.resolve( '' );
|
|
|
|
} );
|
|
|
|
// need to clear the store resolution state between tests.
|
2022-08-22 13:56:44 +00:00
|
|
|
wpDataFunctions
|
|
|
|
.dispatch( CART_STORE_KEY )
|
|
|
|
.invalidateResolutionForStore();
|
2023-03-13 10:29:17 +00:00
|
|
|
wpDataFunctions.dispatch( CART_STORE_KEY ).receiveCart( {
|
|
|
|
...previewCart,
|
|
|
|
payment_methods: [ 'cod', 'credit-card' ],
|
|
|
|
} );
|
2021-12-08 13:41:31 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
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 () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
render( <PaymentMethods /> );
|
2020-10-12 12:43:52 +00:00
|
|
|
|
|
|
|
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 );
|
2022-08-22 13:56:44 +00:00
|
|
|
|
|
|
|
// Reset the mock back to how it was because we don't need it anymore after this test.
|
|
|
|
selectMock.mockRestore();
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-02-02 04:51:47 +00:00
|
|
|
test( 'selecting new payment method', async () => {
|
|
|
|
const ShowActivePaymentMethod = () => {
|
2022-06-15 09:56:52 +00:00
|
|
|
const { activePaymentMethod, activeSavedToken } =
|
2022-08-22 13:56:44 +00:00
|
|
|
wpDataFunctions.useSelect( ( select ) => {
|
2022-10-06 12:46:46 +00:00
|
|
|
const store = select( PAYMENT_STORE_KEY );
|
2022-08-22 13:56:44 +00:00
|
|
|
return {
|
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
activeSavedToken: store.getActiveSavedToken(),
|
|
|
|
};
|
|
|
|
} );
|
2021-02-02 04:51:47 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
{ 'Active Payment Method: ' + activePaymentMethod }
|
|
|
|
</div>
|
|
|
|
<div>{ 'Active Saved Token: ' + activeSavedToken }</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
act( () => {
|
|
|
|
registerMockPaymentMethods();
|
|
|
|
} );
|
|
|
|
// Wait for the payment methods to finish loading before rendering.
|
|
|
|
await waitFor( () => {
|
|
|
|
expect(
|
|
|
|
wpDataFunctions
|
2022-10-06 12:46:46 +00:00
|
|
|
.select( PAYMENT_STORE_KEY )
|
2022-08-22 13:56:44 +00:00
|
|
|
.getActivePaymentMethod()
|
|
|
|
).toBe( 'cod' );
|
|
|
|
} );
|
|
|
|
|
2020-10-12 12:43:52 +00:00
|
|
|
render(
|
2022-08-22 13:56:44 +00:00
|
|
|
<>
|
2020-10-12 12:43:52 +00:00
|
|
|
<PaymentMethods />
|
2021-02-02 04:51:47 +00:00
|
|
|
<ShowActivePaymentMethod />
|
2022-08-22 13:56:44 +00:00
|
|
|
</>
|
2020-10-12 12:43:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await waitFor( () => {
|
|
|
|
const savedPaymentMethodOptions = screen.queryByText(
|
|
|
|
/Saved payment method options/
|
|
|
|
);
|
2022-04-08 13:47:19 +00:00
|
|
|
expect( savedPaymentMethodOptions ).not.toBeNull();
|
|
|
|
} );
|
|
|
|
|
|
|
|
await waitFor( () => {
|
2020-10-12 12:43:52 +00:00
|
|
|
const paymentMethodOptions = screen.queryByText(
|
|
|
|
/Payment method options/
|
|
|
|
);
|
|
|
|
expect( paymentMethodOptions ).not.toBeNull();
|
2022-04-08 13:47:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
await waitFor( () => {
|
2021-02-02 04:51:47 +00:00
|
|
|
const savedToken = screen.queryByText(
|
2022-01-11 10:53:38 +00:00
|
|
|
/Active Payment Method: credit-card/
|
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
|
|
|
} );
|
|
|
|
|
2022-01-31 10:53:21 +00:00
|
|
|
userEvent.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(
|
2022-01-11 10:53:38 +00:00
|
|
|
/Active Payment Method: credit-card/
|
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
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
act( () => resetMockPaymentMethods() );
|
2020-10-12 12:43:52 +00:00
|
|
|
} );
|
|
|
|
} );
|