11import React from 'react' ;
2- import { render , screen } from '@testing-library/react' ;
2+ import { render , screen , waitFor } from '@testing-library/react' ;
33import { MessageBox } from './MessageBox' ;
4+ import userEvent from '@testing-library/user-event' ;
45
56describe ( 'MessageBox' , ( ) => {
67 it ( 'should render Message box' , ( ) => {
@@ -23,4 +24,47 @@ describe('MessageBox', () => {
2324 expect ( ref . current ) . not . toBeNull ( ) ;
2425 expect ( ref . current ) . toBeInstanceOf ( HTMLDivElement ) ;
2526 } ) ;
27+ it ( 'should call onScrollToBottomClick when scroll to top button is clicked' , async ( ) => {
28+ const spy = jest . fn ( ) ;
29+ render (
30+ < MessageBox onScrollToBottomClick = { spy } >
31+ < div > Test message content</ div >
32+ </ MessageBox >
33+ ) ;
34+
35+ // this forces button to show
36+ const region = screen . getByRole ( 'region' ) ;
37+ Object . defineProperty ( region , 'scrollHeight' , { configurable : true , value : 1000 } ) ;
38+ Object . defineProperty ( region , 'clientHeight' , { configurable : true , value : 500 } ) ;
39+ Object . defineProperty ( region , 'scrollTop' , { configurable : true , value : 0 } ) ;
40+ region . dispatchEvent ( new Event ( 'scroll' ) ) ;
41+
42+ await waitFor ( ( ) => {
43+ userEvent . click ( screen . getByRole ( 'button' , { name : / J u m p b o t t o m b u t t o n / i } ) ) ;
44+ expect ( spy ) . toHaveBeenCalled ( ) ;
45+ } ) ;
46+ } ) ;
47+ it ( 'should call onScrollToTopClick when scroll to top button is clicked' , async ( ) => {
48+ const spy = jest . fn ( ) ;
49+ render (
50+ < MessageBox onScrollToTopClick = { spy } >
51+ < div > Test message content</ div >
52+ </ MessageBox >
53+ ) ;
54+
55+ // this forces button to show
56+ const region = screen . getByRole ( 'region' ) ;
57+ Object . defineProperty ( region , 'scrollHeight' , { configurable : true , value : 1000 } ) ;
58+ Object . defineProperty ( region , 'clientHeight' , { configurable : true , value : 500 } ) ;
59+ Object . defineProperty ( region , 'scrollTop' , {
60+ configurable : true ,
61+ value : 500
62+ } ) ;
63+ region . dispatchEvent ( new Event ( 'scroll' ) ) ;
64+
65+ await waitFor ( ( ) => {
66+ userEvent . click ( screen . getByRole ( 'button' , { name : / J u m p t o p b u t t o n / i } ) ) ;
67+ expect ( spy ) . toHaveBeenCalled ( ) ;
68+ } ) ;
69+ } ) ;
2670} ) ;
0 commit comments