@@ -29,10 +29,10 @@ describe('<Autocomplete />', () => {
29
29
30
30
describe ( 'combobox' , ( ) => {
31
31
it ( 'should clear the input when blur' , ( ) => {
32
- const { container } = render (
32
+ const { getByRole } = render (
33
33
< Autocomplete renderInput = { params => < TextField { ...params } /> } /> ,
34
34
) ;
35
- const input = container . querySelector ( 'input ') ;
35
+ const input = getByRole ( 'textbox ') ;
36
36
input . focus ( ) ;
37
37
fireEvent . change ( document . activeElement , { target : { value : 'a' } } ) ;
38
38
expect ( input . value ) . to . equal ( 'a' ) ;
@@ -49,12 +49,51 @@ describe('<Autocomplete />', () => {
49
49
} ) ;
50
50
} ) ;
51
51
52
- describe ( 'multiple' , ( ) => {
52
+ describe ( 'prop: autoSelect' , ( ) => {
53
+ it ( 'should add new value when autoSelect & multiple on blur' , ( ) => {
54
+ const handleChange = spy ( ) ;
55
+ const options = [ 'one' , 'two' ] ;
56
+ render (
57
+ < Autocomplete
58
+ autoSelect
59
+ multiple
60
+ value = { [ options [ 0 ] ] }
61
+ options = { options }
62
+ onChange = { handleChange }
63
+ renderInput = { params => < TextField autoFocus { ...params } /> }
64
+ /> ,
65
+ ) ;
66
+ fireEvent . change ( document . activeElement , { target : { value : 't' } } ) ;
67
+ fireEvent . keyDown ( document . activeElement , { key : 'ArrowDown' } ) ;
68
+ document . activeElement . blur ( ) ;
69
+ expect ( handleChange . callCount ) . to . equal ( 1 ) ;
70
+ expect ( handleChange . args [ 0 ] [ 1 ] ) . to . deep . equal ( options ) ;
71
+ } ) ;
72
+
73
+ it ( 'should add new value when autoSelect & multiple & freeSolo on blur' , ( ) => {
74
+ const handleChange = spy ( ) ;
75
+ render (
76
+ < Autocomplete
77
+ autoSelect
78
+ freeSolo
79
+ multiple
80
+ onChange = { handleChange }
81
+ renderInput = { params => < TextField autoFocus { ...params } /> }
82
+ /> ,
83
+ ) ;
84
+ fireEvent . change ( document . activeElement , { target : { value : 'a' } } ) ;
85
+ document . activeElement . blur ( ) ;
86
+ expect ( handleChange . callCount ) . to . equal ( 1 ) ;
87
+ expect ( handleChange . args [ 0 ] [ 1 ] ) . to . deep . equal ( [ 'a' ] ) ;
88
+ } ) ;
89
+ } ) ;
90
+
91
+ describe ( 'prop: multiple' , ( ) => {
53
92
it ( 'should not crash' , ( ) => {
54
- const { container } = render (
93
+ const { getByRole } = render (
55
94
< Autocomplete renderInput = { params => < TextField { ...params } /> } multiple /> ,
56
95
) ;
57
- const input = container . querySelector ( 'input ') ;
96
+ const input = getByRole ( 'textbox ') ;
58
97
input . focus ( ) ;
59
98
document . activeElement . blur ( ) ;
60
99
input . focus ( ) ;
@@ -98,74 +137,6 @@ describe('<Autocomplete />', () => {
98
137
expect ( handleChange . args [ 0 ] [ 1 ] ) . to . deep . equal ( [ options [ 1 ] ] ) ;
99
138
expect ( document . activeElement ) . to . equal ( getByRole ( 'textbox' ) ) ;
100
139
} ) ;
101
-
102
- it ( 'should add new value when freeSolo & multiple on blur' , ( ) => {
103
- const handleChange = spy ( ) ;
104
- const { container } = render (
105
- < Autocomplete
106
- freeSolo
107
- multiple
108
- onChange = { handleChange }
109
- renderInput = { params => < TextField { ...params } /> }
110
- /> ,
111
- ) ;
112
- const input = container . querySelector ( 'input' ) ;
113
- input . focus ( ) ;
114
- fireEvent . change ( document . activeElement , { target : { value : 'a' } } ) ;
115
- document . activeElement . blur ( ) ;
116
- expect ( handleChange . callCount ) . to . equal ( 1 ) ;
117
- expect ( handleChange . args [ 0 ] [ 1 ] ) . to . deep . equal ( [ 'a' ] ) ;
118
- } ) ;
119
-
120
- it ( 'should not add new value on blur if value is empty' , ( ) => {
121
- const handleChange = spy ( ) ;
122
- const { container } = render (
123
- < Autocomplete
124
- freeSolo
125
- multiple
126
- onChange = { handleChange }
127
- renderInput = { params => < TextField { ...params } /> }
128
- /> ,
129
- ) ;
130
- const input = container . querySelector ( 'input' ) ;
131
- input . focus ( ) ;
132
- fireEvent . change ( document . activeElement , { target : { value : '' } } ) ;
133
- document . activeElement . blur ( ) ;
134
- expect ( handleChange . callCount ) . to . equal ( 0 ) ;
135
- } ) ;
136
-
137
- it ( 'should not add new value on blur without freeSolo' , ( ) => {
138
- const handleChange = spy ( ) ;
139
- const { container } = render (
140
- < Autocomplete
141
- multiple
142
- onChange = { handleChange }
143
- renderInput = { params => < TextField { ...params } /> }
144
- /> ,
145
- ) ;
146
- const input = container . querySelector ( 'input' ) ;
147
- input . focus ( ) ;
148
- fireEvent . change ( document . activeElement , { target : { value : 'a' } } ) ;
149
- document . activeElement . blur ( ) ;
150
- expect ( handleChange . callCount ) . to . equal ( 0 ) ;
151
- } ) ;
152
-
153
- it ( 'should not add new value on blur without multiple' , ( ) => {
154
- const handleChange = spy ( ) ;
155
- const { container } = render (
156
- < Autocomplete
157
- freeSolo
158
- onChange = { handleChange }
159
- renderInput = { params => < TextField { ...params } /> }
160
- /> ,
161
- ) ;
162
- const input = container . querySelector ( 'input' ) ;
163
- input . focus ( ) ;
164
- fireEvent . change ( document . activeElement , { target : { value : 'a' } } ) ;
165
- document . activeElement . blur ( ) ;
166
- expect ( input . value ) . to . equal ( 'a' ) ;
167
- expect ( handleChange . args [ 0 ] [ 1 ] ) . to . deep . equal ( 'a' ) ;
168
- } ) ;
169
140
} ) ;
170
141
171
142
it ( 'should trigger a form expectedly' , ( ) => {
@@ -592,14 +563,15 @@ describe('<Autocomplete />', () => {
592
563
593
564
describe ( 'prop: disabled' , ( ) => {
594
565
it ( 'should disable the input' , ( ) => {
595
- const { container } = render (
566
+ const { getByRole } = render (
596
567
< Autocomplete
597
568
disabled
598
569
options = { [ 'one' , 'two' , 'three' ] }
599
570
renderInput = { params => < TextField { ...params } /> }
600
571
/> ,
601
572
) ;
602
- expect ( container . querySelector ( 'input' ) . disabled ) . to . be . true ;
573
+ const input = getByRole ( 'textbox' ) ;
574
+ expect ( input . disabled ) . to . be . true ;
603
575
} ) ;
604
576
605
577
it ( 'should disable the popup button' , ( ) => {
@@ -713,14 +685,14 @@ describe('<Autocomplete />', () => {
713
685
714
686
it ( 'should not select undefined ' , ( ) => {
715
687
const handleChange = spy ( ) ;
716
- const { container , getByRole } = render (
688
+ const { getByRole } = render (
717
689
< Autocomplete
718
690
onChange = { handleChange }
719
691
options = { [ 'one' , 'two' ] }
720
692
renderInput = { params => < TextField { ...params } /> }
721
693
/> ,
722
694
) ;
723
- const input = container . querySelector ( 'input ') ;
695
+ const input = getByRole ( 'textbox ') ;
724
696
fireEvent . click ( input ) ;
725
697
726
698
const listbox = getByRole ( 'listbox' ) ;
0 commit comments