initial commit
This commit is contained in:
@ -0,0 +1,205 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerPaymentMethodExtensionCallbacks } from '@woocommerce/blocks-registry';
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import * as helpers from '../payment-method-config-helper';
|
||||
import { canMakePaymentExtensionsCallbacks } from '../extensions-config';
|
||||
|
||||
const canMakePaymentArgument = {
|
||||
cartTotals: {
|
||||
total_items: '1488',
|
||||
total_items_tax: '312',
|
||||
total_fees: '0',
|
||||
total_fees_tax: '0',
|
||||
total_discount: '0',
|
||||
total_discount_tax: '0',
|
||||
total_shipping: '0',
|
||||
total_shipping_tax: '0',
|
||||
total_price: '1800',
|
||||
total_tax: '312',
|
||||
tax_lines: [
|
||||
{
|
||||
name: 'BTW',
|
||||
price: '312',
|
||||
rate: '21%',
|
||||
},
|
||||
],
|
||||
currency_code: 'EUR',
|
||||
currency_symbol: '€',
|
||||
currency_minor_unit: 2,
|
||||
currency_decimal_separator: ',',
|
||||
currency_thousand_separator: '.',
|
||||
currency_prefix: '€',
|
||||
currency_suffix: '',
|
||||
},
|
||||
cartNeedsShipping: true,
|
||||
billingData: {
|
||||
first_name: 'name',
|
||||
last_name: 'Name',
|
||||
company: '',
|
||||
address_1: 'fdsfdsfdsf',
|
||||
address_2: '',
|
||||
city: 'Berlin',
|
||||
state: '',
|
||||
postcode: 'xxxxx',
|
||||
country: 'DE',
|
||||
email: 'name.Name@test.com',
|
||||
phone: '1234',
|
||||
},
|
||||
shippingAddress: {
|
||||
first_name: 'name',
|
||||
last_name: 'Name',
|
||||
company: '',
|
||||
address_1: 'fdsfdsfdsf',
|
||||
address_2: '',
|
||||
city: 'Berlin',
|
||||
state: '',
|
||||
postcode: 'xxxxx',
|
||||
country: 'DE',
|
||||
phone: '1234',
|
||||
},
|
||||
selectedShippingMethods: {
|
||||
'0': 'free_shipping:1',
|
||||
},
|
||||
paymentRequirements: [ 'products' ],
|
||||
};
|
||||
describe( 'payment-method-config-helper', () => {
|
||||
const trueCallback = jest.fn().mockReturnValue( true );
|
||||
const falseCallback = jest.fn().mockReturnValue( false );
|
||||
const bacsCallback = jest.fn().mockReturnValue( false );
|
||||
const throwsCallback = jest.fn().mockImplementation( () => {
|
||||
throw new Error();
|
||||
} );
|
||||
beforeAll( () => {
|
||||
// Register extension callbacks for two payment methods.
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'woocommerce-marketplace-extension',
|
||||
{
|
||||
// cod: one extension returns true, the other returns false.
|
||||
cod: trueCallback,
|
||||
// cheque: returns true only if arg.billingData.postcode is 12345.
|
||||
cheque: ( arg ) => arg.billingData.postcode === '12345',
|
||||
// bacs: both extensions return false.
|
||||
bacs: bacsCallback,
|
||||
// woopay: both extensions return true.
|
||||
woopay: trueCallback,
|
||||
// testpay: one callback errors, one returns true
|
||||
testpay: throwsCallback,
|
||||
}
|
||||
);
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'other-woocommerce-marketplace-extension',
|
||||
{
|
||||
cod: falseCallback,
|
||||
woopay: trueCallback,
|
||||
testpay: trueCallback,
|
||||
bacs: bacsCallback,
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
||||
beforeEach( () => {
|
||||
trueCallback.mockClear();
|
||||
throwsCallback.mockClear();
|
||||
falseCallback.mockClear();
|
||||
bacsCallback.mockClear();
|
||||
} );
|
||||
describe( 'getCanMakePayment', () => {
|
||||
it( 'returns callback canMakePaymentWithFeaturesCheck if no extension callback is detected', () => {
|
||||
// Define arguments from a payment method ('missing-payment-method') with no registered extension callbacks.
|
||||
const args = {
|
||||
canMakePayment: jest.fn().mockImplementation( () => true ),
|
||||
features: [ 'products' ],
|
||||
paymentMethodName: 'missing-payment-method',
|
||||
};
|
||||
|
||||
const canMakePayment = helpers.getCanMakePayment(
|
||||
args.canMakePayment,
|
||||
args.features,
|
||||
args.paymentMethodName
|
||||
)( canMakePaymentArgument );
|
||||
|
||||
// Expect that the result of getCanMakePayment is the result of
|
||||
// the payment method's own canMakePayment, as no extension callbacks are called.
|
||||
expect( canMakePayment ).toEqual( args.canMakePayment() );
|
||||
} );
|
||||
|
||||
it( 'returns callbacks from the extensions when they are defined', () => {
|
||||
// Define arguments from a payment method (bacs) with registered extension callbacks.
|
||||
const args = {
|
||||
canMakePaymentConfiguration: jest
|
||||
.fn()
|
||||
.mockImplementation( () => true ),
|
||||
features: [ 'products' ],
|
||||
paymentMethodName: 'bacs',
|
||||
};
|
||||
|
||||
const canMakePayment = helpers.getCanMakePayment(
|
||||
args.canMakePaymentConfiguration,
|
||||
args.features,
|
||||
args.paymentMethodName
|
||||
)( canMakePaymentArgument );
|
||||
|
||||
// Expect that the result of getCanMakePayment is not the result of
|
||||
// the payment method's own canMakePayment (args.canMakePaymentConfiguration),
|
||||
// but of the registered bacsCallback.
|
||||
expect( canMakePayment ).toBe( bacsCallback() );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( 'canMakePaymentWithExtensions', () => {
|
||||
it( "Returns false without executing the registered callbacks, if the payment method's canMakePayment callback returns false.", () => {
|
||||
const canMakePayment = () => false;
|
||||
const canMakePaymentWithExtensionsResult = helpers.canMakePaymentWithExtensions(
|
||||
canMakePayment,
|
||||
canMakePaymentExtensionsCallbacks,
|
||||
'cod'
|
||||
)( canMakePaymentArgument );
|
||||
expect( canMakePaymentWithExtensionsResult ).toBe( false );
|
||||
expect( trueCallback ).not.toHaveBeenCalled();
|
||||
} );
|
||||
|
||||
it( 'Returns early when a registered callback returns false, without executing all the registered callbacks', () => {
|
||||
helpers.canMakePaymentWithExtensions(
|
||||
() => true,
|
||||
canMakePaymentExtensionsCallbacks,
|
||||
'bacs'
|
||||
)( canMakePaymentArgument );
|
||||
expect( bacsCallback ).toHaveBeenCalledTimes( 1 );
|
||||
} );
|
||||
|
||||
it( 'Returns true if all extension callbacks return true', () => {
|
||||
const result = helpers.canMakePaymentWithExtensions(
|
||||
() => true,
|
||||
canMakePaymentExtensionsCallbacks,
|
||||
'woopay'
|
||||
)( canMakePaymentArgument );
|
||||
expect( result ).toBe( true );
|
||||
} );
|
||||
|
||||
it( 'Passes canPayArg to the callback', () => {
|
||||
helpers.canMakePaymentWithExtensions(
|
||||
() => true,
|
||||
canMakePaymentExtensionsCallbacks,
|
||||
'woopay'
|
||||
)( canMakePaymentArgument );
|
||||
expect( trueCallback ).toHaveBeenCalledWith(
|
||||
canMakePaymentArgument
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'Allows all valid callbacks to run, even if one causes an error', () => {
|
||||
helpers.canMakePaymentWithExtensions(
|
||||
() => true,
|
||||
canMakePaymentExtensionsCallbacks,
|
||||
'testpay'
|
||||
)( canMakePaymentArgument );
|
||||
expect( console ).toHaveErrored();
|
||||
expect( throwsCallback ).toHaveBeenCalledTimes( 1 );
|
||||
expect( trueCallback ).toHaveBeenCalledTimes( 1 );
|
||||
} );
|
||||
} );
|
||||
} );
|
@ -0,0 +1,61 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerPaymentMethodExtensionCallbacks } from '@woocommerce/blocks-registry';
|
||||
import type { PaymentMethodConfigInstance } from '@woocommerce/type-defs/payments';
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import PaymentMethodConfig from '../payment-method-config';
|
||||
import * as paymentMethodConfigHelpers from '../payment-method-config-helper';
|
||||
|
||||
describe( 'PaymentMethodConfig', () => {
|
||||
let paymentMethod: PaymentMethodConfigInstance;
|
||||
const extensionsCallbackSpy = jest.spyOn(
|
||||
paymentMethodConfigHelpers,
|
||||
'canMakePaymentWithExtensions'
|
||||
);
|
||||
beforeEach( () => {
|
||||
paymentMethod = new PaymentMethodConfig( {
|
||||
name: 'test-payment-method',
|
||||
label: 'Test payment method',
|
||||
ariaLabel: 'Test payment method',
|
||||
content: <div>Test payment content</div>,
|
||||
edit: <div>Test payment edit</div>,
|
||||
canMakePayment: () => true,
|
||||
supports: { features: [ 'products' ] },
|
||||
} );
|
||||
} );
|
||||
|
||||
it( 'Uses canMakePaymentWithExtensions as the canMakePayment function if an extension registers a callback', () => {
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'woocommerce-marketplace-extension',
|
||||
{
|
||||
'unrelated-payment-method': () => true,
|
||||
}
|
||||
);
|
||||
|
||||
// At this point, since no extensions have registered a callback for
|
||||
// test-payment-method we can expect the canMakePayment getter NOT
|
||||
// to execute canMakePaymentWithExtensions.
|
||||
// Disable no-unused-expressions because we just want to test the getter
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
paymentMethod.canMakePayment;
|
||||
expect( extensionsCallbackSpy ).toHaveBeenCalledTimes( 0 );
|
||||
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'other-woocommerce-marketplace-extension',
|
||||
{
|
||||
'test-payment-method': () => true,
|
||||
}
|
||||
);
|
||||
|
||||
// Now, because an extension _has_ registered a callback for test-payment-method
|
||||
// The getter will use canMakePaymentWithExtensions to create the
|
||||
// canMakePayment function.
|
||||
// Disable no-unused-expressions because we just want to test the getter
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
paymentMethod.canMakePayment;
|
||||
expect( extensionsCallbackSpy ).toHaveBeenCalledTimes( 1 );
|
||||
} );
|
||||
} );
|
@ -0,0 +1,97 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerPaymentMethodExtensionCallbacks } from '@woocommerce/blocks-registry';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { canMakePaymentExtensionsCallbacks } from '../extensions-config';
|
||||
|
||||
describe( 'registerPaymentMethodExtensionCallbacks', () => {
|
||||
it( 'Logs an error to console if namespace is already registered', () => {
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'woocommerce-marketplace-extension',
|
||||
{
|
||||
cod: () => false,
|
||||
}
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
expect( console ).not.toHaveErrored();
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'woocommerce-marketplace-extension',
|
||||
{
|
||||
cod: () => false,
|
||||
}
|
||||
);
|
||||
expect( console ).toHaveErrored();
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
expect( console.error ).toHaveBeenCalledTimes( 1 );
|
||||
} );
|
||||
|
||||
it( 'Does not overwrite a namespace if a second extensions tries to register with the same name', () => {
|
||||
const firstCodCallback = jest.fn().mockReturnValue( false );
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'overwrite-marketplace-extension',
|
||||
{
|
||||
cod: firstCodCallback,
|
||||
}
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
expect( console ).not.toHaveErrored();
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'overwrite-marketplace-extension',
|
||||
{
|
||||
cod: () => false,
|
||||
}
|
||||
);
|
||||
|
||||
expect(
|
||||
canMakePaymentExtensionsCallbacks[
|
||||
'overwrite-marketplace-extension'
|
||||
].cod
|
||||
).toEqual( firstCodCallback );
|
||||
} );
|
||||
|
||||
it( 'Logs an error if a supplied callback is not a function and does not register the callback for that method', () => {
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'other-woocommerce-marketplace-extension',
|
||||
{
|
||||
cod: false,
|
||||
cheque: () => true,
|
||||
}
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
expect( console ).toHaveErrored();
|
||||
expect( canMakePaymentExtensionsCallbacks ).toHaveProperty(
|
||||
'other-woocommerce-marketplace-extension'
|
||||
);
|
||||
expect(
|
||||
canMakePaymentExtensionsCallbacks[
|
||||
'other-woocommerce-marketplace-extension'
|
||||
]
|
||||
).not.toHaveProperty( 'cod' );
|
||||
expect(
|
||||
canMakePaymentExtensionsCallbacks[
|
||||
'other-woocommerce-marketplace-extension'
|
||||
]
|
||||
).toHaveProperty( 'cheque' );
|
||||
} );
|
||||
|
||||
it( 'Adds the namespace and callbacks to the canMakePaymentExtensionCallbacks object', () => {
|
||||
// We are using a new namespace here because canMakePaymentExtensionsCallbacks cannot be reset between tests.
|
||||
registerPaymentMethodExtensionCallbacks(
|
||||
'third-woocommerce-marketplace-extension',
|
||||
{
|
||||
cod: () => false,
|
||||
}
|
||||
);
|
||||
expect( canMakePaymentExtensionsCallbacks ).toHaveProperty(
|
||||
'third-woocommerce-marketplace-extension'
|
||||
);
|
||||
} );
|
||||
} );
|
Reference in New Issue
Block a user