2020-09-24 14:45:40 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-01-31 10:53:21 +00:00
|
|
|
import { render, screen, waitFor, act } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-09-24 14:45:40 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2022-08-22 13:56:44 +00:00
|
|
|
import * as wpDataFunctions from '@wordpress/data';
|
|
|
|
import {
|
|
|
|
CART_STORE_KEY as storeKey,
|
|
|
|
PAYMENT_METHOD_DATA_STORE_KEY,
|
|
|
|
} from '@woocommerce/block-data';
|
2020-09-24 14:45:40 +00:00
|
|
|
import {
|
|
|
|
registerPaymentMethod,
|
|
|
|
registerExpressPaymentMethod,
|
|
|
|
__experimentalDeRegisterPaymentMethod,
|
|
|
|
__experimentalDeRegisterExpressPaymentMethod,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
2021-02-24 01:36:24 +00:00
|
|
|
import { default as fetchMock } from 'jest-fetch-mock';
|
2021-02-19 15:16:39 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-02-19 15:16:39 +00:00
|
|
|
import {
|
|
|
|
CheckoutExpressPayment,
|
|
|
|
SavedPaymentMethodOptions,
|
2022-08-22 13:56:44 +00:00
|
|
|
} from '../../../blocks/cart-checkout-shared/payment-methods';
|
2022-10-05 12:01:56 +00:00
|
|
|
import { checkPaymentMethodsCanPay } from '../check-payment-methods';
|
2022-09-21 09:18:14 +00:00
|
|
|
import { defaultCartState } from '../../cart/default-state';
|
2022-08-22 13:56:44 +00:00
|
|
|
|
|
|
|
const originalSelect = jest.requireActual( '@wordpress/data' ).select;
|
|
|
|
jest.spyOn( wpDataFunctions, 'select' ).mockImplementation( ( storeName ) => {
|
|
|
|
const originalStore = originalSelect( storeName );
|
|
|
|
if ( storeName === storeKey ) {
|
|
|
|
return {
|
|
|
|
...originalStore,
|
|
|
|
hasFinishedResolution: jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation( ( selectorName ) => {
|
|
|
|
if ( selectorName === 'getCartTotals' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return originalStore.hasFinishedResolution( selectorName );
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
} );
|
2020-09-24 14:45:40 +00:00
|
|
|
|
2020-10-08 09:21:47 +00:00
|
|
|
jest.mock( '@woocommerce/settings', () => {
|
|
|
|
const originalModule = jest.requireActual( '@woocommerce/settings' );
|
2021-02-19 15:16:39 +00:00
|
|
|
|
2020-10-08 09:21:47 +00:00
|
|
|
return {
|
|
|
|
// @ts-ignore We know @woocommerce/settings is an object.
|
|
|
|
...originalModule,
|
|
|
|
getSetting: ( setting, ...rest ) => {
|
|
|
|
if ( setting === 'customerPaymentMethods' ) {
|
|
|
|
return {
|
|
|
|
cc: [
|
|
|
|
{
|
|
|
|
method: {
|
2022-01-11 10:53:38 +00:00
|
|
|
gateway: 'credit-card',
|
2020-10-08 09:21:47 +00:00
|
|
|
last4: '4242',
|
|
|
|
brand: 'Visa',
|
|
|
|
},
|
|
|
|
expires: '12/22',
|
|
|
|
is_default: true,
|
|
|
|
tokenId: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalModule.getSetting( setting, ...rest );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2021-12-10 15:26:16 +00:00
|
|
|
const registerMockPaymentMethods = ( savedCards = true ) => {
|
2020-09-24 14:45:40 +00:00
|
|
|
[ 'cheque', 'bacs' ].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: {
|
|
|
|
features: [ 'products' ],
|
|
|
|
},
|
2020-11-18 22:06:33 +00:00
|
|
|
ariaLabel: name,
|
|
|
|
} );
|
2020-09-24 14:45:40 +00:00
|
|
|
} );
|
2022-01-11 10:53:38 +00:00
|
|
|
[ '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,
|
|
|
|
supports: {
|
2021-12-10 15:26:16 +00:00
|
|
|
showSavedCards: savedCards,
|
2021-01-24 13:59:13 +00:00
|
|
|
showSaveOption: true,
|
2021-01-29 06:28:44 +00:00
|
|
|
features: [ 'products' ],
|
2020-11-18 22:06:33 +00:00
|
|
|
},
|
|
|
|
ariaLabel: name,
|
|
|
|
} );
|
2020-10-08 09:21:47 +00:00
|
|
|
} );
|
2020-09-24 14:45:40 +00:00
|
|
|
[ 'express-payment' ].forEach( ( name ) => {
|
2020-11-18 22:06:33 +00:00
|
|
|
const Content = ( {
|
|
|
|
onClose = () => void null,
|
|
|
|
onClick = () => void null,
|
|
|
|
} ) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<button onClick={ onClick }>
|
|
|
|
{ name + ' express payment method' }
|
|
|
|
</button>
|
|
|
|
<button onClick={ onClose }>
|
|
|
|
{ name + ' express payment method close' }
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
registerExpressPaymentMethod( {
|
|
|
|
name,
|
|
|
|
content: <Content />,
|
|
|
|
edit: <div>An express payment method</div>,
|
|
|
|
canMakePayment: () => true,
|
|
|
|
paymentMethodId: name,
|
2021-01-29 06:28:44 +00:00
|
|
|
supports: {
|
|
|
|
features: [ 'products' ],
|
|
|
|
},
|
2020-09-24 14:45:40 +00:00
|
|
|
} );
|
|
|
|
} );
|
2022-09-21 09:18:14 +00:00
|
|
|
checkPaymentMethodsCanPay();
|
|
|
|
checkPaymentMethodsCanPay( true );
|
2020-09-24 14:45:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const resetMockPaymentMethods = () => {
|
2022-01-11 10:53:38 +00:00
|
|
|
[ 'cheque', 'bacs', 'credit-card' ].forEach( ( name ) => {
|
2020-09-24 14:45:40 +00:00
|
|
|
__experimentalDeRegisterPaymentMethod( name );
|
|
|
|
} );
|
|
|
|
[ 'express-payment' ].forEach( ( name ) => {
|
|
|
|
__experimentalDeRegisterExpressPaymentMethod( name );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
describe( 'Payment method data store selectors/thunks', () => {
|
2021-12-07 15:47:50 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
act( () => {
|
2021-12-10 15:26:16 +00:00
|
|
|
registerMockPaymentMethods( false );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2022-02-23 12:00:45 +00:00
|
|
|
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
|
2021-12-07 15:47:50 +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( storeKey ).invalidateResolutionForStore();
|
|
|
|
wpDataFunctions
|
|
|
|
.dispatch( storeKey )
|
|
|
|
.receiveCart( defaultCartState.cartData );
|
2020-09-24 14:45:40 +00:00
|
|
|
} );
|
|
|
|
} );
|
2021-12-10 15:26:16 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
afterEach( async () => {
|
2021-12-07 15:47:50 +00:00
|
|
|
act( () => {
|
|
|
|
resetMockPaymentMethods();
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
} );
|
2020-09-24 14:45:40 +00:00
|
|
|
} );
|
2021-12-10 15:26:16 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
it( 'toggles active payment method correctly for express payment activation and close', async () => {
|
|
|
|
const TriggerActiveExpressPaymentMethod = () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
const activePaymentMethod = wpDataFunctions.useSelect(
|
|
|
|
( select ) => {
|
|
|
|
return select(
|
|
|
|
PAYMENT_METHOD_DATA_STORE_KEY
|
|
|
|
).getActivePaymentMethod();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CheckoutExpressPayment />
|
|
|
|
{ 'Active Payment Method: ' + activePaymentMethod }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const TestComponent = () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
return <TriggerActiveExpressPaymentMethod />;
|
2020-09-24 14:45:40 +00:00
|
|
|
};
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
render( <TestComponent /> );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
// should initialize by default the first payment method.
|
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
2022-08-22 13:56:44 +00:00
|
|
|
/Active Payment Method: credit-card/
|
2020-09-24 14:45:40 +00:00
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
|
|
|
} );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
// Express payment method clicked.
|
|
|
|
userEvent.click(
|
|
|
|
screen.getByText( 'express-payment express payment method' )
|
|
|
|
);
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
|
|
|
/Active Payment Method: express-payment/
|
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
|
|
|
} );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
// Express payment method closed.
|
|
|
|
userEvent.click(
|
|
|
|
screen.getByText( 'express-payment express payment method close' )
|
|
|
|
);
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2020-09-24 14:45:40 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
2022-08-22 13:56:44 +00:00
|
|
|
/Active Payment Method: credit-card/
|
2020-09-24 14:45:40 +00:00
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
|
|
|
} );
|
|
|
|
} );
|
2021-12-10 15:26:16 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
describe( 'Testing Payment Methods work correctly with saved cards turned on', () => {
|
2021-12-10 15:26:16 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
act( () => {
|
|
|
|
registerMockPaymentMethods( true );
|
|
|
|
|
|
|
|
fetchMock.mockResponse( ( req ) => {
|
2022-02-23 12:00:45 +00:00
|
|
|
if ( req.url.match( /wc\/store\/v1\/cart/ ) ) {
|
2021-12-10 15:26:16 +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( storeKey ).invalidateResolutionForStore();
|
|
|
|
wpDataFunctions
|
|
|
|
.dispatch( storeKey )
|
|
|
|
.receiveCart( defaultCartState.cartData );
|
2021-12-10 15:26:16 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
afterEach( async () => {
|
|
|
|
act( () => {
|
|
|
|
resetMockPaymentMethods();
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
} );
|
|
|
|
} );
|
2020-10-08 09:21:47 +00:00
|
|
|
|
|
|
|
it( 'resets saved payment method data after starting and closing an express payment method', async () => {
|
|
|
|
const TriggerActiveExpressPaymentMethod = () => {
|
2022-06-15 09:56:52 +00:00
|
|
|
const { activePaymentMethod, paymentMethodData } =
|
2022-08-22 13:56:44 +00:00
|
|
|
wpDataFunctions.useSelect( ( select ) => {
|
|
|
|
const store = select( PAYMENT_METHOD_DATA_STORE_KEY );
|
|
|
|
return {
|
|
|
|
activePaymentMethod: store.getActivePaymentMethod(),
|
|
|
|
paymentMethodData: store.getPaymentMethodData(),
|
|
|
|
};
|
|
|
|
} );
|
2020-10-08 09:21:47 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CheckoutExpressPayment />
|
2020-10-12 12:43:52 +00:00
|
|
|
<SavedPaymentMethodOptions onChange={ () => void null } />
|
2020-10-08 09:21:47 +00:00
|
|
|
{ 'Active Payment Method: ' + activePaymentMethod }
|
2022-01-11 10:53:38 +00:00
|
|
|
{ paymentMethodData[ 'wc-credit-card-payment-token' ] && (
|
|
|
|
<span>credit-card token</span>
|
2020-10-08 09:21:47 +00:00
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const TestComponent = () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
return <TriggerActiveExpressPaymentMethod />;
|
2020-10-08 09:21:47 +00:00
|
|
|
};
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
render( <TestComponent /> );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2020-10-08 09:21:47 +00:00
|
|
|
// Should initialize by default the default saved payment method.
|
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
2022-01-11 10:53:38 +00:00
|
|
|
/Active Payment Method: credit-card/
|
2020-10-08 09:21:47 +00:00
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
|
|
|
} );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const creditCardToken = screen.queryByText( /credit-card token/ );
|
|
|
|
expect( creditCardToken ).not.toBeNull();
|
2021-12-07 15:47:50 +00:00
|
|
|
} );
|
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
// Express payment method clicked.
|
|
|
|
userEvent.click(
|
|
|
|
screen.getByText( 'express-payment express payment method' )
|
|
|
|
);
|
|
|
|
|
2020-10-08 09:21:47 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
|
|
|
/Active Payment Method: express-payment/
|
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
|
|
|
} );
|
2021-12-07 15:47:50 +00:00
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const creditCardToken = screen.queryByText( /credit-card token/ );
|
|
|
|
expect( creditCardToken ).toBeNull();
|
2021-12-07 15:47:50 +00:00
|
|
|
} );
|
|
|
|
|
2022-04-08 13:47:19 +00:00
|
|
|
// Express payment method closed.
|
|
|
|
userEvent.click(
|
|
|
|
screen.getByText( 'express-payment express payment method close' )
|
|
|
|
);
|
|
|
|
|
2020-10-08 09:21:47 +00:00
|
|
|
await waitFor( () => {
|
|
|
|
const activePaymentMethod = screen.queryByText(
|
2022-01-11 10:53:38 +00:00
|
|
|
/Active Payment Method: credit-card/
|
2020-10-08 09:21:47 +00:00
|
|
|
);
|
|
|
|
expect( activePaymentMethod ).not.toBeNull();
|
2022-04-08 13:47:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
await waitFor( () => {
|
|
|
|
const creditCardToken = screen.queryByText( /credit-card token/ );
|
2022-01-11 10:53:38 +00:00
|
|
|
expect( creditCardToken ).not.toBeNull();
|
2020-10-08 09:21:47 +00:00
|
|
|
} );
|
|
|
|
} );
|
2020-09-24 14:45:40 +00:00
|
|
|
} );
|