55
66import React from 'react' ;
77
8- import { ResizableButton , MIN_SIDECAR_SIZE } from './resizable_button' ;
9- import { shallow } from 'enzyme' ;
8+ import { ResizableButton } from './resizable_button' ;
9+ import { mount , shallow } from 'enzyme' ;
1010import { SIDECAR_DOCKED_MODE } from '../sidecar_service' ;
11+ import { act } from 'react-dom/test-utils' ;
12+ import { waitFor } from '@testing-library/dom' ;
13+ import { MIN_SIDECAR_SIZE } from '../helper' ;
14+
15+ const originalAddEventListener = window . addEventListener ;
16+
17+ const restoreWindowEvents = ( ) => {
18+ window . addEventListener = originalAddEventListener ;
19+ } ;
1120
1221const storeWindowEvents = ( ) => {
1322 const map : Record < string , Function > = { } ;
@@ -47,6 +56,17 @@ test('it should be vertical when docked mode is takeover', () => {
4756test ( 'it should emit onResize with new flyout size when docked right and drag and horizontal' , ( ) => {
4857 const windowEvents = storeWindowEvents ( ) ;
4958
59+ Object . defineProperty ( window , 'innerHeight' , {
60+ writable : true ,
61+ configurable : true ,
62+ value : 2000 ,
63+ } ) ;
64+ Object . defineProperty ( window , 'innerWidth' , {
65+ writable : true ,
66+ configurable : true ,
67+ value : 2000 ,
68+ } ) ;
69+
5070 const onResize = jest . fn ( ) ;
5171 const newProps = { ...props , onResize } ;
5272 const component = shallow ( < ResizableButton { ...newProps } /> ) ;
@@ -63,6 +83,17 @@ test('it should emit onResize with new flyout size when docked right and drag an
6383test ( 'it should emit onResize with new flyout size when docked left and drag and horizontal' , ( ) => {
6484 const windowEvents = storeWindowEvents ( ) ;
6585
86+ Object . defineProperty ( window , 'innerHeight' , {
87+ writable : true ,
88+ configurable : true ,
89+ value : 2000 ,
90+ } ) ;
91+ Object . defineProperty ( window , 'innerWidth' , {
92+ writable : true ,
93+ configurable : true ,
94+ value : 2000 ,
95+ } ) ;
96+
6697 const onResize = jest . fn ( ) ;
6798 const newProps = { ...props , onResize, dockedMode : SIDECAR_DOCKED_MODE . LEFT } ;
6899 const component = shallow ( < ResizableButton { ...newProps } /> ) ;
@@ -79,6 +110,17 @@ test('it should emit onResize with new flyout size when docked left and drag and
79110test ( 'it should emit onResize with new flyout size when drag and vertical' , ( ) => {
80111 const windowEvents = storeWindowEvents ( ) ;
81112
113+ Object . defineProperty ( window , 'innerHeight' , {
114+ writable : true ,
115+ configurable : true ,
116+ value : 2000 ,
117+ } ) ;
118+ Object . defineProperty ( window , 'innerWidth' , {
119+ writable : true ,
120+ configurable : true ,
121+ value : 2000 ,
122+ } ) ;
123+
82124 const onResize = jest . fn ( ) ;
83125 const newProps = { ...props , onResize, dockedMode : SIDECAR_DOCKED_MODE . TAKEOVER } ;
84126 const component = shallow ( < ResizableButton { ...newProps } /> ) ;
@@ -94,6 +136,7 @@ test('it should emit onResize with new flyout size when drag and vertical', () =
94136
95137test ( 'it should emit onResize with min size when drag if new size is below the minimum' , ( ) => {
96138 const windowEvents = storeWindowEvents ( ) ;
139+
97140 const onResize = jest . fn ( ) ;
98141 const newProps = { ...props , onResize } ;
99142 const component = shallow ( < ResizableButton { ...newProps } /> ) ;
@@ -106,3 +149,111 @@ test('it should emit onResize with min size when drag if new size is below the m
106149 expect ( onResize ) . toHaveBeenCalledWith ( newSize ) ;
107150 expect ( component ) . toMatchSnapshot ( ) ;
108151} ) ;
152+
153+ test ( 'it should not call onResize when new size exceeds window bounds' , ( ) => {
154+ const windowEvents = storeWindowEvents ( ) ;
155+ const onResize = jest . fn ( ) ;
156+
157+ // Mock window dimensions
158+ Object . defineProperty ( window , 'innerHeight' , {
159+ writable : true ,
160+ configurable : true ,
161+ value : 1000 ,
162+ } ) ;
163+ Object . defineProperty ( window , 'innerWidth' , {
164+ writable : true ,
165+ configurable : true ,
166+ value : 1200 ,
167+ } ) ;
168+
169+ // Test takeover mode with size exceeding window height
170+ const takeoverProps = {
171+ ...props ,
172+ onResize,
173+ dockedMode : SIDECAR_DOCKED_MODE . TAKEOVER ,
174+ } ;
175+ const takeoverComponent = shallow ( < ResizableButton { ...takeoverProps } /> ) ;
176+ const takeoverResizer = takeoverComponent . find ( `[data-test-subj~="resizableButton"]` ) . first ( ) ;
177+
178+ takeoverResizer . simulate ( 'mousedown' , { clientY : 0 , pageX : 0 , pageY : 0 } ) ;
179+ let mouseMoveEvent = new MouseEvent ( 'mousemove' , {
180+ clientY : - 2000 ,
181+ pageX : 0 ,
182+ pageY : 0 ,
183+ } as any ) ;
184+ windowEvents ?. mousemove ( mouseMoveEvent ) ; // Exceeds window.innerHeight
185+ windowEvents ?. mouseup ( ) ;
186+
187+ expect ( onResize ) . not . toHaveBeenCalled ( ) ;
188+
189+ // Test right mode with size exceeding window width
190+ const rightProps = {
191+ ...props ,
192+ onResize,
193+ dockedMode : SIDECAR_DOCKED_MODE . RIGHT ,
194+ } ;
195+ const rightComponent = shallow ( < ResizableButton { ...rightProps } /> ) ;
196+ const rightResizer = rightComponent . find ( `[data-test-subj~="resizableButton"]` ) . first ( ) ;
197+
198+ rightResizer . simulate ( 'mousedown' , { clientX : 0 , pageX : 0 , pageY : 0 } ) ;
199+ mouseMoveEvent = new MouseEvent ( 'mousemove' , { clientX : - 2000 , pageX : 0 , pageY : 0 } as any ) ;
200+ windowEvents ?. mousemove ( mouseMoveEvent ) ; // Exceeds window.innerWidth
201+ windowEvents ?. mouseup ( ) ;
202+
203+ expect ( onResize ) . not . toHaveBeenCalled ( ) ;
204+ } ) ;
205+
206+ test ( 'it should handle window resize events correctly for different docked modes' , async ( ) => {
207+ restoreWindowEvents ( ) ;
208+ const onResize = jest . fn ( ) ;
209+
210+ Object . defineProperty ( window , 'innerHeight' , {
211+ writable : true ,
212+ configurable : true ,
213+ value : 1000 ,
214+ } ) ;
215+ Object . defineProperty ( window , 'innerWidth' , {
216+ writable : true ,
217+ configurable : true ,
218+ value : 1200 ,
219+ } ) ;
220+
221+ const takeoverProps = {
222+ ...props ,
223+ onResize,
224+ dockedMode : SIDECAR_DOCKED_MODE . TAKEOVER ,
225+ flyoutSize : 800 ,
226+ } ;
227+ let component = mount ( < ResizableButton { ...takeoverProps } /> ) ;
228+
229+ await act ( async ( ) => {
230+ Object . defineProperty ( window , 'innerHeight' , { value : 600 } ) ;
231+ window . dispatchEvent ( new Event ( 'resize' ) ) ;
232+ } ) ;
233+
234+ await waitFor ( ( ) => {
235+ // wait for debounce
236+ expect ( onResize ) . toHaveBeenCalledWith ( 600 ) ;
237+ } ) ;
238+ onResize . mockClear ( ) ;
239+
240+ const rightProps = {
241+ ...props ,
242+ onResize,
243+ dockedMode : SIDECAR_DOCKED_MODE . RIGHT ,
244+ flyoutSize : 1000 ,
245+ } ;
246+ component = mount ( < ResizableButton { ...rightProps } /> ) ;
247+
248+ await act ( async ( ) => {
249+ Object . defineProperty ( window , 'innerWidth' , { value : 800 } ) ;
250+ window . dispatchEvent ( new Event ( 'resize' ) ) ;
251+ } ) ;
252+
253+ await waitFor ( ( ) => {
254+ // wait for debounce
255+ expect ( onResize ) . toHaveBeenCalledWith ( 800 ) ;
256+ } ) ;
257+
258+ component . unmount ( ) ;
259+ } ) ;
0 commit comments