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 = () => {};
|
||||
|
||||
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.
|
||||
*
|
||||
|
@ -27,14 +36,14 @@ const noop = () => {};
|
|||
* @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.
|
||||
*/
|
||||
function CustomerEffortScore( {
|
||||
const CustomerEffortScore: React.FC< CustomerEffortScoreProps > = ( {
|
||||
recordScoreCallback,
|
||||
label,
|
||||
onNoticeShownCallback = noop,
|
||||
onNoticeDismissedCallback = noop,
|
||||
onModalShownCallback = noop,
|
||||
icon,
|
||||
} ) {
|
||||
} ) => {
|
||||
const [ shouldCreateNotice, setShouldCreateNotice ] = useState( true );
|
||||
const [ visible, setVisible ] = useState( false );
|
||||
const { createNotice } = useDispatch( 'core/notices2' );
|
||||
|
@ -78,7 +87,7 @@ function CustomerEffortScore( {
|
|||
recordScoreCallback={ recordScoreCallback }
|
||||
/>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
CustomerEffortScore.propTypes = {
|
||||
/**
|
|
@ -13,7 +13,12 @@ const mockOnSelectCallback = jest.fn();
|
|||
|
||||
describe( 'CustomerFeedbackSimple', () => {
|
||||
it( 'should trigger recordScoreCallback when item is selected', () => {
|
||||
render( <CustomerFeedbackSimple onSelect={ mockOnSelectCallback } /> );
|
||||
render(
|
||||
<CustomerFeedbackSimple
|
||||
onSelect={ mockOnSelectCallback }
|
||||
label=""
|
||||
/>
|
||||
);
|
||||
|
||||
// Select the option.
|
||||
fireEvent.click( screen.getAllByText( '🙂' )[ 0 ] );
|
|
@ -27,7 +27,7 @@ jest.mock( '@wordpress/data', () => {
|
|||
describe( 'CustomerEffortScore', () => {
|
||||
it( 'should call createNotice with appropriate parameters', async () => {
|
||||
const mockCreateNotice = jest.fn();
|
||||
useDispatch.mockReturnValue( {
|
||||
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||
createNotice: mockCreateNotice,
|
||||
} );
|
||||
const icon = <span>icon</span>;
|
||||
|
@ -56,13 +56,12 @@ describe( 'CustomerEffortScore', () => {
|
|||
|
||||
it( 'should not call createNotice on rerender', async () => {
|
||||
const mockCreateNotice = jest.fn();
|
||||
useDispatch.mockReturnValue( {
|
||||
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||
createNotice: mockCreateNotice,
|
||||
} );
|
||||
|
||||
const { rerender } = render(
|
||||
<CustomerEffortScore
|
||||
createNotice={ mockCreateNotice }
|
||||
recordScoreCallback={ noop }
|
||||
label={ 'label' }
|
||||
/>
|
||||
|
@ -93,7 +92,15 @@ describe( 'CustomerEffortScore', () => {
|
|||
|
||||
it( 'should show dialog if "Give feedback" callback is run', async () => {
|
||||
const mockOnModalShownCallback = jest.fn();
|
||||
const createNotice = ( ...args ) => {
|
||||
const createNotice = (
|
||||
...args: [
|
||||
unknown,
|
||||
unknown,
|
||||
{
|
||||
actions: [ { onClick: () => void } ];
|
||||
}
|
||||
]
|
||||
) => {
|
||||
// We're only interested in the 3rd argument.
|
||||
const { actions } = args[ 2 ];
|
||||
|
||||
|
@ -107,7 +114,7 @@ describe( 'CustomerEffortScore', () => {
|
|||
// Modal shown callback should also be called.
|
||||
expect( mockOnModalShownCallback ).toHaveBeenCalled();
|
||||
};
|
||||
useDispatch.mockReturnValue( {
|
||||
( useDispatch as jest.Mock ).mockReturnValue( {
|
||||
createNotice,
|
||||
} );
|
||||
|
Loading…
Reference in New Issue