@@ -558,64 +558,118 @@ describe('Date picker', () => {
558558 expect ( previousYearButton . tabIndex ) . toEqual ( 0 ) ;
559559 } ) ;
560560
561- it ( 'should set focus on nearest date when day input is incorrectly formatted ' , ( ) => {
561+ it ( 'should set focus on today when day input is not a number ' , ( ) => {
562562 datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
563563 const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
564564 const dayInput = document . querySelector ( '.date-picker-day' ) ;
565565 const monthInput = document . querySelector ( '.date-picker-month' ) ;
566566 const yearInput = document . querySelector ( '.date-picker-year' ) ;
567567
568- $ ( dayInput ) . val ( '40 ' ) ;
568+ $ ( dayInput ) . val ( '3 12 ' ) ;
569569 $ ( monthInput ) . val ( '11' ) ;
570570 $ ( yearInput ) . val ( '2021' ) ;
571571
572572 $ ( revealButton ) . trigger ( 'click' ) ;
573573
574- const nearestDate = DateTime . fromObject ( { year : 2021 , month : 11 , day : 30 } ) ;
575- const nearestDateButton = document . querySelector ( `[data-test-id="${ nearestDate . toLocaleString ( ) } "]` ) ;
574+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
576575
577- expect ( nearestDateButton === document . activeElement ) . toBeTruthy ( ) ;
578- expect ( nearestDateButton . tabIndex ) . toEqual ( 0 ) ;
576+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
577+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
579578 } ) ;
580579
581- it ( 'should set focus on nearest date when month input is incorrectly formatted ' , ( ) => {
580+ it ( 'should set focus on today when month input is not a number ' , ( ) => {
582581 datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
583582 const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
584583 const dayInput = document . querySelector ( '.date-picker-day' ) ;
585584 const monthInput = document . querySelector ( '.date-picker-month' ) ;
586585 const yearInput = document . querySelector ( '.date-picker-year' ) ;
587586
588587 $ ( dayInput ) . val ( '15' ) ;
589- $ ( monthInput ) . val ( '13 ' ) ;
588+ $ ( monthInput ) . val ( '11 20 ' ) ;
590589 $ ( yearInput ) . val ( '2021' ) ;
591590
592591 $ ( revealButton ) . trigger ( 'click' ) ;
593592
594- const nearestDate = DateTime . fromObject ( { year : 2021 , month : 12 , day : 15 } ) ;
595- const nearestDateButton = document . querySelector ( `[data-test-id="${ nearestDate . toLocaleString ( ) } "]` ) ;
593+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
596594
597- expect ( nearestDateButton === document . activeElement ) . toBeTruthy ( ) ;
598- expect ( nearestDateButton . tabIndex ) . toEqual ( 0 ) ;
595+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
596+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
599597 } ) ;
600598
601- it ( 'should set focus on 31st December when day and month input is incorrectly formatted ' , ( ) => {
599+ it ( 'should set focus on today when year input is not a number ' , ( ) => {
602600 datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
603601 const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
604602 const dayInput = document . querySelector ( '.date-picker-day' ) ;
605603 const monthInput = document . querySelector ( '.date-picker-month' ) ;
606604 const yearInput = document . querySelector ( '.date-picker-year' ) ;
607605
608- $ ( dayInput ) . val ( '32' ) ;
606+ $ ( dayInput ) . val ( '15' ) ;
607+ $ ( monthInput ) . val ( '11' ) ;
608+ $ ( yearInput ) . val ( '20 22' ) ;
609+
610+ $ ( revealButton ) . trigger ( 'click' ) ;
611+
612+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
613+
614+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
615+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
616+ } ) ;
617+
618+ it ( 'should set focus on today when day input is an invalid number' , ( ) => {
619+ datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
620+ const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
621+ const dayInput = document . querySelector ( '.date-picker-day' ) ;
622+ const monthInput = document . querySelector ( '.date-picker-month' ) ;
623+ const yearInput = document . querySelector ( '.date-picker-year' ) ;
624+
625+ $ ( dayInput ) . val ( '1125' ) ;
626+ $ ( monthInput ) . val ( '11' ) ;
627+ $ ( yearInput ) . val ( '2022' ) ;
628+
629+ $ ( revealButton ) . trigger ( 'click' ) ;
630+
631+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
632+
633+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
634+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
635+ } ) ;
636+
637+ it ( 'should set focus on today when month input is an invalid number' , ( ) => {
638+ datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
639+ const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
640+ const dayInput = document . querySelector ( '.date-picker-day' ) ;
641+ const monthInput = document . querySelector ( '.date-picker-month' ) ;
642+ const yearInput = document . querySelector ( '.date-picker-year' ) ;
643+
644+ $ ( dayInput ) . val ( '11' ) ;
609645 $ ( monthInput ) . val ( '13' ) ;
610646 $ ( yearInput ) . val ( '2022' ) ;
611647
612648 $ ( revealButton ) . trigger ( 'click' ) ;
613649
614- const nearestDate = DateTime . fromObject ( { year : 2022 , month : 12 , day : 31 } ) ;
615- const nearestDateButton = document . querySelector ( `[data-test-id="${ nearestDate . toLocaleString ( ) } "]` ) ;
650+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
616651
617- expect ( nearestDateButton === document . activeElement ) . toBeTruthy ( ) ;
618- expect ( nearestDateButton . tabIndex ) . toEqual ( 0 ) ;
652+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
653+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
654+ } ) ;
655+
656+ it ( 'should set focus on today when year input is an invalid number' , ( ) => {
657+ datePicker ( document . querySelector ( '.date-picker' ) , { } ) ;
658+ const revealButton = document . querySelector ( '.date-picker__reveal' ) ;
659+ const dayInput = document . querySelector ( '.date-picker-day' ) ;
660+ const monthInput = document . querySelector ( '.date-picker-month' ) ;
661+ const yearInput = document . querySelector ( '.date-picker-year' ) ;
662+
663+ $ ( dayInput ) . val ( '11' ) ;
664+ $ ( monthInput ) . val ( '11' ) ;
665+ $ ( yearInput ) . val ( '202220222022202220222022' ) ;
666+
667+ $ ( revealButton ) . trigger ( 'click' ) ;
668+
669+ const focusedDate = document . querySelector ( `[data-test-id="${ now . toLocaleString ( ) } "]` ) ;
670+
671+ expect ( focusedDate === document . activeElement ) . toBeTruthy ( ) ;
672+ expect ( focusedDate . tabIndex ) . toEqual ( 0 ) ;
619673 } ) ;
620674 } ) ;
621675
0 commit comments