@@ -6,6 +6,9 @@ import '@testing-library/jest-dom';
66import { GlobalModal } from '../GlobalModal' ;
77import { ModalDialogManagerProvider } from '../../../context' ;
88
9+ const CLOSE_BUTTON_SELECTOR = '.str-chat__modal__close-button' ;
10+ const OVERLAY_SELECTOR = '.str-chat__modal' ;
11+
912const renderComponent = ( { props } = { } ) =>
1013 render (
1114 < ModalDialogManagerProvider >
@@ -95,14 +98,96 @@ describe('GlobalModal', () => {
9598
9699 it ( 'should call onClose if the modal overlay is clicked' , ( ) => {
97100 const onClose = jest . fn ( ) ;
98- const { container, debug } = renderComponent ( {
101+ const { container } = renderComponent ( {
99102 props : { children : textContent , onClose, open : true } ,
100103 } ) ;
101- console . log ( debug ( container ) ) ;
104+
102105 const dialogOverlay = container . querySelector ( '.str-chat__modal' ) ;
103106
104107 fireEvent . click ( dialogOverlay ) ;
105108
106109 expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
107110 } ) ;
111+
112+ it ( 'should call onClose if onCloseAttempt returns true and Escape pressed' , ( ) => {
113+ const onClose = jest . fn ( ) ;
114+ const onCloseAttempt = ( ) => true ;
115+ renderComponent ( {
116+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
117+ } ) ;
118+
119+ fireEvent (
120+ document ,
121+ new KeyboardEvent ( 'keydown' , {
122+ key : 'Escape' ,
123+ } ) ,
124+ ) ;
125+
126+ expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
127+ } ) ;
128+
129+ it ( 'should call onClose if onCloseAttempt returns true and overlay clicked' , ( ) => {
130+ const onClose = jest . fn ( ) ;
131+ const onCloseAttempt = ( ) => true ;
132+ const { container } = renderComponent ( {
133+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
134+ } ) ;
135+
136+ fireEvent . click ( container . querySelector ( OVERLAY_SELECTOR ) ) ;
137+
138+ expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
139+ } ) ;
140+
141+ it ( 'should call onClose if onCloseAttempt returns true and close button clicked' , ( ) => {
142+ const onClose = jest . fn ( ) ;
143+ const onCloseAttempt = ( ) => true ;
144+ const { container } = renderComponent ( {
145+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
146+ } ) ;
147+
148+ fireEvent . click ( container . querySelector ( CLOSE_BUTTON_SELECTOR ) ) ;
149+
150+ expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
151+ } ) ;
152+
153+ it ( 'should call onClose if onCloseAttempt returns false and Escape pressed' , ( ) => {
154+ const onClose = jest . fn ( ) ;
155+ const onCloseAttempt = ( ) => false ;
156+ renderComponent ( {
157+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
158+ } ) ;
159+
160+ fireEvent (
161+ document ,
162+ new KeyboardEvent ( 'keydown' , {
163+ key : 'Escape' ,
164+ } ) ,
165+ ) ;
166+
167+ expect ( onClose ) . not . toHaveBeenCalled ( ) ;
168+ } ) ;
169+
170+ it ( 'should call onClose if onCloseAttempt returns false and overlay clicked' , ( ) => {
171+ const onClose = jest . fn ( ) ;
172+ const onCloseAttempt = ( ) => false ;
173+ const { container } = renderComponent ( {
174+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
175+ } ) ;
176+
177+ fireEvent . click ( container . querySelector ( OVERLAY_SELECTOR ) ) ;
178+
179+ expect ( onClose ) . not . toHaveBeenCalled ( ) ;
180+ } ) ;
181+
182+ it ( 'should call onClose if onCloseAttempt returns false and close button clicked' , ( ) => {
183+ const onClose = jest . fn ( ) ;
184+ const onCloseAttempt = ( ) => false ;
185+ const { container } = renderComponent ( {
186+ props : { children : textContent , onClose, onCloseAttempt, open : true } ,
187+ } ) ;
188+
189+ fireEvent . click ( container . querySelector ( CLOSE_BUTTON_SELECTOR ) ) ;
190+
191+ expect ( onClose ) . not . toHaveBeenCalled ( ) ;
192+ } ) ;
108193} ) ;
0 commit comments