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,24 @@ 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 ) ;
62
64
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 ) ;
67
69
68
- // end
69
- const endEvent = new Event ( 'touchend' ) ;
70
- getElement ( ) . dispatchEvent ( endEvent ) ;
70
+ // end
71
+ const endEvent = new Event ( 'touchend' ) ;
72
+ getElement ( ) . dispatchEvent ( endEvent ) ;
71
73
72
- // smooth
73
- jest . runAllTimers ( ) ;
74
+ // smooth
75
+ jest . runAllTimers ( ) ;
76
+ } ) ;
74
77
expect ( wrapper . find ( 'ul' ) . instance ( ) . scrollTop > 10 ) . toBeTruthy ( ) ;
75
78
76
79
wrapper . unmount ( ) ;
@@ -83,35 +86,39 @@ describe('List.Touch', () => {
83
86
return wrapper . find ( '.rc-virtual-list-holder' ) . instance ( ) ;
84
87
}
85
88
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 ) ;
90
94
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
+ } ) ;
97
102
98
103
// Call preventDefault
99
104
expect ( preventDefault ) . toHaveBeenCalled ( ) ;
100
105
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 ( ) ;
104
110
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 ) ;
109
115
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
+ } ) ;
115
122
116
123
expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
117
124
} ) ;
@@ -121,12 +128,11 @@ describe('List.Touch', () => {
121
128
const preventDefault = jest . fn ( ) ;
122
129
const wrapper = genList ( { itemHeight : 20 , height : 100 , data : genData ( 100 ) } ) ;
123
130
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
+ } ) ;
130
136
131
137
expect ( preventDefault ) . toHaveBeenCalled ( ) ;
132
138
} ) ;
0 commit comments