11import React from 'react' ;
2+ import { act } from 'react-dom/test-utils' ;
23import { mount } from 'enzyme' ;
34import { spyElementPrototypes } from './utils/domHook' ;
45import List from '../src' ;
@@ -55,22 +56,25 @@ describe('List.Touch', () => {
5556 return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
5657 }
5758
58- // start
59- const touchEvent = new Event ( 'touchstart' ) ;
60- touchEvent . touches = [ { pageY : 100 } ] ;
61- getElement ( ) . dispatchEvent ( touchEvent ) ;
59+ act ( ( ) => {
60+ // start
61+ const touchEvent = new Event ( 'touchstart' ) ;
62+ touchEvent . touches = [ { pageY : 100 } ] ;
63+ getElement ( ) . dispatchEvent ( touchEvent ) ;
64+
65+ // move
66+ const moveEvent = new Event ( 'touchmove' ) ;
67+ moveEvent . touches = [ { pageY : 90 } ] ;
68+ getElement ( ) . dispatchEvent ( moveEvent ) ;
6269
63- // move
64- const moveEvent = new Event ( 'touchmove' ) ;
65- moveEvent . touches = [ { pageY : 90 } ] ;
66- getElement ( ) . dispatchEvent ( moveEvent ) ;
70+ // end
71+ const endEvent = new Event ( 'touchend' ) ;
72+ getElement ( ) . dispatchEvent ( endEvent ) ;
6773
68- // end
69- const endEvent = new Event ( 'touchend' ) ;
70- getElement ( ) . dispatchEvent ( endEvent ) ;
74+ // smooth
75+ jest . runAllTimers ( ) ;
76+ } ) ;
7177
72- // smooth
73- jest . runAllTimers ( ) ;
7478 expect ( wrapper . find ( 'ul' ) . instance ( ) . scrollTop > 10 ) . toBeTruthy ( ) ;
7579
7680 wrapper . unmount ( ) ;
@@ -83,35 +87,40 @@ describe('List.Touch', () => {
8387 return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
8488 }
8589
86- // start
87- const touchEvent = new Event ( 'touchstart' ) ;
88- touchEvent . touches = [ { pageY : 500 } ] ;
89- getElement ( ) . dispatchEvent ( touchEvent ) ;
90-
91- // move
9290 const preventDefault = jest . fn ( ) ;
93- const moveEvent = new Event ( 'touchmove' ) ;
94- moveEvent . touches = [ { pageY : 0 } ] ;
95- moveEvent . preventDefault = preventDefault ;
96- getElement ( ) . dispatchEvent ( moveEvent ) ;
91+
92+ act ( ( ) => {
93+ // start
94+ const touchEvent = new Event ( 'touchstart' ) ;
95+ touchEvent . touches = [ { pageY : 500 } ] ;
96+ getElement ( ) . dispatchEvent ( touchEvent ) ;
97+
98+ // move
99+ const moveEvent = new Event ( 'touchmove' ) ;
100+ moveEvent . touches = [ { pageY : 0 } ] ;
101+ moveEvent . preventDefault = preventDefault ;
102+ getElement ( ) . dispatchEvent ( moveEvent ) ;
103+ } ) ;
97104
98105 // Call preventDefault
99106 expect ( preventDefault ) . toHaveBeenCalled ( ) ;
100107
101- // ======= Not call since scroll to the bottom =======
102- jest . runAllTimers ( ) ;
103- preventDefault . mockReset ( ) ;
108+ act ( ( ) => {
109+ // ======= Not call since scroll to the bottom =======
110+ jest . runAllTimers ( ) ;
111+ preventDefault . mockReset ( ) ;
104112
105- // start
106- const touchEvent2 = new Event ( 'touchstart' ) ;
107- touchEvent2 . touches = [ { pageY : 500 } ] ;
108- getElement ( ) . dispatchEvent ( touchEvent2 ) ;
113+ // start
114+ const touchEvent2 = new Event ( 'touchstart' ) ;
115+ touchEvent2 . touches = [ { pageY : 500 } ] ;
116+ getElement ( ) . dispatchEvent ( touchEvent2 ) ;
109117
110- // move
111- const moveEvent2 = new Event ( 'touchmove' ) ;
112- moveEvent2 . touches = [ { pageY : 0 } ] ;
113- moveEvent2 . preventDefault = preventDefault ;
114- getElement ( ) . dispatchEvent ( moveEvent2 ) ;
118+ // move
119+ const moveEvent2 = new Event ( 'touchmove' ) ;
120+ moveEvent2 . touches = [ { pageY : 0 } ] ;
121+ moveEvent2 . preventDefault = preventDefault ;
122+ getElement ( ) . dispatchEvent ( moveEvent2 ) ;
123+ } ) ;
115124
116125 expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
117126 } ) ;
@@ -121,12 +130,11 @@ describe('List.Touch', () => {
121130 const preventDefault = jest . fn ( ) ;
122131 const wrapper = genList ( { itemHeight : 20 , height : 100 , data : genData ( 100 ) } ) ;
123132
124- const touchEvent = new Event ( 'touchstart' ) ;
125- touchEvent . preventDefault = preventDefault ;
126- wrapper
127- . find ( '.rc-virtual-list-scrollbar' )
128- . instance ( )
129- . dispatchEvent ( touchEvent ) ;
133+ act ( ( ) => {
134+ const touchEvent = new Event ( 'touchstart' ) ;
135+ touchEvent . preventDefault = preventDefault ;
136+ wrapper . find ( '.rc-virtual-list-scrollbar' ) . instance ( ) . dispatchEvent ( touchEvent ) ;
137+ } ) ;
130138
131139 expect ( preventDefault ) . toHaveBeenCalled ( ) ;
132140 } ) ;
0 commit comments