@@ -3,8 +3,24 @@ import {mountWithApp} from 'tests/utilities';
33
44import { Scrollable } from '../Scrollable' ;
55import { ScrollableContext } from '../context' ;
6+ import type { ScrollableProps } from '../Scrollable' ;
67
78describe ( '<Scrollable />' , ( ) => {
9+ let rafSpy : jest . SpyInstance ;
10+
11+ beforeAll ( ( ) => {
12+ rafSpy = jest
13+ . spyOn ( window , 'requestAnimationFrame' )
14+ . mockImplementation ( ( cb ) => {
15+ cb ( Date . now ( ) ) ;
16+ return Math . random ( ) ;
17+ } ) ;
18+ } ) ;
19+
20+ afterAll ( ( ) => {
21+ rafSpy . mockRestore ( ) ;
22+ } ) ;
23+
824 it ( 'mounts' , ( ) => {
925 const scrollable = mountWithApp ( < Scrollable /> ) ;
1026 expect ( scrollable ) . toBeDefined ( ) ;
@@ -71,4 +87,60 @@ describe('<Scrollable />', () => {
7187 tabIndex : 0 ,
7288 } ) ;
7389 } ) ;
90+
91+ it ( 'calls onScrolledToBottom when scrolled to bottom' , ( ) => {
92+ const onScrolledToBottom = jest . fn ( ) ;
93+
94+ const scrollArea = mountWithApp (
95+ < Scrollable
96+ data-test-id = "scroll-element"
97+ onScrolledToBottom = { onScrolledToBottom }
98+ >
99+ < p > Hello</ p >
100+ </ Scrollable > ,
101+ ) ;
102+
103+ const scrollNode = scrollArea . find ( 'div' , {
104+ 'data-test-id' : 'scroll-element' ,
105+ } as ScrollableProps ) ?. domNode ! ;
106+
107+ // defineProperty needed to assign values to readonly node properties
108+ Object . defineProperty ( scrollNode , 'clientHeight' , { get : ( ) => 0 } ) ;
109+ Object . defineProperty ( scrollNode , 'scrollHeight' , { get : ( ) => 10 } ) ;
110+ Object . defineProperty ( scrollNode , 'scrollTop' , { get : ( ) => 10 } ) ;
111+
112+ scrollArea . act ( ( ) => {
113+ scrollNode . dispatchEvent ( new Event ( 'scroll' ) ) ;
114+ } ) ;
115+
116+ expect ( onScrolledToBottom ) . toHaveBeenCalledTimes ( 1 ) ;
117+ } ) ;
118+
119+ it ( `doesn't call onScrolledToBottom when the scroll area is not overflowing` , ( ) => {
120+ const onScrolledToBottom = jest . fn ( ) ;
121+
122+ const scrollArea = mountWithApp (
123+ < Scrollable
124+ data-test-id = "scroll-element"
125+ onScrolledToBottom = { onScrolledToBottom }
126+ >
127+ < p > Hello</ p >
128+ </ Scrollable > ,
129+ ) ;
130+
131+ const scrollNode = scrollArea . find ( 'div' , {
132+ 'data-test-id' : 'scroll-element' ,
133+ } as ScrollableProps ) ?. domNode ! ;
134+
135+ // defineProperty needed to assign values to readonly node properties
136+ Object . defineProperty ( scrollNode , 'clientHeight' , { get : ( ) => 10 } ) ;
137+ Object . defineProperty ( scrollNode , 'scrollHeight' , { get : ( ) => 10 } ) ;
138+ Object . defineProperty ( scrollNode , 'scrollTop' , { get : ( ) => 10 } ) ;
139+
140+ scrollArea . act ( ( ) => {
141+ scrollNode . dispatchEvent ( new Event ( 'scroll' ) ) ;
142+ } ) ;
143+
144+ expect ( onScrolledToBottom ) . not . toHaveBeenCalled ( ) ;
145+ } ) ;
74146} ) ;
0 commit comments