1
1
import React from 'react' ;
2
+ import { act } from 'react-dom/test-utils' ;
2
3
import { mount } from 'enzyme' ;
3
4
import { spyElementPrototypes } from './utils/domHook' ;
4
5
import List from '../src' ;
@@ -55,22 +56,25 @@ describe('List.Touch', () => {
55
56
return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
56
57
}
57
58
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 ) ;
62
69
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 ) ;
67
73
68
- // end
69
- const endEvent = new Event ( 'touchend' ) ;
70
- getElement ( ) . dispatchEvent ( endEvent ) ;
74
+ // smooth
75
+ jest . runAllTimers ( ) ;
76
+ } ) ;
71
77
72
- // smooth
73
- jest . runAllTimers ( ) ;
74
78
expect ( wrapper . find ( 'ul' ) . instance ( ) . scrollTop > 10 ) . toBeTruthy ( ) ;
75
79
76
80
wrapper . unmount ( ) ;
@@ -83,35 +87,40 @@ describe('List.Touch', () => {
83
87
return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
84
88
}
85
89
86
- // start
87
- const touchEvent = new Event ( 'touchstart' ) ;
88
- touchEvent . touches = [ { pageY : 500 } ] ;
89
- getElement ( ) . dispatchEvent ( touchEvent ) ;
90
-
91
- // move
92
90
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
+ } ) ;
97
104
98
105
// Call preventDefault
99
106
expect ( preventDefault ) . toHaveBeenCalled ( ) ;
100
107
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 ( ) ;
104
112
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 ) ;
109
117
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
+ } ) ;
115
124
116
125
expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
117
126
} ) ;
@@ -121,12 +130,11 @@ describe('List.Touch', () => {
121
130
const preventDefault = jest . fn ( ) ;
122
131
const wrapper = genList ( { itemHeight : 20 , height : 100 , data : genData ( 100 ) } ) ;
123
132
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
+ } ) ;
130
138
131
139
expect ( preventDefault ) . toHaveBeenCalled ( ) ;
132
140
} ) ;
0 commit comments