@@ -31,10 +31,12 @@ describe('scroll-lock', () => {
3131 DOM = parseHTMLString ( `
3232 <div class="container">
3333 <div class="scrollable">long</div>
34+ <div ${ SCROLL_LOCK_DISABLE_ATTR } ="true" class="disabled-target"></div>
3435 </div>
3536 ` ) ;
3637 document . body . appendChild ( DOM ) ;
3738 container = DOM . querySelector ( '.container' ) ;
39+ Object . defineProperty ( container , 'scrollHeight' , { value : 10 , writable : true } ) ;
3840 } ) ;
3941 afterEach ( ( ) => {
4042 window . navigator . platform = platform ;
@@ -70,12 +72,7 @@ describe('scroll-lock', () => {
7072 container . ontouchmove ( touchMoveEvent ) ;
7173 expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 ) ;
7274 expect ( stopPropagation ) . toHaveBeenCalledTimes ( 0 ) ;
73- expect ( touchMoveEvent . target . closest ) . toHaveBeenCalledTimes ( 1 ) ;
74- expect ( touchMoveEvent . target . closest ) . toHaveBeenCalledWith ( `[${ SCROLL_LOCK_DISABLE_ATTR } ]` ) ;
75-
7675 // simulate scroll middle
77- // simulate we have enough to scroll
78- Object . defineProperty ( container , 'scrollHeight' , { value : 10 , writable : true } ) ;
7976 container . ontouchmove ( { ...touchMoveEvent , targetTouches : [ { clientY : - 10 } ] } ) ;
8077 expect ( preventDefault ) . toHaveBeenCalledTimes ( 1 ) ;
8178 expect ( stopPropagation ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -86,26 +83,24 @@ describe('scroll-lock', () => {
8683 expect ( preventDefault ) . toHaveBeenCalledTimes ( 2 ) ;
8784 expect ( stopPropagation ) . toHaveBeenCalledTimes ( 1 ) ;
8885
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-
10486 scrollLock . unlockScroll ( container ) ;
10587 expect ( container . ontouchmove ) . toBeFalsy ( ) ;
10688 expect ( container . ontouchstart ) . toBeFalsy ( ) ;
10789 } ) ;
10890
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+
109104 it ( 'prevents body scrolling' , ( ) => {
110105 scrollLock . lockScroll ( container ) ;
111106 // assert body scroll is getting prevented when swiping up/down
0 commit comments