@@ -534,15 +534,72 @@ describe('Radios', function () {
534
534
expect ( radios [ 2 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
535
535
} ) ;
536
536
537
- it ( 'RadioGroup roving tabIndex for autoFocus' , async ( ) => {
538
- jest . useFakeTimers ( ) ;
539
- let { getAllByRole} = renderRadioGroup ( RadioGroup , Radio , { } , [ { } , { autoFocus : true } , { } ] ) ;
540
- let radios = getAllByRole ( 'radio' ) ;
541
- act ( ( ) => { jest . runAllTimers ( ) ; } ) ;
542
- expect ( radios [ 0 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
543
- expect ( radios [ 1 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
544
- expect ( radios [ 2 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
545
- jest . useRealTimers ( ) ;
537
+ describe ( 'roving tab timers' , ( ) => {
538
+ beforeAll ( ( ) => {
539
+ jest . useFakeTimers ( ) ;
540
+ } ) ;
541
+ afterAll ( ( ) => {
542
+ jest . useRealTimers ( ) ;
543
+ } ) ;
544
+ it ( 'RadioGroup roving tabIndex for controlled radios' , async ( ) => {
545
+ function ControlledRadioGroup ( props ) {
546
+ let [ value , setValue ] = React . useState ( null ) ;
547
+ return (
548
+ < >
549
+ < Button variant = "primary" onPress = { ( ) => setValue ( 'cats' ) } >
550
+ Make it "Two"
551
+ </ Button >
552
+ < RadioGroup aria-label = "favorite pet" value = { value } onChange = { setValue } >
553
+ < Radio value = "dogs" > Dogs</ Radio >
554
+ < Radio value = "cats" > Cats</ Radio >
555
+ < Radio value = "dragons" > Dragons</ Radio >
556
+ < Radio value = "unicorns" > Unicorns</ Radio >
557
+ </ RadioGroup >
558
+ < Button variant = "primary" onPress = { ( ) => setValue ( 'dragons' ) } >
559
+ Make it "Three"
560
+ </ Button >
561
+ </ >
562
+ ) ;
563
+ }
564
+
565
+ let { getAllByRole} = render (
566
+ < ControlledRadioGroup />
567
+ ) ;
568
+ let radios = getAllByRole ( 'radio' ) ;
569
+ let buttons = getAllByRole ( 'button' ) ;
570
+ expect ( radios [ 0 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
571
+ expect ( radios [ 1 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
572
+ expect ( radios [ 2 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
573
+ expect ( radios [ 3 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
574
+
575
+ userEvent . tab ( ) ;
576
+ act ( ( ) => { jest . runAllTimers ( ) ; } ) ;
577
+ expect ( document . activeElement ) . toBe ( buttons [ 0 ] ) ;
578
+ fireEvent . keyDown ( document . activeElement , { key : 'Enter' } ) ;
579
+ fireEvent . keyUp ( document . activeElement , { key : 'Enter' } ) ;
580
+ userEvent . tab ( ) ;
581
+ act ( ( ) => { jest . runAllTimers ( ) ; } ) ;
582
+ expect ( document . activeElement ) . toBe ( radios [ 1 ] ) ;
583
+ userEvent . tab ( ) ;
584
+ act ( ( ) => { jest . runAllTimers ( ) ; } ) ;
585
+ expect ( document . activeElement ) . toBe ( buttons [ 1 ] ) ;
586
+ fireEvent . keyDown ( document . activeElement , { key : 'Enter' } ) ;
587
+ fireEvent . keyUp ( document . activeElement , { key : 'Enter' } ) ;
588
+
589
+ expect ( radios [ 0 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
590
+ expect ( radios [ 1 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
591
+ expect ( radios [ 2 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
592
+ expect ( radios [ 3 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
593
+ } ) ;
594
+
595
+ it ( 'RadioGroup roving tabIndex for autoFocus' , async ( ) => {
596
+ let { getAllByRole} = renderRadioGroup ( RadioGroup , Radio , { } , [ { } , { autoFocus : true } , { } ] ) ;
597
+ let radios = getAllByRole ( 'radio' ) ;
598
+ act ( ( ) => { jest . runAllTimers ( ) ; } ) ;
599
+ expect ( radios [ 0 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
600
+ expect ( radios [ 1 ] ) . toHaveAttribute ( 'tabIndex' , '0' ) ;
601
+ expect ( radios [ 2 ] ) . toHaveAttribute ( 'tabIndex' , '-1' ) ;
602
+ } ) ;
546
603
} ) ;
547
604
548
605
it . each `
0 commit comments