11import React from "react" ;
2- import { render , cleanup } from "react-testing-library" ;
2+ import { cleanup , render , wait } from "react-testing-library" ;
33import "jest-dom/extend-expect" ;
44import userEvent from "../src" ;
55
@@ -9,15 +9,59 @@ describe("userEvent.type", () => {
99 it . each ( [ "input" , "textarea" ] ) ( "should type text in <%s>" , type => {
1010 const onChange = jest . fn ( ) ;
1111 const { getByTestId } = render (
12- React . createElement ( type , { "data-testid" : "input" , onChange : onChange } )
12+ React . createElement ( type , {
13+ "data-testid" : "input" ,
14+ onChange : onChange
15+ } )
1316 ) ;
1417 const text = "Hello, world!" ;
1518 userEvent . type ( getByTestId ( "input" ) , text ) ;
16-
1719 expect ( onChange ) . toHaveBeenCalledTimes ( text . length ) ;
1820 expect ( getByTestId ( "input" ) ) . toHaveProperty ( "value" , text ) ;
1921 } ) ;
2022
23+ it ( "should not type when event.preventDefault() is called" , ( ) => {
24+ const onChange = jest . fn ( ) ;
25+ const onKeydown = jest
26+ . fn ( )
27+ . mockImplementation ( event => event . preventDefault ( ) ) ;
28+ const { getByTestId } = render (
29+ < input data-testid = "input" onKeyDown = { onKeydown } onChange = { onChange } />
30+ ) ;
31+ const text = "Hello, world!" ;
32+ userEvent . type ( getByTestId ( "input" ) , text ) ;
33+ expect ( onKeydown ) . toHaveBeenCalledTimes ( text . length ) ;
34+ expect ( onChange ) . toHaveBeenCalledTimes ( 0 ) ;
35+ expect ( getByTestId ( "input" ) ) . not . toHaveProperty ( "value" , text ) ;
36+ } ) ;
37+
38+ it ( "should delayed the typing when opts.dealy is not 0" , async ( ) => {
39+ jest . useFakeTimers ( ) ;
40+ const onChange = jest . fn ( ) ;
41+ const { getByTestId } = render (
42+ React . createElement ( "input" , {
43+ "data-testid" : "input" ,
44+ onChange : onChange
45+ } )
46+ ) ;
47+ const text = "Hello, world!" ;
48+ const delay = 10 ;
49+ userEvent . type ( getByTestId ( "input" ) , text , {
50+ delay
51+ } ) ;
52+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
53+ expect ( getByTestId ( "input" ) ) . not . toHaveProperty ( "value" , text ) ;
54+
55+ for ( let i = 0 ; i < text . length ; i ++ ) {
56+ jest . advanceTimersByTime ( delay ) ;
57+ await wait ( ( ) => expect ( onChange ) . toHaveBeenCalledTimes ( i + 1 ) ) ;
58+ expect ( getByTestId ( "input" ) ) . toHaveProperty (
59+ "value" ,
60+ text . slice ( 0 , i + 1 )
61+ ) ;
62+ }
63+ } ) ;
64+
2165 it . each ( [ "input" , "textarea" ] ) (
2266 "should type text in <%s> all at once" ,
2367 type => {
@@ -29,7 +73,9 @@ describe("userEvent.type", () => {
2973 } )
3074 ) ;
3175 const text = "Hello, world!" ;
32- userEvent . type ( getByTestId ( "input" ) , text , { allAtOnce : true } ) ;
76+ 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