Move payment utils and delete orderPaymentMethods (https://github.com/woocommerce/woocommerce-blocks/pull/7679)
* Moved all payment utils functions in a utils folder * Delete as it's not being used * bot: update checkstyle.xml * bot: update checkstyle.xml * Fix TS error * bot: update checkstyle.xml Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
5e1896d98a
commit
ddd0cc2300
|
@ -10,8 +10,8 @@ import {
|
|||
* Internal dependencies
|
||||
*/
|
||||
import { ACTION_TYPES } from './action-types';
|
||||
import { checkPaymentMethodsCanPay } from './check-payment-methods';
|
||||
import { setDefaultPaymentMethod } from './set-default-payment-method';
|
||||
import { checkPaymentMethodsCanPay } from './utils/check-payment-methods';
|
||||
import { setDefaultPaymentMethod } from './utils/set-default-payment-method';
|
||||
|
||||
// `Thunks are functions that can be dispatched, similar to actions creators
|
||||
export * from './thunks';
|
||||
|
|
|
@ -8,7 +8,7 @@ import deprecated from '@wordpress/deprecated';
|
|||
* Internal dependencies
|
||||
*/
|
||||
import { PaymentMethodDataState } from './default-state';
|
||||
import { filterActiveSavedPaymentMethods } from './utils';
|
||||
import { filterActiveSavedPaymentMethods } from './utils/filter-active-saved-payment-methods';
|
||||
import { STATUS as PAYMENT_STATUS } from './constants';
|
||||
|
||||
export const isPaymentPristine = ( state: PaymentMethodDataState ) =>
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import * as setDefaultPaymentMethodFunctions from '../set-default-payment-method';
|
||||
import { setDefaultPaymentMethod as setDefaultPaymentMethodOriginal } from '../utils/set-default-payment-method';
|
||||
import { PAYMENT_STORE_KEY } from '..';
|
||||
import { PlainPaymentMethods } from '../../../types';
|
||||
|
||||
const originalDispatch = jest.requireActual( '@wordpress/data' ).dispatch;
|
||||
|
||||
jest.mock( '../set-default-payment-method', () => ( {
|
||||
jest.mock( '../utils/set-default-payment-method', () => ( {
|
||||
setDefaultPaymentMethod: jest.fn(),
|
||||
} ) );
|
||||
|
||||
|
@ -28,9 +28,7 @@ describe( 'payment data store actions', () => {
|
|||
Object.keys( paymentMethods )[ 0 ]
|
||||
);
|
||||
actions.__internalSetAvailablePaymentMethods( paymentMethods );
|
||||
expect(
|
||||
setDefaultPaymentMethodFunctions.setDefaultPaymentMethod
|
||||
).not.toBeCalled();
|
||||
expect( setDefaultPaymentMethodOriginal ).not.toBeCalled();
|
||||
} );
|
||||
|
||||
it( 'Resets the default gateway if the current method is no longer available', () => {
|
||||
|
@ -41,9 +39,7 @@ describe( 'payment data store actions', () => {
|
|||
actions.__internalSetAvailablePaymentMethods( [
|
||||
paymentMethods[ Object.keys( paymentMethods )[ 0 ] ],
|
||||
] );
|
||||
expect(
|
||||
setDefaultPaymentMethodFunctions.setDefaultPaymentMethod
|
||||
).toBeCalled();
|
||||
expect( setDefaultPaymentMethodOriginal ).toBeCalled();
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
|
@ -7,7 +7,7 @@ import * as wpDataFunctions from '@wordpress/data';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { setDefaultPaymentMethod } from '../set-default-payment-method';
|
||||
import { setDefaultPaymentMethod } from '../utils/set-default-payment-method';
|
||||
import { PlainPaymentMethods } from '../../../types';
|
||||
import { PAYMENT_STORE_KEY } from '..';
|
||||
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { orderPaymentMethods } from '../utils';
|
||||
|
||||
describe( 'orderPaymentMethods', () => {
|
||||
it( 'orders methods correctly', () => {
|
||||
const order = [ 'cheque', 'cod', 'bacs', 'stripe' ];
|
||||
const methods = [ 'cod', 'bacs', 'stripe', 'cheque' ];
|
||||
const orderedMethods = orderPaymentMethods( order, methods );
|
||||
expect( orderedMethods ).toStrictEqual( order );
|
||||
} );
|
||||
it( 'orders methods correctly and appends missing ones', () => {
|
||||
const order = [ 'cheque', 'cod', 'bacs', 'stripe' ];
|
||||
const methods = [ 'cod', 'paypal', 'bacs', 'stripe', 'cheque' ];
|
||||
const orderedMethods = orderPaymentMethods( order, methods );
|
||||
expect( orderedMethods ).toStrictEqual( [
|
||||
'cheque',
|
||||
'cod',
|
||||
'bacs',
|
||||
'stripe',
|
||||
'paypal',
|
||||
] );
|
||||
} );
|
||||
} );
|
|
@ -23,18 +23,18 @@ import { previewCart } from '@woocommerce/resource-previews';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { STORE_KEY as CART_STORE_KEY } from '../cart/constants';
|
||||
import { STORE_KEY as PAYMENT_STORE_KEY } from './constants';
|
||||
import { noticeContexts } from '../../base/context/event-emit';
|
||||
import { STORE_KEY as CART_STORE_KEY } from '../../cart/constants';
|
||||
import { STORE_KEY as PAYMENT_STORE_KEY } from '../constants';
|
||||
import { noticeContexts } from '../../../base/context/event-emit';
|
||||
import {
|
||||
EMPTY_CART_ERRORS,
|
||||
EMPTY_CART_ITEM_ERRORS,
|
||||
EMPTY_EXTENSIONS,
|
||||
} from '../../data/constants';
|
||||
} from '../../constants';
|
||||
import {
|
||||
defaultBillingAddress,
|
||||
defaultShippingAddress,
|
||||
} from '../../base/context/providers/cart-checkout/customer/constants';
|
||||
} from '../../../base/context/providers/cart-checkout/customer/constants';
|
||||
|
||||
export const checkPaymentMethodsCanPay = async ( express = false ) => {
|
||||
const isEditor = !! select( 'core/editor' );
|
||||
|
@ -129,11 +129,8 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => {
|
|||
shippingRates: previewCart.shipping_rates,
|
||||
isLoadingRates: false,
|
||||
cartHasCalculatedShipping: previewCart.has_calculated_shipping,
|
||||
paymentRequirements: previewCart.paymentRequirements,
|
||||
receiveCart:
|
||||
typeof previewCart?.receiveCart === 'function'
|
||||
? previewCart.receiveCart
|
||||
: () => undefined,
|
||||
paymentRequirements: previewCart.payment_requirements,
|
||||
receiveCart: () => undefined,
|
||||
};
|
||||
canPayArgument = {
|
||||
cart: cartForCanPayArgument,
|
||||
|
@ -149,6 +146,7 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => {
|
|||
};
|
||||
}
|
||||
|
||||
// Order payment methods
|
||||
let paymentMethodsOrder;
|
||||
if ( express ) {
|
||||
paymentMethodsOrder = Object.keys( paymentMethods );
|
|
@ -6,7 +6,7 @@ import { getPaymentMethods } from '@woocommerce/blocks-registry';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import type { SavedPaymentMethods } from './types';
|
||||
import type { SavedPaymentMethods } from '../types';
|
||||
|
||||
/**
|
||||
* Gets the payment methods saved for the current user after filtering out disabled ones.
|
||||
|
@ -47,28 +47,3 @@ export const filterActiveSavedPaymentMethods = (
|
|||
} );
|
||||
return activeSavedPaymentMethods;
|
||||
};
|
||||
|
||||
/**
|
||||
* Given the order of methods from WooCommerce -> Payments, this method takes that order and sorts the list of available
|
||||
* payment methods to match it. This is required to ensure the payment methods show up in the correct order in the
|
||||
* Checkout
|
||||
*
|
||||
* @param order The order of payment methods from WooCommerce -> Settings -> Payments.
|
||||
* @param methods The list of payment method names to add to the state as available.
|
||||
*
|
||||
* @return string[] The list of available methods in their correct order.
|
||||
*/
|
||||
export const orderPaymentMethods = ( order: string[], methods: string[] ) => {
|
||||
const orderedMethods: string[] = [];
|
||||
order.forEach( ( paymentMethodName ) => {
|
||||
if ( methods.includes( paymentMethodName ) ) {
|
||||
orderedMethods.push( paymentMethodName );
|
||||
}
|
||||
} );
|
||||
// Now find any methods in `methods` that were not added to `orderedMethods` and append them to `orderedMethods`
|
||||
methods
|
||||
.filter( ( methodName ) => ! orderedMethods.includes( methodName ) )
|
||||
.forEach( ( methodName ) => orderedMethods.push( methodName ) );
|
||||
|
||||
return orderedMethods;
|
||||
};
|
|
@ -7,7 +7,7 @@ import { PlainPaymentMethods } from '@woocommerce/type-defs/payments';
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { STORE_KEY as PAYMENT_STORE_KEY } from './constants';
|
||||
import { STORE_KEY as PAYMENT_STORE_KEY } from '../constants';
|
||||
|
||||
export const setDefaultPaymentMethod = async (
|
||||
paymentMethods: PlainPaymentMethods
|
|
@ -666,15 +666,12 @@
|
|||
<error line="411" column="5" severity="error" message="Type '{ currency_code: "USD"; currency_symbol: string; currency_minor_unit: number; currency_decimal_separator: string; currency_thousand_separator: string; currency_prefix: string; currency_suffix: string; total: string; total_tax: string; tax_lines: { ...; }[]; }' is not assignable to type 'CartResponseFeeItemTotals'.
|
||||
Object literal may only specify known properties, and 'tax_lines' does not exist in type 'CartResponseFeeItemTotals'." source="TS2322" />
|
||||
</file>
|
||||
<file name="assets/js/data/payment/check-payment-methods.ts">
|
||||
<file name="assets/js/data/payment/utils/check-payment-methods.ts">
|
||||
<error line="15" column="10" severity="error" message="Module '"@wordpress/notices"' has no exported member 'store'." source="TS2305" />
|
||||
<error line="132" column="37" severity="error" message="Property 'paymentRequirements' does not exist on type 'CartResponse'. Did you mean 'payment_requirements'?" source="TS2551" />
|
||||
<error line="134" column="25" severity="error" message="Property 'receiveCart' does not exist on type 'CartResponse'." source="TS2339" />
|
||||
<error line="135" column="20" severity="error" message="Property 'receiveCart' does not exist on type 'CartResponse'." source="TS2339" />
|
||||
<error line="146" column="5" severity="error" message="Argument of type 'unknown' is not assignable to parameter of type 'CartShippingRate[]'." source="TS2345" />
|
||||
<error line="176" column="37" severity="error" message="Argument of type 'Record<string, unknown>' is not assignable to parameter of type 'CanMakePaymentArgument'.
|
||||
<error line="143" column="5" severity="error" message="Argument of type 'unknown' is not assignable to parameter of type 'CartShippingRate[]'." source="TS2345" />
|
||||
<error line="174" column="37" severity="error" message="Argument of type 'Record<string, unknown>' is not assignable to parameter of type 'CanMakePaymentArgument'.
|
||||
Type 'Record<string, unknown>' is missing the following properties from type 'CanMakePaymentArgument': cart, cartTotals, cartNeedsShipping, billingData, and 3 more." source="TS2345" />
|
||||
<error line="188" column="13" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
||||
<error line="186" column="13" severity="error" message="Property 'createErrorNotice' does not exist on type 'typeof import("/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions")'." source="TS2339" />
|
||||
</file>
|
||||
<file name="assets/js/data/payment/thunks.ts">
|
||||
<error line="4" column="10" severity="error" message="Module '"@wordpress/notices"' has no exported member 'store'." source="TS2305" />
|
||||
|
|
Loading…
Reference in New Issue