Skip to content

Commit

Permalink
Add wordpress/data hooks to the general-settings component (#1824)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernando Espinosa <Ferdev@users.noreply.github.com>
Co-authored-by: Francesco <frosso@users.noreply.github.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: Waclaw Jacek <waclaw.jacek@automattic.com>
  • Loading branch information
4 people authored May 24, 2021
1 parent f1c3d51 commit cd7748b
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 25 deletions.
2 changes: 2 additions & 0 deletions client/data/settings/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export default {
SET_IS_WCPAY_ENABLED: 'SET_IS_WCPAY_ENABLED',
SET_ENABLED_PAYMENT_METHOD_IDS: 'SET_ENABLED_PAYMENT_METHOD_IDS',
SET_IS_SAVING_SETTINGS: 'SET_IS_SAVING_SETTINGS',
SET_IS_MANUAL_CAPTURE_ENABLED: 'SET_IS_MANUAL_CAPTURE_ENABLED',
SET_ACCOUNT_STATEMENT_DESCRIPTOR: 'SET_ACCOUNT_STATEMENT_DESCRIPTOR',
};
14 changes: 14 additions & 0 deletions client/data/settings/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ export function updateIsSavingSettings( isSaving ) {
};
}

export function updateIsManualCaptureEnabled( isEnabled ) {
return {
type: ACTION_TYPES.SET_IS_MANUAL_CAPTURE_ENABLED,
isEnabled,
};
}

export function updateAccountStatementDescriptor( accountStatementDescriptor ) {
return {
type: ACTION_TYPES.SET_ACCOUNT_STATEMENT_DESCRIPTOR,
accountStatementDescriptor,
};
}

export function* saveSettings() {
try {
const settings = select( STORE_NAME ).getSettings();
Expand Down
14 changes: 13 additions & 1 deletion client/data/settings/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,30 @@ export const useEnabledPaymentMethodIds = () => {
};

export const useGeneralSettings = () => {
const { updateAccountStatementDescriptor } = useDispatch( STORE_NAME );
const { updateIsManualCaptureEnabled } = useDispatch( STORE_NAME );
const { updateIsWCPayEnabled } = useDispatch( STORE_NAME );

return useSelect(
( select ) => {
const { getAccountStatementDescriptor } = select( STORE_NAME );
const { getIsWCPayEnabled } = select( STORE_NAME );
const { getIsManualCaptureEnabled } = select( STORE_NAME );

return {
accountStatementDescriptor: getAccountStatementDescriptor(),
isManualCaptureEnabled: getIsManualCaptureEnabled(),
isWCPayEnabled: getIsWCPayEnabled(),
updateAccountStatementDescriptor,
updateIsManualCaptureEnabled,
updateIsWCPayEnabled,
};
},
[ updateIsWCPayEnabled ]
[
updateAccountStatementDescriptor,
updateIsManualCaptureEnabled,
updateIsWCPayEnabled,
]
);
};

Expand Down
19 changes: 19 additions & 0 deletions client/data/settings/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ export const receiveSettings = (
is_wcpay_enabled: action.isEnabled,
},
};
case ACTION_TYPES.SET_IS_MANUAL_CAPTURE_ENABLED:
return {
...state,
data: {
...state.data,
// eslint-disable-next-line camelcase
is_manual_capture_enabled: action.isEnabled,
},
};
case ACTION_TYPES.SET_ACCOUNT_STATEMENT_DESCRIPTOR:
return {
...state,
data: {
...state.data,
// eslint-disable-next-line camelcase
account_statement_descriptor:
action.accountStatementDescriptor,
},
};
}

