Skip to content

Commit 5349112

Browse files
committed
Merge branch 'feat/flyout-system' into session-flyouts/track-history
2 parents c75422e + ab3d37d commit 5349112

1 file changed

Lines changed: 94 additions & 5 deletions

File tree

packages/eui/src/components/flyout/use_flyout_resizable.test.ts

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
import { renderHook } from '@testing-library/react';
9+
import { renderHook, act, waitFor } from '@testing-library/react';
1010
import { useEuiFlyoutResizable } from './use_flyout_resizable';
1111

1212
describe('useEuiFlyoutResizable', () => {
@@ -19,11 +19,27 @@ describe('useEuiFlyoutResizable', () => {
1919
size: '50vw',
2020
};
2121

22+
// Mock DOM element with offsetWidth
23+
const createMockElement = (offsetWidth = 600) =>
24+
({ offsetWidth, style: { direction: 'ltr' } } as HTMLElement);
25+
2226
beforeEach(() => {
2327
jest.clearAllMocks();
28+
29+
// Mock window.innerWidth for getFlyoutMinMaxWidth calculations
30+
Object.defineProperty(window, 'innerWidth', {
31+
writable: true,
32+
configurable: true,
33+
value: 1200,
34+
});
35+
36+
// Mock getComputedStyle
37+
jest.spyOn(window, 'getComputedStyle').mockReturnValue({
38+
direction: 'ltr',
39+
} as CSSStyleDeclaration);
2440
});
2541

26-
it('should return original size when disabled (resizable=false)', () => {
42+
it('should not measure flyout width when disabled', () => {
2743
const { result } = renderHook(() =>
2844
useEuiFlyoutResizable({
2945
...mockProps,
@@ -32,16 +48,89 @@ describe('useEuiFlyoutResizable', () => {
3248
})
3349
);
3450

35-
// Should return the original responsive size, not a pixel value
51+
const mockElement = createMockElement(600);
52+
53+
// Set the flyout ref (this would normally trigger measurement)
54+
act(() => {
55+
result.current.setFlyoutRef(mockElement);
56+
});
57+
58+
// Should return the original responsive size, not a measured pixel value
3659
expect(result.current.size).toBe('50vw');
3760
});
3861

39-
it('should return original size when enabled but not measured yet (resizable=true)', () => {
62+
it('should return original size instead of measured width when disabled', async () => {
63+
const { result, rerender } = renderHook(
64+
(props) => useEuiFlyoutResizable(props),
65+
{
66+
initialProps: {
67+
...mockProps,
68+
enabled: true,
69+
size: '50vw',
70+
},
71+
}
72+
);
73+
74+
const mockElement = createMockElement(600);
75+
76+
// Set the flyout ref while enabled (this should measure the width)
77+
act(() => {
78+
result.current.setFlyoutRef(mockElement);
79+
});
80+
81+
// Wait for the useEffect to run and measure the width
82+
await waitFor(() => {
83+
expect(result.current.size).toBe(600);
84+
});
85+
86+
// Now disable resizing with a different size
87+
rerender({
88+
...mockProps,
89+
enabled: false,
90+
size: '400px',
91+
});
92+
93+
// Should return original size, not the previously measured width
94+
expect(result.current.size).toBe('400px');
95+
});
96+
97+
it('should not update flyout width when size changes while disabled', () => {
98+
const { result, rerender } = renderHook(
99+
(props) => useEuiFlyoutResizable(props),
100+
{
101+
initialProps: {
102+
...mockProps,
103+
enabled: false,
104+
size: 300,
105+
},
106+
}
107+
);
108+
109+
const mockElement = createMockElement(600);
110+
act(() => {
111+
result.current.setFlyoutRef(mockElement);
112+
});
113+
114+
// Should return original size
115+
expect(result.current.size).toBe(300);
116+
117+
// Change the size while disabled
118+
rerender({
119+
...mockProps,
120+
enabled: false,
121+
size: 500,
122+
});
123+
124+
// Should return the new original size, not trigger any measurement
125+
expect(result.current.size).toBe(500);
126+
});
127+
128+
it('should return original size when enabled but not measured yet', () => {
40129
const { result } = renderHook(() =>
41130
useEuiFlyoutResizable({
42131
...mockProps,
43132
enabled: true,
44-
size: 400, // Use number size for simpler testing
133+
size: 400,
45134
})
46135
);
47136

0 commit comments

Comments
 (0)