-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathSplitPane.test.tsx
More file actions
400 lines (325 loc) · 11.6 KB
/
SplitPane.test.tsx
File metadata and controls
400 lines (325 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, act } from '@testing-library/react';
import { SplitPane } from './SplitPane';
import { Pane } from './Pane';
import { triggerResize, clearResizeObservers } from '../test/setup';
// These match the mock in test/setup.ts
const CONTAINER_WIDTH = 1024;
const CONTAINER_HEIGHT = 768;
describe('SplitPane', () => {
beforeEach(() => {
clearResizeObservers();
});
it('renders children panes', async () => {
render(
<SplitPane>
<Pane>
<div>Pane 1</div>
</Pane>
<Pane>
<div>Pane 2</div>
</Pane>
</SplitPane>
);
// Flush state updates from ResizeObserver callback
await act(async () => {
await vi.runAllTimersAsync();
});
expect(screen.getByText('Pane 1')).toBeInTheDocument();
expect(screen.getByText('Pane 2')).toBeInTheDocument();
});
it('renders divider between panes', async () => {
const { container } = render(
<SplitPane>
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const divider = container.querySelector('[role="separator"]');
expect(divider).toBeInTheDocument();
});
it('applies horizontal direction class by default', () => {
const { container } = render(
<SplitPane>
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
const splitPane = container.querySelector('.split-pane');
expect(splitPane).toHaveClass('horizontal');
});
it('applies vertical direction class when specified', () => {
const { container } = render(
<SplitPane direction="vertical">
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
const splitPane = container.querySelector('.split-pane');
expect(splitPane).toHaveClass('vertical');
});
it('applies custom className', () => {
const { container } = render(
<SplitPane className="custom-class">
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
const splitPane = container.querySelector('.split-pane');
expect(splitPane).toHaveClass('custom-class');
});
it('renders correct number of dividers for multiple panes', async () => {
const { container } = render(
<SplitPane>
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
<Pane>Pane 3</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const dividers = container.querySelectorAll('[role="separator"]');
expect(dividers).toHaveLength(2); // n-1 dividers for n panes
});
});
describe('SplitPane initial size calculation', () => {
// Uses ResizeObserver mock from test/setup.ts with 1024x768 dimensions
it('distributes remaining space to panes without defaultSize', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane defaultSize="30%">Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
// First pane: 30% of 1024 = 307.2px
expect(panes[0]).toHaveStyle({ width: `${CONTAINER_WIDTH * 0.3}px` });
// Second pane: remaining 70% = 716.8px
expect(panes[1]).toHaveStyle({ width: `${CONTAINER_WIDTH * 0.7}px` });
});
it('distributes remaining space equally among multiple auto-sized panes', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane defaultSize={200}>Pane 1</Pane>
<Pane>Pane 2</Pane>
<Pane>Pane 3</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(3);
// First pane: 200px fixed
expect(panes[0]).toHaveStyle({ width: '200px' });
// Remaining (1024 - 200) = 824px split equally: 412px each
const remainingEach = (CONTAINER_WIDTH - 200) / 2;
expect(panes[1]).toHaveStyle({ width: `${remainingEach}px` });
expect(panes[2]).toHaveStyle({ width: `${remainingEach}px` });
});
it('distributes space equally when no panes have defaultSize', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
// Each pane gets 50% = 512px
const halfWidth = CONTAINER_WIDTH / 2;
expect(panes[0]).toHaveStyle({ width: `${halfWidth}px` });
expect(panes[1]).toHaveStyle({ width: `${halfWidth}px` });
});
it('handles percentage defaultSize correctly', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane defaultSize="25%">Pane 1</Pane>
<Pane defaultSize="25%">Pane 2</Pane>
<Pane>Pane 3</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(3);
// First two panes: 25% each = 256px
const quarterWidth = CONTAINER_WIDTH * 0.25;
expect(panes[0]).toHaveStyle({ width: `${quarterWidth}px` });
expect(panes[1]).toHaveStyle({ width: `${quarterWidth}px` });
// Third pane: remaining 50% = 512px
expect(panes[2]).toHaveStyle({ width: `${CONTAINER_WIDTH * 0.5}px` });
});
it('handles vertical direction correctly', async () => {
const { container } = render(
<SplitPane direction="vertical">
<Pane defaultSize="25%">Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
// First pane: 25% of 768 = 192px
expect(panes[0]).toHaveStyle({ height: `${CONTAINER_HEIGHT * 0.25}px` });
// Second pane: remaining 75% = 576px
expect(panes[1]).toHaveStyle({ height: `${CONTAINER_HEIGHT * 0.75}px` });
});
it('respects controlled size prop over defaultSize', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane size={400} defaultSize="30%">
Pane 1
</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
// First pane: controlled size of 400px (not 30%)
expect(panes[0]).toHaveStyle({ width: '400px' });
// Second pane: remaining 624px
expect(panes[1]).toHaveStyle({ width: `${CONTAINER_WIDTH - 400}px` });
});
it('updates pane sizes when controlled size prop changes', async () => {
const ControlledComponent = ({ sizes }: { sizes: number[] }) => (
<SplitPane direction="horizontal">
<Pane size={sizes[0]}>Pane 1</Pane>
<Pane size={sizes[1]}>Pane 2</Pane>
</SplitPane>
);
const { container, rerender } = render(
<ControlledComponent sizes={[200, 400]} />
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
expect(panes[0]).toHaveStyle({ width: '200px' });
expect(panes[1]).toHaveStyle({ width: '400px' });
// Change sizes (simulates parent state update after drag)
rerender(<ControlledComponent sizes={[300, 300]} />);
await act(async () => {
await vi.runAllTimersAsync();
});
expect(panes[0]).toHaveStyle({ width: '300px' });
expect(panes[1]).toHaveStyle({ width: '300px' });
// Reset to initial values (simulates clicking "Reset" button)
rerender(<ControlledComponent sizes={[200, 400]} />);
await act(async () => {
await vi.runAllTimersAsync();
});
// This is the bug: sizes should update to [200, 400]
expect(panes[0]).toHaveStyle({ width: '200px' });
expect(panes[1]).toHaveStyle({ width: '400px' });
});
});
describe('SplitPane container resize behavior', () => {
beforeEach(() => {
clearResizeObservers();
});
it('maintains controlled pixel sizes when container resizes', async () => {
const onResize = vi.fn();
const sizesAfterResize: number[][] = [];
// Custom SplitPane wrapper to capture intermediate sizes
const CapturingSplitPane = () => {
return (
<SplitPane
direction="horizontal"
onResize={(sizes) => {
sizesAfterResize.push([...sizes]);
onResize(sizes);
}}
>
<Pane size={200}>Pane 1</Pane>
<Pane size={400}>Pane 2</Pane>
</SplitPane>
);
};
const { container } = render(<CapturingSplitPane />);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes).toHaveLength(2);
// Initial sizes should be respected
expect(panes[0]).toHaveStyle({ width: '200px' });
expect(panes[1]).toHaveStyle({ width: '400px' });
// Simulate container resize (window resize)
act(() => {
triggerResize(1200, 768);
});
await act(async () => {
await vi.runAllTimersAsync();
});
// After all updates, sizes should be maintained
expect(panes[0]).toHaveStyle({ width: '200px' });
expect(panes[1]).toHaveStyle({ width: '400px' });
// onResize should NOT be called for container resize (only for user drag)
// If onResize was called, it means distributeSizes was incorrectly applied
expect(onResize).not.toHaveBeenCalled();
});
it('distributes uncontrolled panes proportionally on container resize', async () => {
const { container } = render(
<SplitPane direction="horizontal">
<Pane defaultSize={200}>Pane 1</Pane>
<Pane>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
// Initial: 200px + 824px = 1024px
expect(panes[0]).toHaveStyle({ width: '200px' });
expect(panes[1]).toHaveStyle({ width: `${CONTAINER_WIDTH - 200}px` });
// Simulate container resize to 2048px (double)
await act(async () => {
triggerResize(2048, 768);
await vi.runAllTimersAsync();
});
// Uncontrolled panes should scale proportionally
expect(panes[0]).toHaveStyle({ width: '400px' });
expect(panes[1]).toHaveStyle({ width: `${2048 - 400}px` });
});
it('maintains controlled sizes in vertical direction on container resize', async () => {
const { container } = render(
<SplitPane direction="vertical">
<Pane size={200}>Pane 1</Pane>
<Pane size={300}>Pane 2</Pane>
</SplitPane>
);
await act(async () => {
await vi.runAllTimersAsync();
});
const panes = container.querySelectorAll('[data-pane="true"]');
expect(panes[0]).toHaveStyle({ height: '200px' });
expect(panes[1]).toHaveStyle({ height: '300px' });
// Simulate container resize
await act(async () => {
triggerResize(1024, 1000);
await vi.runAllTimersAsync();
});
// Controlled sizes should be maintained
expect(panes[0]).toHaveStyle({ height: '200px' });
expect(panes[1]).toHaveStyle({ height: '300px' });
});
});