@@ -31,10 +31,12 @@ describe('scroll-lock', () => {
31
31
DOM = parseHTMLString ( `
32
32
<div class="container">
33
33
<div class="scrollable">long</div>
34
+ <div ${ SCROLL_LOCK_DISABLE_ATTR } ="true" class="disabled-target"></div>
34
35
</div>
35
36
` ) ;
36
37
document . body . appendChild ( DOM ) ;
37
38
container = DOM . querySelector ( '.container' ) ;
39
+ Object . defineProperty ( container , 'scrollHeight' , { value : 10 , writable : true } ) ;
38
40
} ) ;
39
41
afterEach ( ( ) => {
40
42
window . navigator . platform = platform ;
@@ -70,12 +72,7 @@ describe('scroll-lock', () => {
70
72
container . ontouchmove ( touchMoveEvent ) ;
71
73
expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 ) ;
72
74
expect ( stopPropagation ) . toHaveBeenCalledTimes ( 0 ) ;
73
- expect ( touchMoveEvent . target . closest ) . toHaveBeenCalledTimes ( 1 ) ;
74
- expect ( touchMoveEvent . target . closest ) . toHaveBeenCalledWith ( `[${ SCROLL_LOCK_DISABLE_ATTR } ]` ) ;
75
-
76
75
// simulate scroll middle
77
- // simulate we have enough to scroll
78
- Object . defineProperty ( container , 'scrollHeight' , { value : 10 , writable : true } ) ;
79
76
container . ontouchmove ( { ...touchMoveEvent , targetTouches : [ { clientY : - 10 } ] } ) ;
80
77
expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 ) ;
81
78
expect ( stopPropagation ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -86,26 +83,24 @@ describe('scroll-lock', () => {
86
83
expect ( preventDefault ) . toHaveBeenCalledTimes ( 2 ) ;
87
84
expect ( stopPropagation ) . toHaveBeenCalledTimes ( 1 ) ;
88
85
89
- // simulate there is a scroll-lock-disable target
90
- container . ontouchmove ( {
91
- ...touchMoveEvent ,
92
- targetTouches : [ { clientY : - 10 } ] ,
93
- target : {
94
- closest : jest . fn ( ) . mockReturnValue ( {
95
- ...container ,
96
- clientHeight : 150 ,
97
- } ) ,
98
- } ,
99
- } ) ;
100
- // assert scrolling was allowed
101
- expect ( preventDefault ) . toHaveBeenCalledTimes ( 2 ) ;
102
- expect ( stopPropagation ) . toHaveBeenCalledTimes ( 2 ) ;
103
-
104
86
scrollLock . unlockScroll ( container ) ;
105
87
expect ( container . ontouchmove ) . toBeFalsy ( ) ;
106
88
expect ( container . ontouchstart ) . toBeFalsy ( ) ;
107
89
} ) ;
108
90
91
+ it ( 'adds event listeners to the disabled targets too' , ( ) => {
92
+ const disabledTarget = DOM . querySelector ( '.disabled-target' ) ;
93
+ // init the scroll lock
94
+ scrollLock . lockScroll ( container ) ;
95
+ // assert event listeners are attached
96
+ expect ( disabledTarget . ontouchstart ) . toEqual ( expect . any ( Function ) ) ;
97
+ expect ( disabledTarget . ontouchmove ) . toEqual ( expect . any ( Function ) ) ;
98
+
99
+ scrollLock . unlockScroll ( container ) ;
100
+ expect ( disabledTarget . ontouchmove ) . toBeFalsy ( ) ;
101
+ expect ( disabledTarget . ontouchstart ) . toBeFalsy ( ) ;
102
+ } ) ;
103
+
109
104
it ( 'prevents body scrolling' , ( ) => {
110
105
scrollLock . lockScroll ( container ) ;
111
106
// assert body scroll is getting prevented when swiping up/down
0 commit comments