return state;
Expand Down
8 changes: 8 additions & 0 deletions client/data/settings/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ export const getAvailablePaymentMethodIds = ( state ) => {
export const isSavingSettings = ( state ) => {
return getSettingsState( state ).isSaving || false;
};

export const getAccountStatementDescriptor = ( state ) => {
return getSettings( state ).account_statement_descriptor || '';
};

export const getIsManualCaptureEnabled = ( state ) => {
return getSettings( state ).is_manual_capture_enabled || false;
};
22 changes: 22 additions & 0 deletions client/data/settings/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ describe( 'Settings hooks tests', () => {
test( 'returns general settings from selector', () => {
actions = {
updateIsWCPayEnabled: jest.fn(),
updateAccountStatementDescriptor: jest.fn(),
updateIsManualCaptureEnabled: jest.fn(),
};

selectors = {
getIsWCPayEnabled: jest.fn( () => 'foo' ),
getIsManualCaptureEnabled: jest.fn( () => 'bar' ),
getAccountStatementDescriptor: jest.fn( () => 'zxy' ),
};

const {
accountStatementDescriptor,
isManualCaptureEnabled,
isWCPayEnabled,
updateAccountStatementDescriptor,
updateIsManualCaptureEnabled,
updateIsWCPayEnabled,
} = useGeneralSettings();
updateIsWCPayEnabled( 'bar' );
Expand All @@ -77,6 +85,20 @@ describe( 'Settings hooks tests', () => {
expect( actions.updateIsWCPayEnabled ).toHaveBeenCalledWith(
'bar'
);

updateAccountStatementDescriptor( 'foo' );

expect( accountStatementDescriptor ).toEqual( 'zxy' );
expect(
actions.updateAccountStatementDescriptor
).toHaveBeenCalledWith( 'foo' );

updateIsManualCaptureEnabled( 'zxy' );

expect( isManualCaptureEnabled ).toEqual( 'bar' );
expect( actions.updateIsManualCaptureEnabled ).toHaveBeenCalledWith(
'zxy'
);
} );
} );

Expand Down
84 changes: 84 additions & 0 deletions client/data/settings/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
updateIsWCPayEnabled,
updateEnabledPaymentMethodIds,
updateIsSavingSettings,
updateIsManualCaptureEnabled,
updateAccountStatementDescriptor,
} from '../actions';

describe( 'Settings reducer tests', () => {
Expand Down Expand Up @@ -174,4 +176,86 @@ describe( 'Settings reducer tests', () => {
} );
} );
} );

describe( 'SET_IS_MANUAL_CAPTURE_ENABLED', () => {
test( 'toggles `data.is_manual_capture_enabled`', () => {
const oldState = {
data: {
is_manual_capture_enabled: false, // eslint-disable-line
},
};

const state = reducer(
oldState,
updateIsManualCaptureEnabled( true )
);

expect( state.data.is_manual_capture_enabled ).toBeTruthy();
} );

test( 'leaves other fields unchanged', () => {
const oldState = {
foo: 'bar',
data: {
is_manual_capture_enabled: false, // eslint-disable-line
baz: 'quux',
},
};

const state = reducer(
oldState,
updateIsManualCaptureEnabled( true )
);

expect( state ).toEqual( {
foo: 'bar',
data: {
is_manual_capture_enabled: true, // eslint-disable-line
baz: 'quux',
},
} );
} );
} );

describe( 'SET_ACCOUNT_STATEMENT_DESCRIPTOR', () => {
test( 'toggles `data.account_statement_descriptor`', () => {
const oldState = {
data: {
account_statement_descriptor: 'Statement', // eslint-disable-line
},
};

const state = reducer(
oldState,
updateAccountStatementDescriptor( 'New Statement' )
);

expect( state.data.account_statement_descriptor ).toEqual(
'New Statement'
);
} );

test( 'leaves other fields unchanged', () => {
const oldState = {
foo: 'bar',
data: {
account_statement_descriptor: 'Statement', // eslint-disable-line
baz: 'quux',
},
};

const state = reducer(
oldState,
updateAccountStatementDescriptor( 'New Statement' )
);

expect( state ).toEqual( {
foo: 'bar',
data: {
account_statement_descriptor: 'New Statement', // eslint-disable-line
baz: 'quux',
},
} );
} );
} );
} );
52 changes: 52 additions & 0 deletions client/data/settings/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
getSettings,
getIsWCPayEnabled,
getEnabledPaymentMethodIds,
getIsManualCaptureEnabled,
getAccountStatementDescriptor,
isSavingSettings,
} from '../selectors';

