Migrate @woocommerce/customer-effort-score to TS
This commit is contained in:
parent
59a66968f8
commit
ff5417c99f
|
@ -13,6 +13,15 @@ import { CustomerFeedbackModal } from './customer-feedback-modal';
|
||||||
|
|
||||||
const noop = () => {};
|
const noop = () => {};
|
||||||
|
|
||||||
|
type CustomerEffortScoreProps = {
|
||||||
|
recordScoreCallback: ( score: number, comments: string ) => void;
|
||||||
|
label: string;
|
||||||
|
onNoticeShownCallback?: () => void;
|
||||||
|
onNoticeDismissedCallback?: () => void;
|
||||||
|
onModalShownCallback?: () => void;
|
||||||
|
icon?: React.ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use `CustomerEffortScore` to gather a customer effort score.
|
* Use `CustomerEffortScore` to gather a customer effort score.
|
||||||
*
|
*
|
||||||
|
@ -27,14 +36,14 @@ const noop = () => {};
|
||||||
* @param {Function} props.onModalShownCallback Function to call when the modal is shown.
|
* @param {Function} props.onModalShownCallback Function to call when the modal is shown.
|
||||||
* @param {Object} props.icon Icon (React component) to be shown on the notice.
|
* @param {Object} props.icon Icon (React component) to be shown on the notice.
|
||||||
*/
|
*/
|
||||||
function CustomerEffortScore( {
|
const CustomerEffortScore: React.FC< CustomerEffortScoreProps > = ( {
|
||||||
recordScoreCallback,
|
recordScoreCallback,
|
||||||
label,
|
label,
|
||||||
onNoticeShownCallback = noop,
|
onNoticeShownCallback = noop,
|
||||||
onNoticeDismissedCallback = noop,
|
onNoticeDismissedCallback = noop,
|
||||||
onModalShownCallback = noop,
|
onModalShownCallback = noop,
|
||||||
icon,
|
icon,
|
||||||
} ) {
|
} ) => {
|
||||||
const [ shouldCreateNotice, setShouldCreateNotice ] = useState( true );
|
const [ shouldCreateNotice, setShouldCreateNotice ] = useState( true );
|
||||||
const [ visible, setVisible ] = useState( false );
|
const [ visible, setVisible ] = useState( false );
|
||||||
const { createNotice } = useDispatch( 'core/notices2' );
|
const { createNotice } = useDispatch( 'core/notices2' );
|
||||||
|
@ -78,7 +87,7 @@ function CustomerEffortScore( {
|
||||||
recordScoreCallback={ recordScoreCallback }
|
recordScoreCallback={ recordScoreCallback }
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
CustomerEffortScore.propTypes = {
|
CustomerEffortScore.propTypes = {
|
||||||
/**
|
/**
|
|
@ -13,7 +13,12 @@ const mockOnSelectCallback = jest.fn();
|
||||||
|
|
||||||
describe( 'CustomerFeedbackSimple', () => {
|
describe( 'CustomerFeedbackSimple', () => {
|
||||||
it( 'should trigger recordScoreCallback when item is selected', () => {
|
it( 'should trigger recordScoreCallback when item is selected', () => {
|
||||||
render( <CustomerFeedbackSimple onSelect={ mockOnSelectCallback } /> );
|
render(
|
||||||
|
<CustomerFeedbackSimple
|
||||||
|
onSelect={ mockOnSelectCallback }
|
||||||
|
label=""
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
// Select the option.
|
// Select the option.
|
||||||
fireEvent.click( screen.getAllByText( '🙂' )[ 0 ] );
|
fireEvent.click( screen.getAllByText( '🙂' )[ 0 ] );
|
|
@ -27,7 +27,7 @@ jest.mock( '@wordpress/data', () => {
|
||||||
describe( 'CustomerEffortScore', () => {
|
describe( 'CustomerEffortScore', () => {
|
||||||
it( 'should call createNotice with appropriate parameters', async () => {
|
it( 'should call createNotice with appropriate parameters', async () => {
|
||||||
const mockCreateNotice = jest.fn();
|
const mockCreateNotice = jest.fn();
|
||||||
useDispatch.mockReturnValue( {
|
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||||
createNotice: mockCreateNotice,
|
createNotice: mockCreateNotice,
|
||||||
} );
|
} );
|
||||||
const icon = <span>icon</span>;
|
const icon = <span>icon</span>;
|
||||||
|
@ -56,13 +56,12 @@ describe( 'CustomerEffortScore', () => {
|
||||||
|
|
||||||
it( 'should not call createNotice on rerender', async () => {
|
it( 'should not call createNotice on rerender', async () => {
|
||||||
const mockCreateNotice = jest.fn();
|
const mockCreateNotice = jest.fn();
|
||||||
useDispatch.mockReturnValue( {
|
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||||
createNotice: mockCreateNotice,
|
createNotice: mockCreateNotice,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
const { rerender } = render(
|
const { rerender } = render(
|
||||||
<CustomerEffortScore
|
<CustomerEffortScore
|
||||||
createNotice={ mockCreateNotice }
|
|
||||||
recordScoreCallback={ noop }
|
recordScoreCallback={ noop }
|
||||||
label={ 'label' }
|
label={ 'label' }
|
||||||
/>
|
/>
|
||||||
|
@ -93,7 +92,15 @@ describe( 'CustomerEffortScore', () => {
|
||||||
|
|
||||||
it( 'should show dialog if "Give feedback" callback is run', async () => {
|
it( 'should show dialog if "Give feedback" callback is run', async () => {
|
||||||
const mockOnModalShownCallback = jest.fn();
|
const mockOnModalShownCallback = jest.fn();
|
||||||
const createNotice = ( ...args ) => {
|
const createNotice = (
|
||||||
|
...args: [
|
||||||
|
unknown,
|
||||||
|
unknown,
|
||||||
|
{
|
||||||
|
actions: [ { onClick: () => void } ];
|
||||||
|
}
|
||||||
|
]
|
||||||
|
) => {
|
||||||
// We're only interested in the 3rd argument.
|
// We're only interested in the 3rd argument.
|
||||||
const { actions } = args[ 2 ];
|
const { actions } = args[ 2 ];
|
||||||
|
|
||||||
|
@ -107,7 +114,7 @@ describe( 'CustomerEffortScore', () => {
|
||||||
// Modal shown callback should also be called.
|
// Modal shown callback should also be called.
|
||||||
expect( mockOnModalShownCallback ).toHaveBeenCalled();
|
expect( mockOnModalShownCallback ).toHaveBeenCalled();
|
||||||
};
|
};
|
||||||
useDispatch.mockReturnValue( {
|
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||||
createNotice,
|
createNotice,
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Reference in New Issue