File tree 3 files changed +28
-0
lines changed
3 files changed +28
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+ import { render , cleanup } from "react-testing-library" ;
3
+ import "jest-dom/extend-expect" ;
4
+ import userEvent from "../src" ;
5
+
6
+ afterEach ( cleanup ) ;
7
+
8
+ describe ( "userEvent.type" , ( ) => {
9
+ it . each ( [ "input" , "textarea" ] ) ( "should type text in <%s>" , type => {
10
+ const onChange = jest . fn ( ) ;
11
+ const { getByTestId } = render (
12
+ React . createElement ( type , { "data-testid" : "input" , onChange : onChange } )
13
+ ) ;
14
+ const text = "Hello, world!" ;
15
+ userEvent . type ( getByTestId ( "input" ) , text ) ;
16
+
17
+ expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
18
+ expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
19
+ } ) ;
20
+ } ) ;
Original file line number Diff line number Diff line change @@ -71,6 +71,14 @@ const userEvent = {
71
71
}
72
72
73
73
wasAnotherElementFocused && focusedElement . blur ( ) ;
74
+ } ,
75
+ type ( element , text ) {
76
+ this . click ( element ) ;
77
+ text
78
+ . split ( "" )
79
+ . forEach ( ( _ , i ) =>
80
+ fireEvent . change ( element , { target : { value : text . slice ( 0 , i + 1 ) } } )
81
+ ) ;
74
82
}
75
83
} ;
76
84
You can’t perform that action at this time.
0 commit comments