1- import React from "react" ;
2- import { render , cleanup } from "react-testing-library" ;
31import "jest-dom/extend-expect" ;
2+
3+ import { cleanup , render } from "react-testing-library" ;
4+
5+ import React from "react" ;
46import userEvent from "../src" ;
57
68afterEach ( cleanup ) ;
79
810describe ( "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 ( ) => {
1048 const onChange = jest . fn ( ) ;
1149 const { getByTestId } = render (
12- React . createElement ( type , { "data-testid" : "input" , onChange : onChange } )
50+ React . createElement ( "input" , {
51+ "data-testid" : "input" ,
52+ onChange : onChange
53+ } )
1354 ) ;
1455 const text = "Hello, world!" ;
15- userEvent . type ( getByTestId ( "input" ) , text ) ;
56+ await userEvent . type ( getByTestId ( "input" ) , text , {
57+ allAtOnce : false ,
58+ delay : 10
59+ } ) ;
1660
1761 expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
1862 expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
1963 } ) ;
2064
2165 it . each ( [ "input" , "textarea" ] ) (
2266 "should type text in <%s> all at once" ,
23- type => {
67+ async type => {
2468 const onChange = jest . fn ( ) ;
2569 const { getByTestId } = render (
2670 React . createElement ( type , {
@@ -29,7 +73,9 @@ describe("userEvent.type", () => {
2973 } )
3074 ) ;
3175 const text = "Hello, world!" ;
32- userEvent . type ( getByTestId ( "input" ) , text , { allAtOnce : true } ) ;
76+ await userEvent . type ( getByTestId ( "input" ) , text , {
77+ allAtOnce : true
78+ } ) ;
3379
3480 expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
3581 expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
0 commit comments