Skip to content

Commit

Permalink
Fix crash when null date passed to TimePicker (#27316)
Browse files Browse the repository at this point in the history
* Fix crash when null date passed.

* Update test
  • Loading branch information
tellthemachines authored Nov 27, 2020
1 parent 8b748e3 commit 51bf5f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/components/src/date-time/test/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,28 @@ describe( 'TimePicker', () => {

expect( monthInputIndex > dayInputIndex ).toBe( true );
} );

it( 'Should set a time when passed a null currentTime', () => {
const onChangeSpy = jest.fn();

render(
<TimePicker
currentTime={ null }
onChange={ onChangeSpy }
is12Hour
/>
);

const monthInput = screen.getByLabelText( 'Month' ).value;
const dayInput = screen.getByLabelText( 'Day' ).value;
const yearInput = screen.getByLabelText( 'Year' ).value;
const hoursInput = screen.getByLabelText( 'Hours' ).value;
const minutesInput = screen.getByLabelText( 'Minutes' ).value;

expect( Number.isNaN( parseInt( monthInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( dayInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( yearInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( hoursInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( minutesInput, 10 ) ) ).toBe( false );
} );
} );
4 changes: 3 additions & 1 deletion packages/components/src/date-time/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export function TimePicker( { is12Hour, currentTime, onChange } ) {

// Reset the state when currentTime changed.
useEffect( () => {
setDate( moment( currentTime ).startOf( 'minutes' ) );
setDate(
currentTime ? moment( currentTime ).startOf( 'minutes' ) : moment()
);
}, [ currentTime ] );

const { day, month, year, minutes, hours, am } = useMemo(
Expand Down

0 comments on commit 51bf5f3

Please sign in to comment.