1
- import { cleanup , render , screen } from '../../../test' ;
1
+ import { cleanup , render , screen , userEvent } from '../../../test' ;
2
2
import { RadioGroup , RadioItem } from './Radio' ;
3
3
4
+ const label = 'Test item 1' ;
5
+ const value = 'test-item-1' ;
6
+
4
7
describe ( '<Radio />' , ( ) => {
5
8
afterEach ( cleanup ) ;
6
9
7
10
it ( 'renders' , ( ) => {
8
- const label = 'Test item 1' ;
9
- const value = 'test-item-1' ;
10
-
11
11
render (
12
12
< >
13
13
< RadioGroup >
@@ -19,4 +19,57 @@ describe('<Radio />', () => {
19
19
expect ( screen . getByRole ( 'radio' , { name : label } ) ) . toBeVisible ( ) ;
20
20
expect ( screen . getByRole ( 'label' , { name : label } ) ) . toBeVisible ( ) ;
21
21
} ) ;
22
+
23
+ it ( 'passing value controls the input' , async ( ) => {
24
+ const label1 = 'radio-1' ;
25
+ const label2 = 'radio-2' ;
26
+ const value1 = 'value-1' ;
27
+ const value2 = 'value-2' ;
28
+
29
+ render (
30
+ < RadioGroup value = { value2 } >
31
+ < RadioItem label = { label1 } value = { value1 } />
32
+ < RadioItem label = { label2 } value = { value2 } />
33
+ </ RadioGroup > ,
34
+ ) ;
35
+
36
+ const radio1 = screen . getByRole ( 'radio' , { name : label1 } ) ;
37
+ const radio2 = screen . getByRole ( 'radio' , { name : label2 } ) ;
38
+ expect ( radio1 ) . not . toBeChecked ( ) ;
39
+ expect ( radio2 ) . toBeChecked ( ) ;
40
+ } ) ;
41
+
42
+ it ( 'changes selected radio item' , async ( ) => {
43
+ const user = userEvent . setup ( ) ;
44
+ const label1 = 'radio-1' ;
45
+ const label2 = 'radio-2' ;
46
+ const value1 = 'value-1' ;
47
+ const value2 = 'value-2' ;
48
+
49
+ render (
50
+ < RadioGroup >
51
+ < RadioItem label = { label1 } value = { value1 } />
52
+ < RadioItem label = { label2 } value = { value2 } />
53
+ </ RadioGroup > ,
54
+ ) ;
55
+
56
+ const radio1 = screen . getByRole ( 'radio' , { name : label1 } ) ;
57
+ const radio2 = screen . getByRole ( 'radio' , { name : label2 } ) ;
58
+
59
+ await user . click ( radio2 ) ;
60
+
61
+ expect ( radio1 ) . not . toBeChecked ( ) ;
62
+ expect ( radio2 ) . toBeChecked ( ) ;
63
+ } ) ;
64
+
65
+ it ( 'sets the disabled state of the input' , ( ) => {
66
+ render (
67
+ < RadioGroup >
68
+ < RadioItem disabled label = { label } value = { value } />
69
+ </ RadioGroup > ,
70
+ ) ;
71
+ expect ( screen . getByRole ( 'radio' , { name : label } ) ) . toHaveAttribute (
72
+ 'disabled' ,
73
+ ) ;
74
+ } ) ;
22
75
} ) ;
0 commit comments