2021-09-15 12:40:40 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerPaymentMethodExtensionCallbacks } from '@woocommerce/blocks-registry';
|
2021-09-23 09:09:55 +00:00
|
|
|
import type { PaymentMethodConfigInstance } from '@woocommerce/type-defs/payments';
|
2021-09-15 12:40:40 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import PaymentMethodConfig from '../payment-method-config';
|
|
|
|
import * as paymentMethodConfigHelpers from '../payment-method-config-helper';
|
|
|
|
|
|
|
|
describe( 'PaymentMethodConfig', () => {
|
2021-09-23 09:09:55 +00:00
|
|
|
let paymentMethod: PaymentMethodConfigInstance;
|
|
|
|
const extensionsCallbackSpy = jest.spyOn(
|
|
|
|
paymentMethodConfigHelpers,
|
|
|
|
'canMakePaymentWithExtensions'
|
|
|
|
);
|
2021-09-15 12:40:40 +00:00
|
|
|
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,
|
2021-09-23 09:09:55 +00:00
|
|
|
supports: { features: [ 'products' ] },
|
2021-09-15 12:40:40 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'Uses canMakePaymentWithExtensions as the canMakePayment function if an extension registers a callback', () => {
|
|
|
|
registerPaymentMethodExtensionCallbacks(
|
|
|
|
'woocommerce-marketplace-extension',
|
|
|
|
{
|
2022-03-28 13:00:20 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2021-09-15 12:40:40 +00:00
|
|
|
'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;
|
2021-09-23 09:09:55 +00:00
|
|
|
expect( extensionsCallbackSpy ).toHaveBeenCalledTimes( 0 );
|
2021-09-15 12:40:40 +00:00
|
|
|
|
|
|
|
registerPaymentMethodExtensionCallbacks(
|
|
|
|
'other-woocommerce-marketplace-extension',
|
|
|
|
{
|
2022-03-28 13:00:20 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2021-09-15 12:40:40 +00:00
|
|
|
'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;
|
2021-09-23 09:09:55 +00:00
|
|
|
expect( extensionsCallbackSpy ).toHaveBeenCalledTimes( 1 );
|
2021-09-15 12:40:40 +00:00
|
|
|
} );
|
|
|
|
} );
|