2020-07-15 12:10:21 +00:00
|
|
|
/**
|
2020-08-13 02:05:22 +00:00
|
|
|
* External dependencies
|
2020-07-15 12:10:21 +00:00
|
|
|
*/
|
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import user from '@testing-library/user-event';
|
|
|
|
import '@testing-library/jest-dom/extend-expect';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
2020-08-13 02:05:22 +00:00
|
|
|
* Internal dependencies
|
2020-07-15 12:10:21 +00:00
|
|
|
*/
|
|
|
|
import { PayPal } from '../tasks/payments/paypal';
|
2020-12-16 02:04:56 +00:00
|
|
|
import { getPaymentMethods } from '../tasks/payments/methods';
|
2020-07-15 12:10:21 +00:00
|
|
|
|
2020-08-13 02:05:22 +00:00
|
|
|
jest.mock( '@wordpress/api-fetch' );
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
describe( 'TaskList > Payments', () => {
|
2020-12-16 02:04:56 +00:00
|
|
|
describe( 'Methods', () => {
|
|
|
|
const params = {
|
|
|
|
activePlugins: [],
|
|
|
|
countryCode: 'SE',
|
|
|
|
onboardingStatus: {},
|
|
|
|
options: [],
|
|
|
|
profileItems: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
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',
|
|
|
|
'UK',
|
|
|
|
];
|
|
|
|
supportedCountryCodes.forEach( ( countryCode ) => {
|
|
|
|
params.countryCode = countryCode;
|
|
|
|
const methods = getPaymentMethods( params );
|
|
|
|
expect(
|
|
|
|
methods.some( ( e ) => e.key === 'klarna_payments' )
|
|
|
|
).toBe( true );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2020-07-15 12:10:21 +00:00
|
|
|
describe( 'PayPal', () => {
|
|
|
|
afterEach( () => jest.clearAllMocks() );
|
|
|
|
|
|
|
|
const mockInstallStep = {
|
|
|
|
isComplete: true,
|
|
|
|
key: 'install',
|
|
|
|
label: 'Install',
|
|
|
|
};
|
|
|
|
|
|
|
|
it( 'shows "create account" when Jetpack and WCS are connected', async () => {
|
|
|
|
const mockConnectUrl = 'https://connect.woocommerce.test/paypal';
|
|
|
|
apiFetch.mockResolvedValue( {
|
|
|
|
connectUrl: mockConnectUrl,
|
|
|
|
} );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'jetpack',
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
'woocommerce-services',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
isJetpackConnected
|
|
|
|
wcsTosAccepted
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// By default, the "create account" is checked.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeChecked();
|
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'Email address', { selector: 'input' } )
|
|
|
|
).toBeDefined();
|
|
|
|
expect(
|
|
|
|
screen.getByText( 'Create account', { selector: 'button' } )
|
|
|
|
).toBeDefined();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// The email input should disappear when "create account" is unchecked.
|
2020-07-28 02:32:58 +00:00
|
|
|
user.click(
|
|
|
|
screen.getByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
screen.queryByLabelText( 'Email address', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Since the oauth response was mocked, we should have a "connect" button.
|
2020-07-28 02:32:58 +00:00
|
|
|
const oauthButton = await screen.findByText( 'Connect', {
|
|
|
|
selector: 'a',
|
|
|
|
} );
|
2020-07-15 12:10:21 +00:00
|
|
|
expect( oauthButton ).toBeDefined();
|
|
|
|
expect( oauthButton.href ).toEqual( mockConnectUrl );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'requires WCS to have TOS accepted to show "create account"', async () => {
|
|
|
|
const mockConnectUrl = 'https://connect.woocommerce.test/paypal';
|
|
|
|
apiFetch.mockResolvedValue( {
|
|
|
|
connectUrl: mockConnectUrl,
|
|
|
|
} );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'jetpack',
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
'woocommerce-services',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
isJetpackConnected
|
|
|
|
wcsTosAccepted={ false }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify "create account" isn't shown.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
screen.queryByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Since the oauth response was mocked, we should have a "connect" button.
|
2020-07-28 02:32:58 +00:00
|
|
|
const oauthButton = await screen.findByText( 'Connect', {
|
|
|
|
selector: 'a',
|
|
|
|
} );
|
2020-07-15 12:10:21 +00:00
|
|
|
expect( oauthButton ).toBeDefined();
|
|
|
|
expect( oauthButton.href ).toEqual( mockConnectUrl );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'validates "create account" form and persists PayPal options', async () => {
|
|
|
|
const mockConnectUrl = 'https://connect.woocommerce.test/paypal';
|
|
|
|
apiFetch.mockResolvedValue( {
|
|
|
|
connectUrl: mockConnectUrl,
|
|
|
|
} );
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
const mockUpdateOptions = jest
|
|
|
|
.fn()
|
|
|
|
.mockResolvedValue( { success: true } );
|
2020-07-15 12:10:21 +00:00
|
|
|
const mockCreateNotice = jest.fn();
|
|
|
|
const mockMarkConfigured = jest.fn();
|
|
|
|
const mockOptions = {
|
|
|
|
woocommerce_ppec_paypal_settings: {
|
|
|
|
test: 'yes',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'jetpack',
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
'woocommerce-services',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
isJetpackConnected
|
|
|
|
wcsTosAccepted
|
|
|
|
options={ mockOptions }
|
|
|
|
createNotice={ mockCreateNotice }
|
|
|
|
markConfigured={ mockMarkConfigured }
|
|
|
|
updateOptions={ mockUpdateOptions }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
const createButton = screen.getByText( 'Create account', {
|
|
|
|
selector: 'button',
|
|
|
|
} );
|
|
|
|
const emailInput = screen.getByLabelText( 'Email address', {
|
|
|
|
selector: 'input',
|
|
|
|
} );
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Verify empty emails are invalid.
|
|
|
|
user.click( createButton );
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
await screen.findByText( 'Please enter a valid email address' )
|
|
|
|
).toBeDefined();
|
2020-07-15 12:10:21 +00:00
|
|
|
expect( mockUpdateOptions ).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Verify non-empty email validation.
|
|
|
|
await user.type( emailInput, 'not an email' );
|
|
|
|
user.click( createButton );
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
await screen.findByText( 'Please enter a valid email address' )
|
|
|
|
).toBeDefined();
|
2020-07-15 12:10:21 +00:00
|
|
|
expect( mockUpdateOptions ).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Submit a good email.
|
|
|
|
user.clear( emailInput );
|
|
|
|
await user.type( emailInput, 'owner@store.com' );
|
|
|
|
user.click( createButton );
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
screen.queryByText( 'Please enter a valid email address' )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Trick to wait for the async code to call updateOption().
|
2020-07-28 02:32:58 +00:00
|
|
|
await waitFor( () =>
|
|
|
|
expect( mockUpdateOptions ).toHaveBeenCalledTimes( 1 )
|
|
|
|
);
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Verify the persisted options.
|
|
|
|
expect( mockUpdateOptions ).toHaveBeenCalledWith( {
|
|
|
|
woocommerce_ppec_paypal_settings: {
|
|
|
|
email: 'owner@store.com',
|
|
|
|
enabled: 'yes',
|
|
|
|
reroute_requests: 'yes',
|
|
|
|
test: 'yes', // Makes sure we're extending the retrieved settings.
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'shows API credential inputs when "create account" opted out and OAuth fetch fails', async () => {
|
|
|
|
apiFetch.mockResolvedValue( false );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'jetpack',
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
'woocommerce-services',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
isJetpackConnected
|
|
|
|
wcsTosAccepted
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// The email input should disappear when "create account" is unchecked.
|
2020-07-28 02:32:58 +00:00
|
|
|
user.click(
|
|
|
|
screen.getByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
screen.queryByLabelText( 'Email address', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Since the oauth response failed, we should have the API credentials form.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
await screen.findByText( 'Proceed', { selector: 'button' } )
|
|
|
|
).toBeDefined();
|
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'API Username', { selector: 'input' } )
|
|
|
|
).toBeDefined();
|
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'API Password', { selector: 'input' } )
|
|
|
|
).toBeDefined();
|
2020-07-15 12:10:21 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'shows OAuth connect button', async () => {
|
|
|
|
const mockConnectUrl = 'https://connect.woocommerce.test/paypal';
|
|
|
|
apiFetch.mockResolvedValue( {
|
|
|
|
connectUrl: mockConnectUrl,
|
|
|
|
} );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify the "create account" option is absent.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
screen.queryByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Since the oauth response was mocked, we should have a "connect" button.
|
2020-07-28 02:32:58 +00:00
|
|
|
const oauthButton = await screen.findByText( 'Connect', {
|
|
|
|
selector: 'a',
|
|
|
|
} );
|
2020-07-15 12:10:21 +00:00
|
|
|
expect( oauthButton ).toBeDefined();
|
|
|
|
expect( oauthButton.href ).toEqual( mockConnectUrl );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'shows API credential inputs when OAuth fetch fails', async () => {
|
|
|
|
apiFetch.mockResolvedValue( false );
|
|
|
|
|
|
|
|
render(
|
|
|
|
<PayPal
|
|
|
|
activePlugins={ [
|
|
|
|
'woocommerce-gateway-paypal-express-checkout',
|
|
|
|
] }
|
|
|
|
installStep={ mockInstallStep }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
// Verify the "create account" option is absent.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
screen.queryByLabelText( 'Create a PayPal account for me', {
|
|
|
|
selector: 'input',
|
|
|
|
} )
|
|
|
|
).toBeNull();
|
2020-07-15 12:10:21 +00:00
|
|
|
|
|
|
|
// Since the oauth response failed, we should have the API credentials form.
|
2020-07-28 02:32:58 +00:00
|
|
|
expect(
|
|
|
|
await screen.findByText( 'Proceed', { selector: 'button' } )
|
|
|
|
).toBeDefined();
|
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'API Username', { selector: 'input' } )
|
|
|
|
).toBeDefined();
|
|
|
|
expect(
|
|
|
|
screen.getByLabelText( 'API Password', { selector: 'input' } )
|
|
|
|
).toBeDefined();
|
2020-07-15 12:10:21 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|