Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Move payment utils and delete orderPaymentMethods #7679

Merged
merged 8 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions assets/js/data/payment/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
import { PaymentStatus } from './types';

// `Thunks are functions that can be dispatched, similar to actions creators
Expand Down
2 changes: 1 addition & 1 deletion assets/js/data/payment/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { objectHasProp } from '@woocommerce/types';
* Internal dependencies
*/
import { PaymentMethodDataState } from './default-state';
import { filterActiveSavedPaymentMethods } from './utils';
import { filterActiveSavedPaymentMethods } from './utils/filter-active-saved-payment-methods';

export const isExpressPaymentMethodActive = (
state: PaymentMethodDataState
Expand Down
12 changes: 4 additions & 8 deletions assets/js/data/payment/test/actions.ts
Original file line number Diff line number Diff line change
@@ -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(),
} ) );

Expand All @@ -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', () => {
Expand All @@ -41,9 +39,7 @@ describe( 'payment data store actions', () => {
actions.__internalSetAvailablePaymentMethods( [
paymentMethods[ Object.keys( paymentMethods )[ 0 ] ],
] );
expect(
setDefaultPaymentMethodFunctions.setDefaultPaymentMethod
).toBeCalled();
expect( setDefaultPaymentMethodOriginal ).toBeCalled();
} );
} );
} );
2 changes: 1 addition & 1 deletion assets/js/data/payment/test/set-default-payment-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '..';

Expand Down
25 changes: 0 additions & 25 deletions assets/js/data/payment/test/utils.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -149,6 +149,7 @@ export const checkPaymentMethodsCanPay = async ( express = false ) => {
};
}

// Order payment methods
let paymentMethodsOrder;
if ( express ) {
paymentMethodsOrder = Object.keys( paymentMethods );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,15 @@
<error line="411" column="5" severity="error" message="Type &apos;{ currency_code: &quot;USD&quot;; 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: { ...; }[]; }&apos; is not assignable to type &apos;CartResponseFeeItemTotals&apos;.
Object literal may only specify known properties, and &apos;tax_lines&apos; does not exist in type &apos;CartResponseFeeItemTotals&apos;." 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 &apos;&quot;@wordpress/notices&quot;&apos; has no exported member &apos;store&apos;." source="TS2305" />
<error line="132" column="37" severity="error" message="Property &apos;paymentRequirements&apos; does not exist on type &apos;CartResponse&apos;. Did you mean &apos;payment_requirements&apos;?" source="TS2551" />
<error line="134" column="25" severity="error" message="Property &apos;receiveCart&apos; does not exist on type &apos;CartResponse&apos;." source="TS2339" />
<error line="135" column="20" severity="error" message="Property &apos;receiveCart&apos; does not exist on type &apos;CartResponse&apos;." source="TS2339" />
<error line="146" column="5" severity="error" message="Argument of type &apos;unknown&apos; is not assignable to parameter of type &apos;CartShippingRate[]&apos;." source="TS2345" />
<error line="176" column="37" severity="error" message="Argument of type &apos;Record&lt;string, unknown&gt;&apos; is not assignable to parameter of type &apos;CanMakePaymentArgument&apos;.
<error line="177" column="37" severity="error" message="Argument of type &apos;Record&lt;string, unknown&gt;&apos; is not assignable to parameter of type &apos;CanMakePaymentArgument&apos;.
Type &apos;Record&lt;string, unknown&gt;&apos; is missing the following properties from type &apos;CanMakePaymentArgument&apos;: cart, cartTotals, cartNeedsShipping, billingData, and 3 more." source="TS2345" />
<error line="188" column="13" severity="error" message="Property &apos;createErrorNotice&apos; does not exist on type &apos;typeof import(&quot;/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions&quot;)&apos;." source="TS2339" />
<error line="189" column="13" severity="error" message="Property &apos;createErrorNotice&apos; does not exist on type &apos;typeof import(&quot;/home/runner/work/woocommerce-blocks/woocommerce-blocks/node_modules/@types/wordpress__rich-text/store/actions&quot;)&apos;." source="TS2339" />
</file>
<file name="assets/js/data/payment/thunks.ts">
<error line="4" column="10" severity="error" message="Module &apos;&quot;@wordpress/notices&quot;&apos; has no exported member &apos;store&apos;." source="TS2305" />
Expand Down