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,24 @@ 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 ) ;
6264
63- // move
64- const moveEvent = new Event ( 'touchmove' ) ;
65- moveEvent . touches = [ { pageY : 90 } ] ;
66- getElement ( ) . dispatchEvent ( moveEvent ) ;
65+ // move
66+ const moveEvent = new Event ( 'touchmove' ) ;
67+ moveEvent . touches = [ { pageY : 90 } ] ;
68+ getElement ( ) . dispatchEvent ( moveEvent ) ;
6769
68- // end
69- const endEvent = new Event ( 'touchend' ) ;
70- getElement ( ) . dispatchEvent ( endEvent ) ;
70+ // end
71+ const endEvent = new Event ( 'touchend' ) ;
72+ getElement ( ) . dispatchEvent ( endEvent ) ;
7173
72- // smooth
73- jest . runAllTimers ( ) ;
74+ // smooth
75+ jest . runAllTimers ( ) ;
76+ } ) ;
7477 expect ( wrapper . find ( 'ul' ) . instance ( ) . scrollTop > 10 ) . toBeTruthy ( ) ;
7578
7679 wrapper . unmount ( ) ;
@@ -83,35 +86,39 @@ describe('List.Touch', () => {
8386 return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
8487 }
8588
86- // start
87- const touchEvent = new Event ( 'touchstart' ) ;
88- touchEvent . touches = [ { pageY : 500 } ] ;
89- getElement ( ) . dispatchEvent ( touchEvent ) ;
89+ act ( ( ) => {
90+ // start
91+ const touchEvent = new Event ( 'touchstart' ) ;
92+ touchEvent . touches = [ { pageY : 500 } ] ;
93+ getElement ( ) . dispatchEvent ( touchEvent ) ;
9094
91- // move
92- const preventDefault = jest . fn ( ) ;
93- const moveEvent = new Event ( 'touchmove' ) ;
94- moveEvent . touches = [ { pageY : 0 } ] ;
95- moveEvent . preventDefault = preventDefault ;
96- getElement ( ) . dispatchEvent ( moveEvent ) ;
95+ // move
96+ const preventDefault = jest . fn ( ) ;
97+ const moveEvent = new Event ( 'touchmove' ) ;
98+ moveEvent . touches = [ { pageY : 0 } ] ;
99+ moveEvent . preventDefault = preventDefault ;
100+ getElement ( ) . dispatchEvent ( moveEvent ) ;
101+ } ) ;
97102
98103 // Call preventDefault
99104 expect ( preventDefault ) . toHaveBeenCalled ( ) ;
100105
101- // ======= Not call since scroll to the bottom =======
102- jest . runAllTimers ( ) ;
103- preventDefault . mockReset ( ) ;
106+ act ( ( ) => {
107+ // ======= Not call since scroll to the bottom =======
108+ jest . runAllTimers ( ) ;
109+ preventDefault . mockReset ( ) ;
104110
105- // start
106- const touchEvent2 = new Event ( 'touchstart' ) ;
107- touchEvent2 . touches = [ { pageY : 500 } ] ;
108- getElement ( ) . dispatchEvent ( touchEvent2 ) ;
111+ // start
112+ const touchEvent2 = new Event ( 'touchstart' ) ;
113+ touchEvent2 . touches = [ { pageY : 500 } ] ;
114+ getElement ( ) . dispatchEvent ( touchEvent2 ) ;
109115
110- // move
111- const moveEvent2 = new Event ( 'touchmove' ) ;
112- moveEvent2 . touches = [ { pageY : 0 } ] ;
113- moveEvent2 . preventDefault = preventDefault ;
114- getElement ( ) . dispatchEvent ( moveEvent2 ) ;
116+ // move
117+ const moveEvent2 = new Event ( 'touchmove' ) ;
118+ moveEvent2 . touches = [ { pageY : 0 } ] ;
119+ moveEvent2 . preventDefault = preventDefault ;
120+ getElement ( ) . dispatchEvent ( moveEvent2 ) ;
121+ } ) ;
115122
116123 expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
117124 } ) ;
@@ -121,12 +128,11 @@ describe('List.Touch', () => {
121128 const preventDefault = jest . fn ( ) ;
122129 const wrapper = genList ( { itemHeight : 20 , height : 100 , data : genData ( 100 ) } ) ;
123130
124- const touchEvent = new Event ( 'touchstart' ) ;
125- touchEvent . preventDefault = preventDefault ;
126- wrapper
127- . find ( '.rc-virtual-list-scrollbar' )
128- . instance ( )
129- . dispatchEvent ( touchEvent ) ;
131+ act ( ( ) => {
132+ const touchEvent = new Event ( 'touchstart' ) ;
133+ touchEvent . preventDefault = preventDefault ;
134+ wrapper . find ( '.rc-virtual-list-scrollbar' ) . instance ( ) . dispatchEvent ( touchEvent ) ;
135+ } ) ;
130136
131137 expect ( preventDefault ) . toHaveBeenCalled ( ) ;
132138 } ) ;
0 commit comments