1
- import React from "react" ;
2
- import { render , cleanup } from "react-testing-library" ;
3
1
import "jest-dom/extend-expect" ;
2
+
3
+ import { cleanup , render } from "react-testing-library" ;
4
+
5
+ import React from "react" ;
4
6
import userEvent from "../src" ;
5
7
6
8
afterEach ( cleanup ) ;
7
9
8
10
describe ( "userEvent.type" , ( ) => {
9
- it . each ( [ "input" , "textarea" ] ) ( "should type text in <%s>" , type => {
11
+ it . each ( [ "input" , "textarea" ] ) ( "should type text in <%s>" , async type => {
12
+ const onChange = jest . fn ( ) ;
13
+ const { getByTestId } = render (
14
+ React . createElement ( type , {
15
+ "data-testid" : "input" ,
16
+ onChange : onChange
17
+ } )
18
+ ) ;
19
+ const text = "Hello, world!" ;
20
+ await userEvent . type ( getByTestId ( "input" ) , text ) ;
21
+ expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
22
+ expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
23
+ } ) ;
24
+
25
+ it . each ( [ "input" , "textarea" ] ) (
26
+ "should not type <%s> when prevented" ,
27
+ async type => {
28
+ const onChange = jest . fn ( ) ;
29
+ const onKeydown = jest
30
+ . fn ( )
31
+ . mockImplementation ( event => event . preventDefault ( ) ) ;
32
+ const { getByTestId } = render (
33
+ React . createElement ( type , {
34
+ "data-testid" : "input" ,
35
+ onKeyDown : onKeydown ,
36
+ onChange : onChange
37
+ } )
38
+ ) ;
39
+ const text = "Hello, world!" ;
40
+ await userEvent . type ( getByTestId ( "input" ) , text ) ;
41
+ expect ( onKeydown ) . toHaveBeenCalledTimes ( text . length ) ;
42
+ expect ( onChange ) . toHaveBeenCalledTimes ( 0 ) ;
43
+ expect ( getByTestId ( "input" ) ) . not . toHaveProperty ( "value" , text ) ;
44
+ }
45
+ ) ;
46
+
47
+ it ( "test delayed typing of text" , async ( ) => {
10
48
const onChange = jest . fn ( ) ;
11
49
const { getByTestId } = render (
12
- React . createElement ( type , { "data-testid" : "input" , onChange : onChange } )
50
+ React . createElement ( "input" , {
51
+ "data-testid" : "input" ,
52
+ onChange : onChange
53
+ } )
13
54
) ;
14
55
const text = "Hello, world!" ;
15
- userEvent . type ( getByTestId ( "input" ) , text ) ;
56
+ await userEvent . type ( getByTestId ( "input" ) , text , {
57
+ allAtOnce : false ,
58
+ delay : 10
59
+ } ) ;
16
60
17
61
expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
18
62
expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
19
63
} ) ;
20
64
21
65
it . each ( [ "input" , "textarea" ] ) (
22
66
"should type text in <%s> all at once" ,
23
- type => {
67
+ async type => {
24
68
const onChange = jest . fn ( ) ;
25
69
const { getByTestId } = render (
26
70
React . createElement ( type , {
@@ -29,7 +73,9 @@ describe("userEvent.type", () => {
29
73
} )
30
74
) ;
31
75
const text = "Hello, world!" ;
32
- userEvent . type ( getByTestId ( "input" ) , text , { allAtOnce : true } ) ;
76
+ await userEvent . type ( getByTestId ( "input" ) , text , {
77
+ allAtOnce : true
78
+ } ) ;
33
79
34
80
expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
35
81
expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
0 commit comments