2022-08-22 13:56:44 +00:00
|
|
|
/* eslint-disable no-unused-expressions */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import * as wpDataFunctions from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-11-16 09:38:48 +00:00
|
|
|
import { setDefaultPaymentMethod } from '../utils/set-default-payment-method';
|
2022-10-07 17:32:17 +00:00
|
|
|
import { PlainPaymentMethods } from '../../../types';
|
2022-10-06 12:46:46 +00:00
|
|
|
import { PAYMENT_STORE_KEY } from '..';
|
2022-08-22 13:56:44 +00:00
|
|
|
|
|
|
|
const originalSelect = jest.requireActual( '@wordpress/data' ).select;
|
|
|
|
|
|
|
|
describe( 'setDefaultPaymentMethod', () => {
|
|
|
|
afterEach( () => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
jest.resetModules();
|
|
|
|
} );
|
|
|
|
|
2022-10-07 17:32:17 +00:00
|
|
|
const paymentMethods: PlainPaymentMethods = {
|
2022-08-22 13:56:44 +00:00
|
|
|
'wc-payment-gateway-1': {
|
|
|
|
name: 'wc-payment-gateway-1',
|
|
|
|
},
|
|
|
|
'wc-payment-gateway-2': {
|
|
|
|
name: 'wc-payment-gateway-2',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-11 19:14:18 +00:00
|
|
|
it( 'correctly sets the first payment method in the list of available payment methods', async () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
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,
|
|
|
|
getAvailableExpressPaymentMethods: () => {
|
|
|
|
return {
|
|
|
|
express_payment_1: {
|
|
|
|
name: 'express_payment_1',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getSavedPaymentMethods: () => {
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const originalDispatch =
|
|
|
|
jest.requireActual( '@wordpress/data' ).dispatch;
|
|
|
|
const setActivePaymentMethodMock = jest.fn();
|
|
|
|
jest.spyOn( wpDataFunctions, 'dispatch' ).mockImplementation(
|
|
|
|
( storeName ) => {
|
|
|
|
const originalStore = originalDispatch( storeName );
|
2022-10-06 12:46:46 +00:00
|
|
|
if ( storeName === PAYMENT_STORE_KEY ) {
|
2022-08-22 13:56:44 +00:00
|
|
|
return {
|
|
|
|
...originalStore,
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetActivePaymentMethod:
|
|
|
|
setActivePaymentMethodMock,
|
2022-08-22 13:56:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await setDefaultPaymentMethod( paymentMethods );
|
|
|
|
expect( setActivePaymentMethodMock ).toHaveBeenCalledWith(
|
|
|
|
'wc-payment-gateway-1'
|
|
|
|
);
|
|
|
|
} );
|
2022-11-11 19:14:18 +00:00
|
|
|
it( 'correctly sets the saved payment method if one is available', async () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
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,
|
|
|
|
getAvailableExpressPaymentMethods: () => {
|
|
|
|
return {
|
|
|
|
express_payment_1: {
|
|
|
|
name: 'express_payment_1',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
getSavedPaymentMethods: () => {
|
|
|
|
return {
|
|
|
|
cc: [
|
|
|
|
{
|
|
|
|
method: {
|
|
|
|
gateway: 'saved-method',
|
|
|
|
last4: '4242',
|
|
|
|
brand: 'Visa',
|
|
|
|
},
|
|
|
|
expires: '04/44',
|
|
|
|
is_default: true,
|
|
|
|
actions: {
|
|
|
|
delete: {
|
|
|
|
url: 'https://example.com/delete',
|
|
|
|
name: 'Delete',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tokenId: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const originalDispatch =
|
|
|
|
jest.requireActual( '@wordpress/data' ).dispatch;
|
|
|
|
const setActivePaymentMethodMock = jest.fn();
|
|
|
|
jest.spyOn( wpDataFunctions, 'dispatch' ).mockImplementation(
|
|
|
|
( storeName ) => {
|
|
|
|
const originalStore = originalDispatch( storeName );
|
2022-10-06 12:46:46 +00:00
|
|
|
if ( storeName === PAYMENT_STORE_KEY ) {
|
2022-08-22 13:56:44 +00:00
|
|
|
return {
|
|
|
|
...originalStore,
|
2022-10-05 10:04:16 +00:00
|
|
|
__internalSetActivePaymentMethod:
|
|
|
|
setActivePaymentMethodMock,
|
2022-11-15 12:27:39 +00:00
|
|
|
__internalSetPaymentError: () => void 0,
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetPaymentIdle: () => void 0,
|
|
|
|
__internalSetExpressPaymentStarted: () => void 0,
|
2022-11-15 12:27:39 +00:00
|
|
|
__internalSetPaymentProcessing: () => void 0,
|
2023-02-14 12:08:19 +00:00
|
|
|
__internalSetPaymentReady: () => void 0,
|
2022-08-22 13:56:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return originalStore;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await setDefaultPaymentMethod( paymentMethods );
|
|
|
|
expect( setActivePaymentMethodMock ).toHaveBeenCalledWith(
|
|
|
|
'saved-method',
|
|
|
|
{
|
|
|
|
isSavedToken: true,
|
|
|
|
payment_method: 'saved-method',
|
|
|
|
token: '2',
|
|
|
|
'wc-saved-method-payment-token': '2',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|