Skip to content

Commit

Permalink
Fix issue with the orignal value
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemd24 committed Sep 15, 2024
1 parent e4686c5 commit f218b47
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ const CountriesTimeInput = ( {
return <AppSpinner />;
}

/**
*
* @param {Object} e The event object
* @param {number} numberValue The string value of the input field converted to a number
* @param {string} field The field name: time or maxTime
*/
const handleBlur = ( e, numberValue, field ) => {
if ( time === numberValue ) {
if ( value[ field ] === numberValue ) {
return;
}

Expand All @@ -47,6 +53,11 @@ const CountriesTimeInput = ( {
} );
};

/**
*
* @param {number} numberValue The string value of the input field converted to a number
* @param {string} field The field name: time or maxTime
*/
const handleIncrement = ( numberValue, field ) => {
onChange( {
...value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,47 @@ describe( 'CountriesTimeInput', () => {
expect( onChange.mock.calls[ 3 ][ 0 ].maxTime ).toBe( 32 );
} );
} );

describe( 'Test handleBlur', () => {
it( 'Test onChange when handleBlur is called', async () => {
const onChange = jest.fn();
const onDelete = jest.fn();
const { queryAllByRole } = render(
<CountriesTimeInput
value={ { countries: [ 'ES' ], time: 1, maxTime: 32 } }
audienceCountries={ [ 'ES' ] }
onChange={ onChange }
onDelete={ onDelete }
/>
);

const inputs = queryAllByRole( 'textbox' );

expect( inputs ).toHaveLength( 2 );

const [ timeInput, maxTimeInput ] = inputs;

// The value is the same, so the onChange function shouldnt be called
fireEvent.blur( timeInput, { target: { value: '1' } } );
expect( onChange ).toHaveBeenCalledTimes( 0 );

// The value is different, so the onChange function should be called
fireEvent.blur( timeInput, { target: { value: '2' } } );
expect( onChange ).toHaveBeenCalledTimes( 1 );
// It should update the time property.
expect( onChange.mock.calls[ 0 ][ 0 ].time ).toBe( 2 );

onChange.mockClear();

// The value is the same, so the onChange function shouldnt be called
fireEvent.blur( maxTimeInput, { target: { value: '32' } } );
expect( onChange ).toHaveBeenCalledTimes( 0 );

// The value is different, so the onChange function should be called
fireEvent.blur( maxTimeInput, { target: { value: '10' } } );
expect( onChange ).toHaveBeenCalledTimes( 1 );
// It should update the maxTime property.
expect( onChange.mock.calls[ 0 ][ 0 ].maxTime ).toBe( 10 );
} );
} );
} );

0 comments on commit f218b47

Please sign in to comment.