File tree 2 files changed +31
-6
lines changed
2 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -17,4 +17,22 @@ describe("userEvent.type", () => {
17
17
expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
18
18
expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
19
19
} ) ;
20
+
21
+ it . each ( [ "input" , "textarea" ] ) (
22
+ "should type text in <%s> all at once" ,
23
+ type => {
24
+ const onChange = jest . fn ( ) ;
25
+ const { getByTestId } = render (
26
+ React . createElement ( type , {
27
+ "data-testid" : "input" ,
28
+ onChange : onChange
29
+ } )
30
+ ) ;
31
+ const text = "Hello, world!" ;
32
+ userEvent . type ( getByTestId ( "input" ) , text , { allAtOnce : true } ) ;
33
+
34
+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
35
+ expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
36
+ }
37
+ ) ;
20
38
} ) ;
Original file line number Diff line number Diff line change @@ -72,13 +72,20 @@ const userEvent = {
72
72
73
73
wasAnotherElementFocused && focusedElement . blur ( ) ;
74
74
} ,
75
- type ( element , text ) {
75
+ type ( element , text , userOpts = { } ) {
76
+ const defaultOpts = { allAtOnce : false } ;
77
+ const opts = Object . assign ( defaultOpts , userOpts ) ;
78
+
76
79
this . click ( element ) ;
77
- text
78
- . split ( "" )
79
- . forEach ( ( _ , i ) =>
80
- fireEvent . change ( element , { target : { value : text . slice ( 0 , i + 1 ) } } )
81
- ) ;
80
+ if ( opts . allAtOnce ) {
81
+ fireEvent . change ( element , { target : { value : text } } ) ;
82
+ } else {
83
+ text
84
+ . split ( "" )
85
+ . forEach ( ( _ , i ) =>
86
+ fireEvent . change ( element , { target : { value : text . slice ( 0 , i + 1 ) } } )
87
+ ) ;
88
+ }
82
89
}
83
90
} ;
84
91
You can’t perform that action at this time.
0 commit comments