@@ -519,7 +519,7 @@ describe('DatePicker', function () {
519
519
expect ( getTextValue ( combobox ) ) . toBe ( '2/4/2019, 1:45 AM' ) ;
520
520
} ) ;
521
521
522
- it ( 'should not fire onChange until both date and time are selected' , function ( ) {
522
+ it ( 'should fire onChange until both date and time are selected' , function ( ) {
523
523
let onChange = jest . fn ( ) ;
524
524
let { getByRole, getAllByRole, getAllByLabelText} = render (
525
525
< Provider theme = { theme } >
@@ -577,17 +577,18 @@ describe('DatePicker', function () {
577
577
578
578
expect ( document . activeElement ) . toHaveAttribute ( 'aria-valuetext' , '00' ) ;
579
579
580
- expect ( onChange ) . not . toHaveBeenCalled ( ) ;
581
- expectPlaceholder ( combobox , 'mm/dd/yyyy, ––:–– AM' ) ;
580
+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
581
+ let parts = formatter . formatToParts ( today ( getLocalTimeZone ( ) ) . toDate ( getLocalTimeZone ( ) ) ) ;
582
+ let month = parts . find ( p => p . type === 'month' ) . value ;
583
+ let day = parts . find ( p => p . type === 'day' ) . value ;
584
+ let year = parts . find ( p => p . type === 'year' ) . value ;
585
+ expectPlaceholder ( combobox , `${ month } /${ day } /${ year } , 12:00 AM` ) ;
582
586
583
587
fireEvent . keyDown ( hour , { key : 'ArrowRight' } ) ;
584
588
fireEvent . keyUp ( hour , { key : 'ArrowRight' } ) ;
585
589
586
590
expect ( document . activeElement ) . toHaveAttribute ( 'aria-label' , 'AM/PM, ' ) ;
587
- expect ( document . activeElement ) . toHaveAttribute ( 'aria-valuetext' , 'Empty' ) ;
588
-
589
- fireEvent . keyDown ( document . activeElement , { key : 'ArrowUp' } ) ;
590
- fireEvent . keyUp ( document . activeElement , { key : 'ArrowUp' } ) ;
591
+ expect ( document . activeElement ) . toHaveAttribute ( 'aria-valuetext' , 'AM' ) ;
591
592
592
593
expect ( dialog ) . toBeVisible ( ) ;
593
594
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -670,6 +671,68 @@ describe('DatePicker', function () {
670
671
expect ( onChange ) . not . toHaveBeenCalled ( ) ;
671
672
} ) ;
672
673
674
+ it ( 'should confirm valid date time on dialog close' , function ( ) {
675
+ let onChange = jest . fn ( ) ;
676
+ let { getByRole, getAllByRole, getAllByLabelText} = render (
677
+ < Provider theme = { theme } >
678
+ < DatePicker label = "Date" granularity = "minute" onChange = { onChange } />
679
+ </ Provider >
680
+ ) ;
681
+
682
+ let combobox = getAllByRole ( 'group' ) [ 0 ] ;
683
+ expectPlaceholder ( combobox , 'mm/dd/yyyy, ––:–– AM' ) ;
684
+
685
+ let button = getByRole ( 'button' ) ;
686
+ triggerPress ( button ) ;
687
+ act ( ( ) => jest . runAllTimers ( ) ) ;
688
+
689
+ let dialog = getByRole ( 'dialog' ) ;
690
+ expect ( dialog ) . toBeVisible ( ) ;
691
+
692
+ let cells = getAllByRole ( 'gridcell' ) ;
693
+ let todayCell = cells . find ( cell => cell . firstChild . getAttribute ( 'aria-label' ) ?. startsWith ( 'Today' ) ) ;
694
+ triggerPress ( todayCell . firstChild ) ;
695
+ expect ( todayCell ) . toHaveAttribute ( 'aria-selected' , 'true' ) ;
696
+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
697
+
698
+ let timeField = getAllByLabelText ( 'Time' ) [ 0 ] ;
699
+ expectPlaceholder ( timeField , '––:–– AM' ) ;
700
+
701
+ let hour = within ( timeField ) . getByLabelText ( 'hour,' ) ;
702
+ expect ( hour ) . toHaveAttribute ( 'role' , 'spinbutton' ) ;
703
+ expect ( hour ) . toHaveAttribute ( 'aria-valuetext' , 'Empty' ) ;
704
+
705
+ act ( ( ) => hour . focus ( ) ) ;
706
+ fireEvent . keyDown ( hour , { key : 'ArrowUp' } ) ;
707
+ fireEvent . keyUp ( hour , { key : 'ArrowUp' } ) ;
708
+
709
+ expect ( hour ) . toHaveAttribute ( 'aria-valuetext' , '12 AM' ) ;
710
+
711
+ let minute = within ( timeField ) . getByLabelText ( 'minute,' ) ;
712
+ expect ( minute ) . toHaveAttribute ( 'role' , 'spinbutton' ) ;
713
+ expect ( minute ) . toHaveAttribute ( 'aria-valuetext' , 'Empty' ) ;
714
+
715
+ act ( ( ) => minute . focus ( ) ) ;
716
+ fireEvent . keyDown ( minute , { key : 'ArrowUp' } ) ;
717
+ fireEvent . keyUp ( minute , { key : 'ArrowUp' } ) ;
718
+ fireEvent . keyDown ( minute , { key : 'ArrowUp' } ) ;
719
+ fireEvent . keyUp ( minute , { key : 'ArrowUp' } ) ;
720
+
721
+ expect ( minute ) . toHaveAttribute ( 'aria-valuetext' , '01' ) ;
722
+
723
+ userEvent . click ( document . body ) ;
724
+ act ( ( ) => jest . runAllTimers ( ) ) ;
725
+
726
+ expect ( dialog ) . not . toBeInTheDocument ( ) ;
727
+ expect ( onChange ) . toHaveBeenCalledTimes ( 2 ) ;
728
+ let formatter = new Intl . DateTimeFormat ( 'en-US' , { year : 'numeric' , month : 'numeric' , day : 'numeric' } ) ;
729
+ let parts = formatter . formatToParts ( today ( getLocalTimeZone ( ) ) . toDate ( getLocalTimeZone ( ) ) ) ;
730
+ let month = parts . find ( p => p . type === 'month' ) . value ;
731
+ let day = parts . find ( p => p . type === 'day' ) . value ;
732
+ let year = parts . find ( p => p . type === 'year' ) . value ;
733
+ expectPlaceholder ( combobox , `${ month } /${ day } /${ year } , 12:01 AM` ) ;
734
+ } ) ;
735
+
673
736
it ( 'should clear date and time when controlled value is set to null' , function ( ) {
674
737
function ControlledDatePicker ( ) {
675
738
let [ value , setValue ] = React . useState ( null ) ;
@@ -715,8 +778,6 @@ describe('DatePicker', function () {
715
778
fireEvent . keyDown ( hour , { key : 'ArrowRight' } ) ;
716
779
fireEvent . keyUp ( hour , { key : 'ArrowRight' } ) ;
717
780
expect ( document . activeElement ) . toHaveAttribute ( 'aria-label' , 'AM/PM, ' ) ;
718
- fireEvent . keyDown ( document . activeElement , { key : 'ArrowUp' } ) ;
719
- fireEvent . keyUp ( document . activeElement , { key : 'ArrowUp' } ) ;
720
781
expect ( document . activeElement ) . toHaveAttribute ( 'aria-valuetext' , 'AM' ) ;
721
782
722
783
userEvent . click ( document . body ) ;
0 commit comments