@@ -3,6 +3,7 @@ import {mountWithApp} from 'tests/utilities';
33
44import { Scrollable } from '../Scrollable' ;
55import { ScrollableContext } from '../context' ;
6+ import type { ScrollableProps } from '../Scrollable' ;
67
78describe ( '<Scrollable />' , ( ) => {
89 it ( 'mounts' , ( ) => {
@@ -71,4 +72,60 @@ describe('<Scrollable />', () => {
7172 tabIndex : 0 ,
7273 } ) ;
7374 } ) ;
75+
76+ it ( 'calls onScrolledToBottom when scrolled to bottom' , ( ) => {
77+ const onScrolledToBottom = jest . fn ( ) ;
78+
79+ const scrollArea = mountWithApp (
80+ < Scrollable
81+ data-test-id = "scroll-element"
82+ onScrolledToBottom = { onScrolledToBottom }
83+ >
84+ < p > Hello</ p >
85+ </ Scrollable > ,
86+ ) ;
87+
88+ const scrollNode = scrollArea . find ( 'div' , {
89+ 'data-test-id' : 'scroll-element' ,
90+ } as ScrollableProps ) ?. domNode ! ;
91+
92+ // defineProperty needed to assign values to readonly node properties
93+ Object . defineProperty ( scrollNode , 'clientHeight' , { get : ( ) => 0 } ) ;
94+ Object . defineProperty ( scrollNode , 'scrollHeight' , { get : ( ) => 10 } ) ;
95+ Object . defineProperty ( scrollNode , 'scrollTop' , { get : ( ) => 10 } ) ;
96+
97+ scrollArea . act ( ( ) => {
98+ scrollNode . dispatchEvent ( new Event ( 'scroll' ) ) ;
99+ } ) ;
100+
101+ expect ( onScrolledToBottom ) . toHaveBeenCalledTimes ( 1 ) ;
102+ } ) ;
103+
104+ it ( `doesn't call onScrolledToBottom when the scroll area is not overflowing` , ( ) => {
105+ const onScrolledToBottom = jest . fn ( ) ;
106+
107+ const scrollArea = mountWithApp (
108+ < Scrollable
109+ data-test-id = "scroll-element"
110+ onScrolledToBottom = { onScrolledToBottom }
111+ >
112+ < p > Hello</ p >
113+ </ Scrollable > ,
114+ ) ;
115+
116+ const scrollNode = scrollArea . find ( 'div' , {
117+ 'data-test-id' : 'scroll-element' ,
118+ } as ScrollableProps ) ?. domNode ! ;
119+
120+ // defineProperty needed to assign values to readonly node properties
121+ Object . defineProperty ( scrollNode , 'clientHeight' , { get : ( ) => 10 } ) ;
122+ Object . defineProperty ( scrollNode , 'scrollHeight' , { get : ( ) => 10 } ) ;
123+ Object . defineProperty ( scrollNode , 'scrollTop' , { get : ( ) => 10 } ) ;
124+
125+ scrollArea . act ( ( ) => {
126+ scrollNode . dispatchEvent ( new Event ( 'scroll' ) ) ;
127+ } ) ;
128+
129+ expect ( onScrolledToBottom ) . not . toHaveBeenCalled ( ) ;
130+ } ) ;
74131} ) ;
0 commit comments