@@ -4,280 +4,175 @@ import { shallow } from 'enzyme';
4
4
5
5
import ScrollLock from '../src/ScrollLock' ;
6
6
7
+ const scrollLockInstance = props =>
8
+ shallow ( < ScrollLock { ...props } > < div /> </ ScrollLock > ) . instance ( ) ;
9
+
7
10
describe ( 'ScrollLock' , ( ) => {
11
+ afterEach ( ( ) => {
12
+ jest . resetAllMocks ( ) ;
13
+ } ) ;
8
14
describe ( 'component lifecycle methods' , ( ) => {
9
- afterEach ( ( ) => {
10
- jest . resetAllMocks ( ) ;
11
- } ) ;
12
15
it ( 'componentDidMount - enabled' , ( ) => {
13
- const component = shallow (
14
- < ScrollLock >
15
- < div />
16
- </ ScrollLock >
17
- ) . instance ( ) ;
16
+ const component = scrollLockInstance ( ) ;
18
17
component . listenToScrollEvents = jest . fn ( ) ;
19
-
20
18
component . componentDidMount ( ) ;
21
-
22
19
expect ( component . listenToScrollEvents ) . toBeCalled ( ) ;
23
20
} ) ;
24
21
it ( 'componentDidMount - enabled=false' , ( ) => {
25
- const component = shallow (
26
- < ScrollLock enabled = { false } >
27
- < div />
28
- </ ScrollLock >
29
- ) . instance ( ) ;
22
+ const component = scrollLockInstance ( { enabled : false } ) ;
30
23
component . listenToScrollEvents = jest . fn ( ) ;
31
-
32
24
component . componentDidMount ( ) ;
33
-
34
25
expect ( component . listenToScrollEvents ) . toHaveBeenCalledTimes ( 0 ) ;
35
26
} ) ;
36
27
it ( 'componentWillUnmount' , ( ) => {
37
- const component = shallow (
38
- < ScrollLock enabled = { false } >
39
- < div />
40
- </ ScrollLock >
41
- ) . instance ( ) ;
28
+ const component = scrollLockInstance ( ) ;
42
29
component . stopListeningToScrollEvents = jest . fn ( ) ;
43
-
44
30
component . componentWillUnmount ( ) ;
45
-
46
31
expect ( component . stopListeningToScrollEvents ) . toBeCalled ( ) ;
47
32
} ) ;
48
33
it ( 'componentWillReceiveProps - disabled to enabled' , ( ) => {
49
- const component = shallow (
50
- < ScrollLock enabled = { false } >
51
- < div />
52
- </ ScrollLock >
53
- ) . instance ( ) ;
34
+ const component = scrollLockInstance ( { enabled : false } ) ;
54
35
component . listenToScrollEvents = jest . fn ( ) ;
55
-
56
36
component . componentWillReceiveProps ( { enabled : true } ) ;
57
-
58
37
expect ( component . listenToScrollEvents ) . toBeCalled ( ) ;
59
38
} ) ;
60
39
it ( 'componentWillReceiveProps - enabled to disabled' , ( ) => {
61
- const component = shallow (
62
- < ScrollLock >
63
- < div />
64
- </ ScrollLock >
65
- ) . instance ( ) ;
40
+ const component = scrollLockInstance ( ) ;
66
41
component . stopListeningToScrollEvents = jest . fn ( ) ;
67
-
68
42
component . componentWillReceiveProps ( { enabled : false } ) ;
69
-
70
43
expect ( component . stopListeningToScrollEvents ) . toBeCalled ( ) ;
71
44
} ) ;
72
45
} ) ;
73
-
74
46
describe ( 'component methods' , ( ) => {
75
47
describe ( 'setScrollingElement' , ( ) => {
76
48
it ( 'should set the scrolling element to firstChild' , ( ) => {
77
- const component = shallow (
78
- < ScrollLock >
79
- < div />
80
- </ ScrollLock >
81
- ) . instance ( ) ;
82
-
49
+ const component = scrollLockInstance ( ) ;
83
50
assert . equal ( component . scrollingElement , undefined ) ;
84
-
85
51
const firstChild = < div > BLAH BLAH TEST BLAH</ div > ;
86
52
component . setScrollingElement ( { firstChild } ) ;
87
-
88
53
assert . equal ( component . scrollingElement , firstChild ) ;
89
54
} ) ;
90
55
it ( 'should set the scrolling element to undefined if no argument passed' , ( ) => {
91
- const component = shallow (
92
- < ScrollLock >
93
- < div />
94
- </ ScrollLock >
95
- ) . instance ( ) ;
96
-
56
+ const component = scrollLockInstance ( ) ;
97
57
component . setScrollingElement ( ) ;
98
-
99
58
assert . equal ( component . scrollingElement , undefined ) ;
100
59
} ) ;
101
60
} ) ;
102
-
103
61
describe ( 'handleEventDelta' , ( ) => {
104
- afterEach ( ( ) => {
105
- jest . resetAllMocks ( ) ;
106
- } ) ;
107
62
it ( 'should cancel scroll event if delta beaks lower limit' , ( ) => {
108
- const component = shallow (
109
- < ScrollLock >
110
- < div />
111
- </ ScrollLock >
112
- ) . instance ( ) ;
63
+ const component = scrollLockInstance ( ) ;
113
64
component . cancelScrollEvent = jest . fn ( ) ;
114
65
component . scrollingElement = {
115
66
scrollTop : 50 ,
116
67
scrollHeight : 450 ,
117
68
clientHeight : 400
118
69
} ;
119
-
120
70
component . handleEventDelta ( { } , - 60 ) ;
121
-
122
- // scroll top should be 0
123
71
assert . equal ( component . scrollingElement . scrollTop , 0 ) ;
124
72
expect ( component . cancelScrollEvent ) . toBeCalled ( ) ;
125
73
} ) ;
126
74
it ( 'should cancel scroll event if delta breaks upper limit' , ( ) => {
127
75
const scrollHeight = 450 ;
128
- const component = shallow (
129
- < ScrollLock >
130
- < div />
131
- </ ScrollLock >
132
- ) . instance ( ) ;
76
+ const component = scrollLockInstance ( ) ;
133
77
component . cancelScrollEvent = jest . fn ( ) ;
134
78
component . scrollingElement = {
135
79
scrollTop : 400 ,
136
80
scrollHeight,
137
81
clientHeight : 400
138
82
} ;
139
-
140
83
component . handleEventDelta ( { } , 60 ) ;
141
-
142
- // scroll top should be scrollHeight of scrollingElement
143
84
assert . equal ( component . scrollingElement . scrollTop , scrollHeight ) ;
144
85
expect ( component . cancelScrollEvent ) . toBeCalled ( ) ;
145
86
} ) ;
146
87
} ) ;
147
-
148
88
describe ( 'onWheelHandler' , ( ) => {
149
89
it ( 'should call handleEventDelta with correct args' , ( ) => {
150
90
const synthEvent = {
151
91
deltaY : 60
152
92
} ;
153
- const component = shallow (
154
- < ScrollLock >
155
- < div />
156
- </ ScrollLock >
157
- ) . instance ( ) ;
93
+ const component = scrollLockInstance ( ) ;
158
94
component . handleEventDelta = jest . fn ( ) ;
159
-
160
95
component . onWheelHandler ( synthEvent ) ;
161
-
162
96
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , synthEvent . deltaY ) ;
163
97
jest . resetAllMocks ( ) ;
164
98
} ) ;
165
99
} ) ;
166
-
167
100
describe ( 'onTouchStartHandler' , ( ) => {
168
101
it ( 'should set this.touchStart' , ( ) => {
169
102
const touchClientY = 50 ;
170
- const component = shallow (
171
- < ScrollLock >
172
- < div />
173
- </ ScrollLock >
174
- ) . instance ( ) ;
103
+ const component = scrollLockInstance ( ) ;
175
104
component . onTouchStartHandler ( {
176
105
changedTouches : [ { clientY : touchClientY } ]
177
106
} ) ;
178
107
assert . equal ( component . touchStart , touchClientY ) ;
179
108
} ) ;
180
109
} ) ;
181
-
182
110
describe ( 'onTouchMoveHandler' , ( ) => {
183
111
it ( 'should call handleEventDelta with correct args' , ( ) => {
184
112
const touchClientY = 70 ;
185
113
const touchStart = 50 ;
186
114
const synthEvent = {
187
115
changedTouches : [ { clientY : touchClientY } ]
188
116
} ;
189
- const component = shallow (
190
- < ScrollLock >
191
- < div />
192
- </ ScrollLock >
193
- ) . instance ( ) ;
117
+ const component = scrollLockInstance ( ) ;
194
118
component . handleEventDelta = jest . fn ( ) ;
195
119
component . touchStart = touchStart ;
196
-
197
120
component . onTouchMoveHandler ( synthEvent ) ;
198
-
199
121
expect ( component . handleEventDelta )
200
122
. toBeCalledWith ( synthEvent , touchStart - touchClientY ) ;
201
123
} ) ;
202
124
} ) ;
203
125
describe ( 'onKeyDownHandler' , ( ) => {
204
126
let component ;
205
- let synthEvent ;
206
127
beforeEach ( ( ) => {
207
- component = shallow (
208
- < ScrollLock >
209
- < div />
210
- </ ScrollLock >
211
- ) . instance ( ) ;
128
+ component = scrollLockInstance ( ) ;
212
129
component . handleEventDelta = jest . fn ( ) ;
213
130
} ) ;
214
- afterEach ( ( ) => {
215
- jest . resetAllMocks ( ) ;
216
- } ) ;
217
131
it ( 'should call handleEventDelta with delta of 1 for space bar' , ( ) => {
218
- synthEvent = { keyCode : 32 } ;
132
+ const synthEvent = { keyCode : 32 } ;
219
133
component . onKeyDownHandler ( synthEvent ) ;
220
134
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
221
135
} ) ;
222
136
it ( 'should call handleEventDelta with delta of 1 for pageDown' , ( ) => {
223
- synthEvent = { keyCode : 34 } ;
137
+ const synthEvent = { keyCode : 34 } ;
224
138
component . onKeyDownHandler ( synthEvent ) ;
225
139
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
226
140
} ) ;
227
141
it ( 'should call handleEventDelta with delta of 1 for downArrow' , ( ) => {
228
- synthEvent = { keyCode : 40 } ;
142
+ const synthEvent = { keyCode : 40 } ;
229
143
component . onKeyDownHandler ( synthEvent ) ;
230
144
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
231
145
} ) ;
232
146
it ( 'should call handleEventDelta with delta of -1 for pageUp' , ( ) => {
233
- synthEvent = { keyCode : 33 } ;
147
+ const synthEvent = { keyCode : 33 } ;
234
148
component . onKeyDownHandler ( synthEvent ) ;
235
149
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , - 1 ) ;
236
150
} ) ;
237
151
it ( 'should call handleEventDelta with delta of -1 for arrowUp' , ( ) => {
238
- synthEvent = { keyCode : 38 } ;
152
+ const synthEvent = { keyCode : 38 } ;
239
153
component . onKeyDownHandler ( synthEvent ) ;
240
154
expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , - 1 ) ;
241
155
} ) ;
242
156
} ) ;
243
-
244
157
describe ( 'cancelScrollEvent component method' , ( ) => {
245
158
it ( 'should cancel scroll event' , ( ) => {
246
159
const synthEvent = {
247
160
stopImmediatePropagation : jest . fn ( ) ,
248
161
preventDefault : jest . fn ( )
249
162
} ;
250
- const component = shallow (
251
- < ScrollLock >
252
- < div />
253
- </ ScrollLock >
254
- ) . instance ( ) ;
255
-
163
+ const component = scrollLockInstance ( ) ;
256
164
component . cancelScrollEvent ( synthEvent ) ;
257
-
258
165
expect ( synthEvent . stopImmediatePropagation ) . toBeCalled ( ) ;
259
166
expect ( synthEvent . preventDefault ) . toBeCalled ( ) ;
260
-
261
- jest . resetAllMocks ( ) ;
262
167
} ) ;
263
168
} ) ;
264
-
265
169
describe ( 'listenToScrollEvents component method' , ( ) => {
266
- afterEach ( ( ) => {
267
- jest . resetAllMocks ( ) ;
268
- } ) ;
269
170
it ( 'should add the proper event listeners' , ( ) => {
270
171
const scrollingElement = {
271
172
addEventListener : jest . fn ( )
272
173
} ;
273
- const component = shallow (
274
- < ScrollLock >
275
- < div />
276
- </ ScrollLock >
277
- ) . instance ( ) ;
278
-
174
+ const component = scrollLockInstance ( ) ;
279
175
component . listenToScrollEvents ( scrollingElement ) ;
280
-
281
176
const expectedCalls = [
282
177
[ 'wheel' , component . onWheelHandler , false ] ,
283
178
[ 'touchstart' , component . onTouchStartHandler , false ] ,
@@ -288,19 +183,11 @@ describe('ScrollLock', () => {
288
183
} ) ;
289
184
} ) ;
290
185
describe ( 'stopListeningToScrollEvents component method' , ( ) => {
291
- afterEach ( ( ) => {
292
- jest . resetAllMocks ( ) ;
293
- } ) ;
294
186
it ( 'should remove the proper event listeners' , ( ) => {
295
187
const scrollingElement = {
296
188
removeEventListener : jest . fn ( )
297
189
} ;
298
- const component = shallow (
299
- < ScrollLock >
300
- < div />
301
- </ ScrollLock >
302
- ) . instance ( ) ;
303
-
190
+ const component = scrollLockInstance ( ) ;
304
191
component . stopListeningToScrollEvents ( scrollingElement ) ;
305
192
const expectedCalls = [
306
193
[ 'wheel' , component . onWheelHandler , false ] ,
0 commit comments