Expand Down Expand Up @@ -99,4 +101,54 @@ describe( 'Settings selectors tests', () => {
}
);
} );

describe( 'getIsManualCaptureEnabled()', () => {
test( 'returns the value of state.settings.data.is_manual_capture_enabled', () => {
const state = {
settings: {
data: {
// eslint-disable-next-line camelcase
is_manual_capture_enabled: true,
},
},
};

expect( getIsManualCaptureEnabled( state ) ).toBeTruthy();
} );

test.each( [
[ undefined ],
[ {} ],
[ { settings: {} } ],
[ { settings: { data: {} } } ],
] )( 'returns false if missing (tested state: %j)', ( state ) => {
expect( getIsManualCaptureEnabled( state ) ).toBeFalsy();
} );
} );

describe( 'getAccountStatementDescriptor()', () => {
test( 'returns the value of state.settings.data.account_statement_descriptor', () => {
const state = {
settings: {
data: {
// eslint-disable-next-line camelcase
account_statement_descriptor: 'my account statement',
},
},
};

expect( getAccountStatementDescriptor( state ) ).toEqual(
'my account statement'
);
} );

test.each( [
[ undefined ],
[ {} ],
[ { settings: {} } ],
[ { settings: { data: {} } } ],
] )( 'returns false if missing (tested state: %j)', ( state ) => {
expect( getAccountStatementDescriptor( state ) ).toEqual( '' );
} );
} );
} );
22 changes: 12 additions & 10 deletions client/settings/general-settings/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import React, { useState } from 'react';
import { __ } from '@wordpress/i18n';
import {
Card,
Expand All @@ -20,11 +19,14 @@ import { useGeneralSettings } from 'data';
const ACCOUNT_STATEMENT_MAX_LENGTH = 22;

const GeneralSettings = ( { accountLink } ) => {
const { isWCPayEnabled, updateIsWCPayEnabled } = useGeneralSettings();
const [ isManualCaptureEnabled, setIsManualCaptureEnabled ] = useState(
false
);
const [ accountStatement, setAccountStatement ] = useState( '' );
const {
accountStatementDescriptor,
isManualCaptureEnabled,
isWCPayEnabled,
updateAccountStatementDescriptor,
updateIsManualCaptureEnabled,
updateIsWCPayEnabled,
} = useGeneralSettings();

return (
<Card className="general-settings">
Expand All @@ -42,7 +44,7 @@ const GeneralSettings = ( { accountLink } ) => {
</h4>
<CheckboxControl
checked={ isManualCaptureEnabled }
onChange={ setIsManualCaptureEnabled }
onChange={ updateIsManualCaptureEnabled }
label={ __(
'Issue an authorization on checkout, and capture later',
'woocommerce-payments'
Expand All @@ -66,13 +68,13 @@ const GeneralSettings = ( { accountLink } ) => {
'Customer bank statement',
'woocommerce-payments'
) }
value={ accountStatement }
onChange={ setAccountStatement }
value={ accountStatementDescriptor }
onChange={ updateAccountStatementDescriptor }
maxLength={ ACCOUNT_STATEMENT_MAX_LENGTH }
hideLabelFromVision
/>
<span className="input-help-text" aria-hidden="true">
{ `${ accountStatement.length } / ${ ACCOUNT_STATEMENT_MAX_LENGTH }` }
{ `${ accountStatementDescriptor.length } / ${ ACCOUNT_STATEMENT_MAX_LENGTH }` }
</span>
</div>
<div className="general-settings__bank-information">
Expand Down
Loading

0 comments on commit cd7748b

Please sign in to comment.