@@ -4,280 +4,175 @@ import { shallow } from 'enzyme';
44
55import ScrollLock from '../src/ScrollLock' ;
66
7+ const scrollLockInstance = props =>
8+ shallow ( < ScrollLock { ...props } > < div /> </ ScrollLock > ) . instance ( ) ;
9+
710describe ( 'ScrollLock' , ( ) => {
11+ afterEach ( ( ) => {
12+ jest . resetAllMocks ( ) ;
13+ } ) ;
814 describe ( 'component lifecycle methods' , ( ) => {
9- afterEach ( ( ) => {
10- jest . resetAllMocks ( ) ;
11- } ) ;
1215 it ( 'componentDidMount - enabled' , ( ) => {
13- const component = shallow (
14- < ScrollLock >
15- < div />
16- </ ScrollLock >
17- ) . instance ( ) ;
16+ const component = scrollLockInstance ( ) ;
1817 component . listenToScrollEvents = jest . fn ( ) ;
19-
2018 component . componentDidMount ( ) ;
21-
2219 expect ( component . listenToScrollEvents ) . toBeCalled ( ) ;
2320 } ) ;
2421 it ( 'componentDidMount - enabled=false' , ( ) => {
25- const component = shallow (
26- < ScrollLock enabled = { false } >
27- < div />
28- </ ScrollLock >
29- ) . instance ( ) ;
22+ const component = scrollLockInstance ( { enabled : false } ) ;
3023 component . listenToScrollEvents = jest . fn ( ) ;
31-
3224 component . componentDidMount ( ) ;
33-
3425 expect ( component . listenToScrollEvents ) . toHaveBeenCalledTimes ( 0 ) ;
3526 } ) ;
3627 it ( 'componentWillUnmount' , ( ) => {
37- const component = shallow (
38- < ScrollLock enabled = { false } >
39- < div />
40- </ ScrollLock >
41- ) . instance ( ) ;
28+ const component = scrollLockInstance ( ) ;
4229 component . stopListeningToScrollEvents = jest . fn ( ) ;
43-
4430 component . componentWillUnmount ( ) ;
45-
4631 expect ( component . stopListeningToScrollEvents ) . toBeCalled ( ) ;
4732 } ) ;
4833 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 } ) ;
5435 component . listenToScrollEvents = jest . fn ( ) ;
55-
5636 component . componentWillReceiveProps ( { enabled : true } ) ;
57-
5837 expect ( component . listenToScrollEvents ) . toBeCalled ( ) ;
5938 } ) ;
6039 it ( 'componentWillReceiveProps - enabled to disabled' , ( ) => {
61- const component = shallow (
62- < ScrollLock >
63- < div />
64- </ ScrollLock >
65- ) . instance ( ) ;
40+ const component = scrollLockInstance ( ) ;
6641 component . stopListeningToScrollEvents = jest . fn ( ) ;
67-
6842 component . componentWillReceiveProps ( { enabled : false } ) ;
69-
7043 expect ( component . stopListeningToScrollEvents ) . toBeCalled ( ) ;
7144 } ) ;
7245 } ) ;
73-
7446 describe ( 'component methods' , ( ) => {
7547 describe ( 'setScrollingElement' , ( ) => {
7648 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 ( ) ;
8350 assert . equal ( component . scrollingElement , undefined ) ;
84-
8551 const firstChild = < div > BLAH BLAH TEST BLAH</ div > ;
8652 component . setScrollingElement ( { firstChild } ) ;
87-
8853 assert . equal ( component . scrollingElement , firstChild ) ;
8954 } ) ;
9055 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 ( ) ;
9757 component . setScrollingElement ( ) ;
98-
9958 assert . equal ( component . scrollingElement , undefined ) ;
10059 } ) ;
10160 } ) ;
102-
10361 describe ( 'handleEventDelta' , ( ) => {
104- afterEach ( ( ) => {
105- jest . resetAllMocks ( ) ;
106- } ) ;
10762 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 ( ) ;
11364 component . cancelScrollEvent = jest . fn ( ) ;
11465 component . scrollingElement = {
11566 scrollTop : 50 ,
11667 scrollHeight : 450 ,
11768 clientHeight : 400
11869 } ;
119-
12070 component . handleEventDelta ( { } , - 60 ) ;
121-
122- // scroll top should be 0
12371 assert . equal ( component . scrollingElement . scrollTop , 0 ) ;
12472 expect ( component . cancelScrollEvent ) . toBeCalled ( ) ;
12573 } ) ;
12674 it ( 'should cancel scroll event if delta breaks upper limit' , ( ) => {
12775 const scrollHeight = 450 ;
128- const component = shallow (
129- < ScrollLock >
130- < div />
131- </ ScrollLock >
132- ) . instance ( ) ;
76+ const component = scrollLockInstance ( ) ;
13377 component . cancelScrollEvent = jest . fn ( ) ;
13478 component . scrollingElement = {
13579 scrollTop : 400 ,
13680 scrollHeight,
13781 clientHeight : 400
13882 } ;
139-
14083 component . handleEventDelta ( { } , 60 ) ;
141-
142- // scroll top should be scrollHeight of scrollingElement
14384 assert . equal ( component . scrollingElement . scrollTop , scrollHeight ) ;
14485 expect ( component . cancelScrollEvent ) . toBeCalled ( ) ;
14586 } ) ;
14687 } ) ;
147-
14888 describe ( 'onWheelHandler' , ( ) => {
14989 it ( 'should call handleEventDelta with correct args' , ( ) => {
15090 const synthEvent = {
15191 deltaY : 60
15292 } ;
153- const component = shallow (
154- < ScrollLock >
155- < div />
156- </ ScrollLock >
157- ) . instance ( ) ;
93+ const component = scrollLockInstance ( ) ;
15894 component . handleEventDelta = jest . fn ( ) ;
159-
16095 component . onWheelHandler ( synthEvent ) ;
161-
16296 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , synthEvent . deltaY ) ;
16397 jest . resetAllMocks ( ) ;
16498 } ) ;
16599 } ) ;
166-
167100 describe ( 'onTouchStartHandler' , ( ) => {
168101 it ( 'should set this.touchStart' , ( ) => {
169102 const touchClientY = 50 ;
170- const component = shallow (
171- < ScrollLock >
172- < div />
173- </ ScrollLock >
174- ) . instance ( ) ;
103+ const component = scrollLockInstance ( ) ;
175104 component . onTouchStartHandler ( {
176105 changedTouches : [ { clientY : touchClientY } ]
177106 } ) ;
178107 assert . equal ( component . touchStart , touchClientY ) ;
179108 } ) ;
180109 } ) ;
181-
182110 describe ( 'onTouchMoveHandler' , ( ) => {
183111 it ( 'should call handleEventDelta with correct args' , ( ) => {
184112 const touchClientY = 70 ;
185113 const touchStart = 50 ;
186114 const synthEvent = {
187115 changedTouches : [ { clientY : touchClientY } ]
188116 } ;
189- const component = shallow (
190- < ScrollLock >
191- < div />
192- </ ScrollLock >
193- ) . instance ( ) ;
117+ const component = scrollLockInstance ( ) ;
194118 component . handleEventDelta = jest . fn ( ) ;
195119 component . touchStart = touchStart ;
196-
197120 component . onTouchMoveHandler ( synthEvent ) ;
198-
199121 expect ( component . handleEventDelta )
200122 . toBeCalledWith ( synthEvent , touchStart - touchClientY ) ;
201123 } ) ;
202124 } ) ;
203125 describe ( 'onKeyDownHandler' , ( ) => {
204126 let component ;
205- let synthEvent ;
206127 beforeEach ( ( ) => {
207- component = shallow (
208- < ScrollLock >
209- < div />
210- </ ScrollLock >
211- ) . instance ( ) ;
128+ component = scrollLockInstance ( ) ;
212129 component . handleEventDelta = jest . fn ( ) ;
213130 } ) ;
214- afterEach ( ( ) => {
215- jest . resetAllMocks ( ) ;
216- } ) ;
217131 it ( 'should call handleEventDelta with delta of 1 for space bar' , ( ) => {
218- synthEvent = { keyCode : 32 } ;
132+ const synthEvent = { keyCode : 32 } ;
219133 component . onKeyDownHandler ( synthEvent ) ;
220134 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
221135 } ) ;
222136 it ( 'should call handleEventDelta with delta of 1 for pageDown' , ( ) => {
223- synthEvent = { keyCode : 34 } ;
137+ const synthEvent = { keyCode : 34 } ;
224138 component . onKeyDownHandler ( synthEvent ) ;
225139 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
226140 } ) ;
227141 it ( 'should call handleEventDelta with delta of 1 for downArrow' , ( ) => {
228- synthEvent = { keyCode : 40 } ;
142+ const synthEvent = { keyCode : 40 } ;
229143 component . onKeyDownHandler ( synthEvent ) ;
230144 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , 1 ) ;
231145 } ) ;
232146 it ( 'should call handleEventDelta with delta of -1 for pageUp' , ( ) => {
233- synthEvent = { keyCode : 33 } ;
147+ const synthEvent = { keyCode : 33 } ;
234148 component . onKeyDownHandler ( synthEvent ) ;
235149 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , - 1 ) ;
236150 } ) ;
237151 it ( 'should call handleEventDelta with delta of -1 for arrowUp' , ( ) => {
238- synthEvent = { keyCode : 38 } ;
152+ const synthEvent = { keyCode : 38 } ;
239153 component . onKeyDownHandler ( synthEvent ) ;
240154 expect ( component . handleEventDelta ) . toBeCalledWith ( synthEvent , - 1 ) ;
241155 } ) ;
242156 } ) ;
243-
244157 describe ( 'cancelScrollEvent component method' , ( ) => {
245158 it ( 'should cancel scroll event' , ( ) => {
246159 const synthEvent = {
247160 stopImmediatePropagation : jest . fn ( ) ,
248161 preventDefault : jest . fn ( )
249162 } ;
250- const component = shallow (
251- < ScrollLock >
252- < div />
253- </ ScrollLock >
254- ) . instance ( ) ;
255-
163+ const component = scrollLockInstance ( ) ;
256164 component . cancelScrollEvent ( synthEvent ) ;
257-
258165 expect ( synthEvent . stopImmediatePropagation ) . toBeCalled ( ) ;
259166 expect ( synthEvent . preventDefault ) . toBeCalled ( ) ;
260-
261- jest . resetAllMocks ( ) ;
262167 } ) ;
263168 } ) ;
264-
265169 describe ( 'listenToScrollEvents component method' , ( ) => {
266- afterEach ( ( ) => {
267- jest . resetAllMocks ( ) ;
268- } ) ;
269170 it ( 'should add the proper event listeners' , ( ) => {
270171 const scrollingElement = {
271172 addEventListener : jest . fn ( )
272173 } ;
273- const component = shallow (
274- < ScrollLock >
275- < div />
276- </ ScrollLock >
277- ) . instance ( ) ;
278-
174+ const component = scrollLockInstance ( ) ;
279175 component . listenToScrollEvents ( scrollingElement ) ;
280-
281176 const expectedCalls = [
282177 [ 'wheel' , component . onWheelHandler , false ] ,
283178 [ 'touchstart' , component . onTouchStartHandler , false ] ,
@@ -288,19 +183,11 @@ describe('ScrollLock', () => {
288183 } ) ;
289184 } ) ;
290185 describe ( 'stopListeningToScrollEvents component method' , ( ) => {
291- afterEach ( ( ) => {
292- jest . resetAllMocks ( ) ;
293- } ) ;
294186 it ( 'should remove the proper event listeners' , ( ) => {
295187 const scrollingElement = {
296188 removeEventListener : jest . fn ( )
297189 } ;
298- const component = shallow (
299- < ScrollLock >
300- < div />
301- </ ScrollLock >
302- ) . instance ( ) ;
303-
190+ const component = scrollLockInstance ( ) ;
304191 component . stopListeningToScrollEvents ( scrollingElement ) ;
305192 const expectedCalls = [
306193 [ 'wheel' , component . onWheelHandler , false ] ,
0 commit comments