woocommerce/plugins/woocommerce-blocks/assets/js/data/payment-methods/test/set-default-payment-method.ts

144 lines
3.6 KiB
TypeScript
Raw Normal View History

Refactor registration of payment methods and update unit tests for payment data store (https://github.com/woocommerce/woocommerce-blocks/pull/6669) * Mock getCartTotals * Change test to use data store instead of context * Move payment method context test to data store selectors * Change description of test suite * Bump commit to trigger tests * Fix path in test * update package lock * Set correct state payment method reducer tests/use correct actions * Get saved payment methods from store not context * Mock stores and update tests to allow switching payment methods * Update tests to get onSubmit from checkoutEventsContext * Remove cartTotalsLoaded check from payment method initialize check * Make PaymentMethods test wait until payments initialized * initialize payment method data store when cart is loaded * Remove unneeded actions and add initializePaymentMethodDataStore * Remove check for cart totals loaded in checkPaymentMethods * Remove updateAvilablePaymentMethods from registry * Remove unneeded mock * Remove unused import * Rename imports to fix eslint errors * Remove unused imports * Remove return false from checkPaymentMethods * Remove unnecessary setPaymentMethodsInitialized call * Add todo comment to track refactoring opportunity * Remove savedpayment methods from payment method context and rename it * Rename payment method data context to payment method events context * Add tests for setDefaultPaymentMethods * Optimize the availablePaymentMethods state data Store only the "name" attribute for now. * Get list of payment methods from the registry instead of the store We are using this hook to get some React elements in the payment method object. So, we are getting the raw data directly from the registry instead of the store. * Fix payment state not loading on the Checkout edit page * Handle checkout edit page case * Fix infinite loop error on C&C Blocks * Include @wordpress/redux-routine in transformIgnorePatterns jest config Co-authored-by: Saad Tarhi <saad.trh@gmail.com>
2022-08-22 13:56:44 +00:00
/* eslint-disable no-unused-expressions */
/**
* External dependencies
*/
import * as wpDataFunctions from '@wordpress/data';
/**
* Internal dependencies
*/
import { setDefaultPaymentMethod } from '../set-default-payment-method';
import { PaymentMethods } from '../../../types';
import { PAYMENT_METHOD_DATA_STORE_KEY } from '..';
const originalSelect = jest.requireActual( '@wordpress/data' ).select;
describe( 'setDefaultPaymentMethod', () => {
afterEach( () => {
jest.resetAllMocks();
jest.resetModules();
} );
const paymentMethods: PaymentMethods = {
'wc-payment-gateway-1': {
name: 'wc-payment-gateway-1',
},
'wc-payment-gateway-2': {
name: 'wc-payment-gateway-2',
},
};
it( ' correctly sets the first payment method in the list of available payment methods', async () => {
jest.spyOn( wpDataFunctions, 'select' ).mockImplementation(
( storeName ) => {
const originalStore = originalSelect( storeName );
if ( storeName === PAYMENT_METHOD_DATA_STORE_KEY ) {
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 );
if ( storeName === PAYMENT_METHOD_DATA_STORE_KEY ) {
return {
...originalStore,
setActivePaymentMethod: setActivePaymentMethodMock,
};
}
return originalStore;
}
);
await setDefaultPaymentMethod( paymentMethods );
expect( setActivePaymentMethodMock ).toHaveBeenCalledWith(
'wc-payment-gateway-1'
);
} );
it( ' correctly sets the saved payment method if one is available', async () => {
jest.spyOn( wpDataFunctions, 'select' ).mockImplementation(
( storeName ) => {
const originalStore = originalSelect( storeName );
if ( storeName === PAYMENT_METHOD_DATA_STORE_KEY ) {
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 );
if ( storeName === PAYMENT_METHOD_DATA_STORE_KEY ) {
return {
...originalStore,
setActivePaymentMethod: setActivePaymentMethodMock,
setPaymentStatus: () => void 0,
};
}
return originalStore;
}
);
await setDefaultPaymentMethod( paymentMethods );
expect( setActivePaymentMethodMock ).toHaveBeenCalledWith(
'saved-method',
{
isSavedToken: true,
payment_method: 'saved-method',
token: '2',
'wc-saved-method-payment-token': '2',
}
);
} );
} );