From 085f446c8623cb3a9841700bafa56998ebae2424 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 27 Nov 2020 10:40:53 +1100 Subject: [PATCH 1/2] Fix crash when null date passed. --- .../components/src/date-time/test/time.js | 24 +++++++++++++++++++ packages/components/src/date-time/time.js | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/components/src/date-time/test/time.js b/packages/components/src/date-time/test/time.js index 32a47ca9ed2dc6..203ac00631d688 100644 --- a/packages/components/src/date-time/test/time.js +++ b/packages/components/src/date-time/test/time.js @@ -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( + + ); + + 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( isNaN( monthInput ) ).toBe( false ); + expect( isNaN( dayInput ) ).toBe( false ); + expect( isNaN( yearInput ) ).toBe( false ); + expect( isNaN( hoursInput ) ).toBe( false ); + expect( isNaN( minutesInput ) ).toBe( false ); + } ); } ); diff --git a/packages/components/src/date-time/time.js b/packages/components/src/date-time/time.js index 5df865e95e27d4..de10573f992524 100644 --- a/packages/components/src/date-time/time.js +++ b/packages/components/src/date-time/time.js @@ -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( From 074477a0da6389e06805257c248238edadd22e49 Mon Sep 17 00:00:00 2001 From: tellthemachines Date: Fri, 27 Nov 2020 13:56:16 +1100 Subject: [PATCH 2/2] Update test --- packages/components/src/date-time/test/time.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/components/src/date-time/test/time.js b/packages/components/src/date-time/test/time.js index 203ac00631d688..7124e80e6c1b1a 100644 --- a/packages/components/src/date-time/test/time.js +++ b/packages/components/src/date-time/test/time.js @@ -289,10 +289,10 @@ describe( 'TimePicker', () => { const hoursInput = screen.getByLabelText( 'Hours' ).value; const minutesInput = screen.getByLabelText( 'Minutes' ).value; - expect( isNaN( monthInput ) ).toBe( false ); - expect( isNaN( dayInput ) ).toBe( false ); - expect( isNaN( yearInput ) ).toBe( false ); - expect( isNaN( hoursInput ) ).toBe( false ); - expect( isNaN( minutesInput ) ).toBe( false ); + 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 ); } ); } );