woocommerce/plugins/woocommerce-admin/client/task-list/test/payments.js

226 lines
5.5 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { PayPal, PAYPAL_PLUGIN } from '../tasks/payments/paypal';
import { getPaymentMethods } from '../tasks/payments/methods';
import { setMethodEnabledOption } from '../../task-list/tasks/payments';
jest.mock( '@wordpress/api-fetch' );
describe( 'TaskList > Payments', () => {
describe( 'Payments', () => {
const optionName = 'woocommerce_mollie_payments_settings';
it( 'does not update an option if the value is the same as the current one', async () => {
const mockProps = {
clearTaskStatusCache: jest.fn(),
updateOptions: jest.fn(),
options: {
woocommerce_klarna_payments_settings: false,
woocommerce_mollie_payments_settings: { enabled: 'yes' },
},
};
await setMethodEnabledOption( optionName, 'yes', mockProps );
expect( mockProps.updateOptions ).not.toHaveBeenCalled();
} );
it( 'does update an option if the value is different to the current one', async () => {
const mockProps = {
clearTaskStatusCache: jest.fn(),
updateOptions: jest.fn(),
options: {
woocommerce_klarna_payments_settings: false,
woocommerce_mollie_payments_settings: { enabled: 'no' },
},
};
await setMethodEnabledOption( optionName, 'yes', mockProps );
expect( mockProps.updateOptions ).toHaveBeenCalledWith( {
woocommerce_mollie_payments_settings: { enabled: 'yes' },
} );
} );
} );
describe( 'Methods', () => {
const params = {
activePlugins: [],
countryCode: 'SE',
onboardingStatus: {},
options: [],
profileItems: { industry: [] },
};
it( 'includes Klarna Checkout for SE, NO, and FI', () => {
[ 'SE', 'NO', 'FI' ].forEach( ( countryCode ) => {
params.countryCode = countryCode;
const methods = getPaymentMethods( params );
expect(
methods.some(
( method ) => method.key === 'klarna_checkout'
)
).toBe( true );
} );
} );
it( 'includes Klarna Payment for EU countries', () => {
const supportedCountryCodes = [
'DK',
'DE',
'AT',
'NL',
'CH',
'BE',
'SP',
'PL',
'FR',
'IT',
'GB',
];
supportedCountryCodes.forEach( ( countryCode ) => {
params.countryCode = countryCode;
const methods = getPaymentMethods( params );
expect(
methods.some( ( e ) => e.key === 'klarna_payments' )
).toBe( true );
} );
} );
describe( 'Mollie', () => {
it( 'Detects the plugin is enabled based on the options passed', () => {
const mollieParams = {
...params,
options: {
woocommerce_mollie_payments_settings: {
enabled: 'yes',
},
},
};
const mollieMethod = getPaymentMethods( mollieParams ).find(
( method ) => method.key === 'mollie'
);
expect( mollieMethod.isEnabled ).toBe( true );
} );
it( 'is enabled for supported countries', () => {
[
'FR',
'DE',
'GB',
'AT',
'CH',
'ES',
'IT',
'PL',
'FI',
'NL',
'BE',
].forEach( ( countryCode ) => {
const methods = getPaymentMethods( {
...params,
countryCode,
} );
expect(
methods.filter( ( method ) => method.key === 'mollie' )
.length
).toBe( 1 );
} );
} );
} );
it( 'is marked as `isConfigured` if the plugin is active', () => {
expect(
getPaymentMethods( {
...params,
activePlugins: [ 'mollie-payments-for-woocommerce' ],
} ).find( ( method ) => method.key === 'mollie' ).isConfigured
).toBe( true );
} );
} );
describe( 'PayPal', () => {
afterEach( () => jest.clearAllMocks() );
const mockInstallStep = {
isComplete: true,
key: 'install',
label: 'Install',
};
it( 'shows API credential inputs when "create account" opted out and OAuth fetch fails', async () => {
apiFetch.mockResolvedValue( false );
render(
<PayPal
activePlugins={ [
'jetpack',
PAYPAL_PLUGIN,
'woocommerce-services',
] }
isRequestingOptions={ false }
options={ {} }
installStep={ mockInstallStep }
/>
);
// Since the oauth response failed, we should have the API credentials form.
expect(
await screen.findByText( 'Proceed', { selector: 'button' } )
).toBeDefined();
expect(
screen.queryByLabelText( 'Email address', {
selector: 'input',
} )
).toBeDefined();
expect(
screen.getByLabelText( 'Merchant Id', { selector: 'input' } )
).toBeDefined();
expect(
screen.getByLabelText( 'Client Id', { selector: 'input' } )
).toBeDefined();
expect(
screen.getByLabelText( 'Secret Key', { selector: 'input' } )
).toBeDefined();
} );
it( 'shows OAuth connect button', async () => {
global.ppcp_onboarding = {
reload: () => {},
};
const mockConnectUrl = 'https://connect.woocommerce.test/paypal';
apiFetch.mockResolvedValue( {
signupLink: mockConnectUrl,
} );
render(
<PayPal
activePlugins={ [ PAYPAL_PLUGIN ] }
installStep={ mockInstallStep }
isRequestingOptions={ false }
options={ {} }
/>
);
// Since the oauth response was mocked, we should have a "connect" button.
const oauthButton = await screen.findByText( 'Connect', {
selector: 'a',
} );
expect( oauthButton ).toBeDefined();
expect( oauthButton.href ).toEqual( mockConnectUrl );
expect( oauthButton.dataset.paypalButton ).toEqual( 'true' );
expect( oauthButton.dataset.paypalOnboardButton ).toEqual( 'true' );
} );
} );
} );