@@ -46,29 +46,33 @@ jest.mock('../flyout.component', () => {
46
46
} ;
47
47
} ) ;
48
48
49
- // Mock hooks that would otherwise depend on ResizeObserver or animation timing
49
+ // Shared mock functions - must be defined in module scope for Jest
50
50
const mockCloseFlyout = jest . fn ( ) ;
51
+ const createMockState = ( ) => ( {
52
+ sessions : [ ] ,
53
+ flyouts : [ ] ,
54
+ layoutMode : 'side-by-side' as const ,
55
+ } ) ;
56
+ const createMockFunctions = ( ) => ( {
57
+ dispatch : jest . fn ( ) ,
58
+ addFlyout : jest . fn ( ) ,
59
+ closeFlyout : mockCloseFlyout ,
60
+ setActiveFlyout : jest . fn ( ) ,
61
+ setFlyoutWidth : jest . fn ( ) ,
62
+ goBack : jest . fn ( ) ,
63
+ goToFlyout : jest . fn ( ) ,
64
+ getHistoryItems : jest . fn ( ( ) => [ ] ) ,
65
+ } ) ;
51
66
67
+ // Mock hooks that would otherwise depend on ResizeObserver or animation timing
52
68
jest . mock ( './hooks' , ( ) => ( {
53
69
useFlyoutManagerReducer : ( ) => ( {
54
- state : { sessions : [ ] , flyouts : [ ] , layoutMode : 'side-by-side' } ,
55
- dispatch : jest . fn ( ) ,
56
- addFlyout : jest . fn ( ) ,
57
- closeFlyout : mockCloseFlyout ,
58
- setActiveFlyout : jest . fn ( ) ,
59
- setFlyoutWidth : jest . fn ( ) ,
60
- goBack : jest . fn ( ) ,
61
- goToFlyout : jest . fn ( ) ,
62
- getHistoryItems : jest . fn ( ( ) => [ ] ) ,
70
+ state : createMockState ( ) ,
71
+ ...createMockFunctions ( ) ,
63
72
} ) ,
64
73
useFlyoutManager : ( ) => ( {
65
- state : { sessions : [ ] , flyouts : [ ] , layoutMode : 'side-by-side' } ,
66
- addFlyout : jest . fn ( ) ,
67
- closeFlyout : mockCloseFlyout ,
68
- setFlyoutWidth : jest . fn ( ) ,
69
- goBack : jest . fn ( ) ,
70
- goToFlyout : jest . fn ( ) ,
71
- getHistoryItems : jest . fn ( ( ) => [ ] ) ,
74
+ state : createMockState ( ) ,
75
+ ...createMockFunctions ( ) ,
72
76
} ) ,
73
77
useIsFlyoutActive : ( ) => true ,
74
78
useHasChildFlyout : ( ) => false ,
@@ -104,13 +108,8 @@ jest.mock('./validation', () => ({
104
108
jest . mock ( './provider' , ( ) => ( {
105
109
...jest . requireActual ( './provider' ) ,
106
110
useFlyoutManager : ( ) => ( {
107
- state : { sessions : [ ] , flyouts : [ ] , layoutMode : 'side-by-side' } ,
108
- addFlyout : jest . fn ( ) ,
109
- closeFlyout : mockCloseFlyout ,
110
- setFlyoutWidth : jest . fn ( ) ,
111
- goBack : jest . fn ( ) ,
112
- goToFlyout : jest . fn ( ) ,
113
- getHistoryItems : jest . fn ( ( ) => [ ] ) ,
111
+ state : createMockState ( ) ,
112
+ ...createMockFunctions ( ) ,
114
113
} ) ,
115
114
} ) ) ;
116
115
@@ -119,6 +118,19 @@ jest.mock('../../observer/resize_observer', () => ({
119
118
useResizeObserver : ( ) => ( { width : 480 } ) ,
120
119
} ) ) ;
121
120
121
+ // Import test utilities after mocks
122
+ import {
123
+ createTestState ,
124
+ createTestSession ,
125
+ createTestFlyout ,
126
+ } from './__mocks__' ;
127
+
128
+ // Helper functions for tests - these can reference the mock functions defined above
129
+ const createFlyoutManagerMock = ( ) => ( {
130
+ state : createMockState ( ) ,
131
+ ...createMockFunctions ( ) ,
132
+ } ) ;
133
+
122
134
describe ( 'EuiManagedFlyout' , ( ) => {
123
135
const renderInProvider = ( ui : React . ReactElement ) =>
124
136
render ( < EuiFlyoutManager > { ui } </ EuiFlyoutManager > ) ;
@@ -285,7 +297,7 @@ describe('EuiManagedFlyout', () => {
285
297
} ) ;
286
298
287
299
describe ( 'onClose callback behavior' , ( ) => {
288
- it ( 'calls onClose callback during component cleanup/unmount' , ( ) => {
300
+ it ( 'does not call onClose callback during component cleanup/unmount' , ( ) => {
289
301
const onClose = jest . fn ( ) ;
290
302
291
303
const { unmount } = renderInProvider (
@@ -305,9 +317,9 @@ describe('EuiManagedFlyout', () => {
305
317
unmount ( ) ;
306
318
} ) ;
307
319
308
- // onClose should be called during cleanup
309
- expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
310
- expect ( onClose ) . toHaveBeenCalledWith ( expect . any ( MouseEvent ) ) ;
320
+ // onClose should NOT be called during cleanup (intentional design)
321
+ expect ( onClose ) . not . toHaveBeenCalled ( ) ;
322
+ expect ( mockCloseFlyout ) . toHaveBeenCalledWith ( 'cleanup-test' ) ;
311
323
} ) ;
312
324
313
325
it ( 'does not call onClose multiple times (double-firing prevention)' , ( ) => {
0 commit comments