-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathResizable.test.tsx
More file actions
817 lines (703 loc) · 29 KB
/
Copy pathResizable.test.tsx
File metadata and controls
817 lines (703 loc) · 29 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
import React from 'react';
import {render, screen} from '@testing-library/react';
import Resizable from '../lib/Resizable';
// Helper to simulate drag events on handle elements
// This simulates DraggableCore's onDrag callback by calling the component's resizeHandler
function createMockDragEvent() {
return {};
}
describe('render Resizable', () => {
const props = {
axis: 'both',
className: 'test-classname',
draggableOpts: {},
handleSize: [20, 20],
height: 50,
lockAspectRatio: false,
maxConstraints: [Infinity, Infinity],
minConstraints: [20, 20],
onResize: jest.fn(),
onResizeStart: jest.fn(),
onResizeStop: jest.fn(),
resizeHandles: ['se', 'e'],
transformScale: 1,
width: 50,
};
const userChildren = <span className={'children'} />;
const resizableBoxChildren = <div style={{width: '50px', height: '50px'}}>{userChildren}</div>;
beforeEach(() => {
jest.clearAllMocks();
});
test('snapshot default props', () => {
const {container} = render(<Resizable {...props}>{resizableBoxChildren}</Resizable>);
expect(container.firstChild).toMatchSnapshot();
});
test('with correct props', () => {
const {container} = render(<Resizable {...props}>{resizableBoxChildren}</Resizable>);
// Check test-classname exists and contains children
expect(container.querySelector('.test-classname')).toBeInTheDocument();
expect(container.querySelector('.test-classname .children')).toBeInTheDocument();
// Check resize handles are rendered (DraggableCore wraps them, we verify by finding the handles)
const seHandle = container.querySelector('.react-resizable-handle-se');
const eHandle = container.querySelector('.react-resizable-handle-e');
expect(seHandle).toBeInTheDocument();
expect(eHandle).toBeInTheDocument();
// Verify there are exactly 2 resize handles (corresponding to 2 DraggableCores)
const allHandles = container.querySelectorAll('.react-resizable-handle');
expect(allHandles).toHaveLength(2);
});
describe('Handles', () => {
test('with handle function', () => {
const handleFn = (axis, ref) => {
expect(axis).toMatch(/(se|e)/);
expect(ref).toMatchObject({current: null}); // ReactRef
return <span className={`custom-handle-${axis}`} ref={ref} />;
};
const {container} = render(<Resizable {...props} handle={handleFn}>{resizableBoxChildren}</Resizable>);
expect(container.querySelector('.test-classname .children')).toBeInTheDocument();
// Custom handles should be rendered
const cursorSe = container.querySelector('.custom-handle-se');
const cursorE = container.querySelector('.custom-handle-e');
expect(cursorSe).toBeInTheDocument();
expect(cursorE).toBeInTheDocument();
// Verify there are exactly 2 custom handles
expect(container.querySelectorAll('[class^="custom-handle-"]')).toHaveLength(2);
});
test('with handle component', () => {
const ResizeHandle = React.forwardRef((props, ref) => {
// $FlowIgnore doesn't know this is cloned and has handleAxis
const {handleAxis, ...restProps} = props;
return (
<div
ref={ref}
className={`react-resizable-handle element-handle-${handleAxis}`}
{...restProps}
/>
);
});
const {container} = render(<Resizable {...props} handle={<ResizeHandle />}>{resizableBoxChildren}</Resizable>);
expect(container.querySelector('.test-classname .children')).toBeInTheDocument();
// Element handles should be rendered
const cursorSe = container.querySelector('.element-handle-se');
const cursorE = container.querySelector('.element-handle-e');
expect(cursorSe).toBeInTheDocument();
expect(cursorE).toBeInTheDocument();
// Verify there are exactly 2 custom handles
expect(container.querySelectorAll('[class*="element-handle-"]')).toHaveLength(2);
});
describe('and pass handle props', () => {
test('as component', () => {
const customProps = {
...props,
resizeHandles: ['se'],
handle: <span className={'custom-component'} />
};
const {container} = render(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
expect(container.querySelector('.react-resizable-handle-se')).not.toBeInTheDocument();
expect(container.querySelector('.custom-component')).toBeInTheDocument();
});
test('as function', () => {
const customProps = {
...props,
resizeHandles: ['se'],
handle: (h) => <span className={`custom-component-${h}`} />
};
const {container} = render(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
expect(container.querySelector('.custom-component-se')).toBeInTheDocument();
});
});
});
describe('<Resizable> props filtering', () => {
const allProps = {
...props,
draggableOpts: {},
handle: <div />,
};
// Ensure everything in propTypes is represented here. Otherwise the next two tests are not valid
test('all intended props are in our allProps object', () => {
expect(['children', ...Object.keys(allProps)].sort()).toEqual(Object.keys(Resizable.propTypes).sort());
});
test('none of these props leak down to the child', () => {
const {container} = render(<Resizable {...allProps}><div className="foo">{[]}</div></Resizable>);
const fooElement = container.querySelector('.foo');
// Get the attributes that are actually on the DOM element
// In RTL, we can't access React props directly, but we can check that
// Resizable's props don't appear as DOM attributes
const attributes = Array.from(fooElement.attributes).map(attr => attr.name);
// The child should only have class and potentially children rendered inside
// class becomes className in React but 'class' in DOM
expect(attributes.filter(attr => attr !== 'class')).toEqual([]);
// Verify the child has the correct classes
expect(fooElement).toHaveClass('foo');
expect(fooElement).toHaveClass(allProps.className);
expect(fooElement).toHaveClass('react-resizable');
});
test('className is constructed properly', () => {
const {container} = render(<Resizable {...allProps}><div className="foo">{[]}</div></Resizable>);
const fooElement = container.querySelector('.foo');
expect(fooElement.className).toEqual(`foo ${allProps.className} react-resizable`);
});
});
describe('onResize callback with modified position', () => {
const customProps = {
...props,
resizeHandles: ['nw', 'sw', 'ne', 'se', 'n', 's', 'w', 'e'],
};
const mockClientRect = {
left: 0,
top: 0,
};
const node = document.createElement('div');
// $FlowIgnore need to override to have control over dummy dom element
node.getBoundingClientRect = () => ({ ...mockClientRect });
const mockEvent = {};
// Helper function to get component instance and call resizeHandler
// Since RTL doesn't expose component instances, we need to test through the actual component
// We'll use a ref to access the Resizable instance
function renderWithRef(renderProps) {
const ref = React.createRef();
const Wrapper = () => {
const resizableRef = React.useRef(null);
// Store ref for external access
React.useEffect(() => {
ref.current = resizableRef.current;
});
return (
<Resizable {...renderProps} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
};
render(<Wrapper />);
return ref;
}
// For these tests, we need to access the component's internal methods
// Since Resizable is a class component, we can use a ref
let resizableRef;
beforeEach(() => {
jest.clearAllMocks();
mockClientRect.left = 0;
mockClientRect.top = 0;
resizableRef = React.createRef();
});
test('Gradual resizing without movement between does not modify callback', () => {
render(
<Resizable {...customProps} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
expect(props.onResize).not.toHaveBeenCalled();
// Simulate drag on 'se' handle
const resizeHandler = resizableRef.current.resizeHandler('onResize', 'se');
resizeHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 60,
width: 55,
},
})
);
});
test('Movement between callbacks modifies response values', () => {
render(
<Resizable {...customProps} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
expect(props.onResize).not.toHaveBeenCalled();
// Test nw handle with movement
const nwHandler = resizableRef.current.resizeHandler('onResize', 'nw');
// First call initializes lastHandleRect (simulating first drag event)
nwHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
mockClientRect.top = -10; // Object moves between callbacks
nwHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 50, // No height change since deltaY is caused by clientRect moving vertically
width: 45,
},
})
);
mockClientRect.left = 20; // Object moves between callbacks
nwHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 40, // 50 (previous lastSize) - 10 from deltaY
width: 20, // 45 (previous lastSize) - 25 (5 from deltaX + 20 from position shift)
},
})
);
props.onResize.mockClear();
mockClientRect.left -= 10; // Object moves between callbacks
mockClientRect.top -= 10; // Object moves between callbacks
nwHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
expect(props.onResize).not.toHaveBeenCalled();
mockClientRect.left -= 10; // Object moves between callbacks
mockClientRect.top -= 10; // Object moves between callbacks
const swHandler = resizableRef.current.resizeHandler('onResize', 'sw');
swHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 50, // 40 (lastSize) + 10 from deltaY ('s' has no position adjustment)
width: 20, // 20 (lastSize) + 0 (movement cancels the deltaX)
},
})
);
mockClientRect.left -= 10; // Object moves between callbacks
mockClientRect.top -= 10; // Object moves between callbacks
const neHandler = resizableRef.current.resizeHandler('onResize', 'ne');
neHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 50, // 50 (lastSize) + 0 (movement cancels deltaY for 'n')
width: 30, // 20 (lastSize) + 10 from deltaX ('e' has no position adjustment)
},
})
);
mockClientRect.left -= 10; // Object moves between callbacks
mockClientRect.top -= 10; // Object moves between callbacks
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
seHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 60, // 50 (lastSize) + 10 from deltaY
width: 40, // 30 (lastSize) + 10 from deltaX ('se' has no position adjustment)
},
})
);
});
test('use of < 1 transformScale', () => {
render(
<Resizable {...customProps} transformScale={0.5} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
expect(props.onResize).not.toHaveBeenCalled();
const nwHandler = resizableRef.current.resizeHandler('onResize', 'nw');
nwHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
// Should be doubled
height: 30,
width: 40,
},
})
);
mockClientRect.left = 20; // Object moves between callbacks
nwHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 20, // 30 (lastSize) - 20 (deltaY=-10 / scale=0.5), clamped by minConstraints=20
width: 20, // 40 (lastSize) - 50, clamped by minConstraints=20
},
})
);
});
test('use of > 1 transformScale', () => {
render(
<Resizable {...customProps} transformScale={2} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
expect(props.onResize).not.toHaveBeenCalled();
const nwHandler = resizableRef.current.resizeHandler('onResize', 'nw');
nwHandler(mockEvent, { node, deltaX: 5, deltaY: 10 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
// Should be halved
height: 45,
width: 47.5,
},
})
);
});
describe('lockAspectRatio', () => {
[[5, 0], [0, 5], [10, 5], [5, 10], [50, 51]].forEach(([w, h]) => {
test(`drags with aspect ratio preserved w:${w} h:${h}`, () => {
render(
<Resizable {...customProps} lockAspectRatio={true} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
expect(props.onResize).not.toHaveBeenCalled();
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
seHandler(mockEvent, { node, deltaX: w, deltaY: h });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
height: 50 + Math.max(w, h),
width: 50 + Math.max(w, h),
},
})
);
});
});
});
describe('onResizeStop with stale props', () => {
// This tests the fix for a bug where onResizeStop would report stale size data
// because React's batched state updates mean props.width/height haven't updated yet
// when onResizeStop fires. The fix stores the last size from onResize and uses it
// in onResizeStop. See: https://github.com/react-grid-layout/react-grid-layout/pull/2224
test('onResizeStop reports correct size even when props are stale', () => {
const onResizeStop = jest.fn();
const onResize = jest.fn();
const resizableRef = React.createRef();
render(
<Resizable {...customProps} onResize={onResize} onResizeStop={onResizeStop} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
// Simulate onResizeStart
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
// Simulate dragging - this calls onResize with the new size
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
dragHandler(mockEvent, { node, deltaX: 20, deltaY: 30 });
expect(onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 70, height: 80 },
})
);
// Now simulate onResizeStop. In a real app, React may not have re-rendered yet,
// so props.width/height would still be 50. The deltaX/deltaY from DraggableCore's
// onStop is typically 0 or very small since the mouse hasn't moved since the last
// drag event. Without the fix, this would incorrectly report size: {width: 50, height: 50}.
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'se');
stopHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
// With the fix, onResizeStop should report the same size as the last onResize
expect(onResizeStop).toHaveBeenCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 70, height: 80 },
})
);
});
test('onResizeStop reports correct size for west handle with stale props', () => {
const onResizeStop = jest.fn();
const onResize = jest.fn();
const resizableRef = React.createRef();
const testMockClientRect = { left: 0, top: 0 };
const testNode = document.createElement('div');
testNode.getBoundingClientRect = () => ({ ...testMockClientRect });
render(
<Resizable {...customProps} onResize={onResize} onResizeStop={onResizeStop} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
// Simulate onResizeStart - this sets lastHandleRect to {left: 0, top: 0}
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'w');
startHandler(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
// Simulate dragging west (left)
// deltaX = -15 from drag, plus position adjustment of -15 (handle moved from 0 to -15)
// Total deltaX = -30, reversed for 'w' = +30, so width = 50 + 30 = 80
const dragHandler = resizableRef.current.resizeHandler('onResize', 'w');
testMockClientRect.left = -15;
dragHandler(mockEvent, { node: testNode, deltaX: -15, deltaY: 0 });
expect(onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 80, height: 50 },
})
);
// Continue dragging - element moves further left
// position adjustment: -25 - (-15) = -10, deltaX becomes -10 + (-10) = -20
// reversed for 'w' = +20. Base is lastSize.width=80 (accumulated), so width = 80 + 20 = 100.
testMockClientRect.left = -25;
dragHandler(mockEvent, { node: testNode, deltaX: -10, deltaY: 0 });
expect(onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 100, height: 50 },
})
);
// onResizeStop with stale props - should use stored lastSize (100x50 from last onResize)
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'w');
stopHandler(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
expect(onResizeStop).toHaveBeenCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 100, height: 50 },
})
);
});
});
});
// ============================================
// ADDITIONAL TEST COVERAGE
// ============================================
describe('axis restrictions', () => {
test('axis="x" only allows horizontal resizing', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} axis="x" resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
seHandler(mockEvent, { node, deltaX: 10, deltaY: 20 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
width: 60, // Changed
height: 50, // Unchanged - y-axis disabled
},
})
);
});
test('axis="y" only allows vertical resizing', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} axis="y" resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
seHandler(mockEvent, { node, deltaX: 10, deltaY: 20 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
width: 50, // Unchanged - x-axis disabled
height: 70, // Changed
},
})
);
});
test('axis="none" disables all resizing', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} axis="none" resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
seHandler(mockEvent, { node, deltaX: 10, deltaY: 20 });
// onResize should not be called when axis is 'none'
expect(props.onResize).not.toHaveBeenCalled();
});
});
describe('constraints', () => {
test('minConstraints prevents sizing below minimum', () => {
const resizableRef = React.createRef();
render(
<Resizable
{...props}
minConstraints={[30, 30]}
maxConstraints={[Infinity, Infinity]}
resizeHandles={['se']}
ref={resizableRef}
>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
// Try to resize to 10x10 (below min of 30x30)
seHandler(mockEvent, { node, deltaX: -40, deltaY: -40 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
width: 30, // Constrained to min
height: 30, // Constrained to min
},
})
);
});
test('maxConstraints prevents sizing above maximum', () => {
const resizableRef = React.createRef();
render(
<Resizable
{...props}
minConstraints={[20, 20]}
maxConstraints={[80, 80]}
resizeHandles={['se']}
ref={resizableRef}
>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const seHandler = resizableRef.current.resizeHandler('onResize', 'se');
// Try to resize to 150x150 (above max of 80x80)
seHandler(mockEvent, { node, deltaX: 100, deltaY: 100 });
expect(props.onResize).toHaveBeenLastCalledWith(
mockEvent,
expect.objectContaining({
size: {
width: 80, // Constrained to max
height: 80, // Constrained to max
},
})
);
});
});
describe('onResizeStart and onResizeStop callbacks', () => {
test('onResizeStart is called with correct data', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
expect(props.onResizeStart).toHaveBeenCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 50, height: 50 },
handle: 'se',
})
);
});
test('onResizeStop is called with correct data', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
// Start resize
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
// Drag to resize - this stores the size for onResizeStop to use
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
dragHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
// Stop resize - uses the stored size from the last onResize
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'se');
stopHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
expect(props.onResizeStop).toHaveBeenCalledWith(
mockEvent,
expect.objectContaining({
size: { width: 60, height: 60 },
handle: 'se',
})
);
});
});
describe('all resize handle directions', () => {
const allHandles = ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw'];
test('renders all specified handles', () => {
const {container} = render(
<Resizable {...props} resizeHandles={allHandles}>
{resizableBoxChildren}
</Resizable>
);
allHandles.forEach(handle => {
expect(container.querySelector(`.react-resizable-handle-${handle}`)).toBeInTheDocument();
});
expect(container.querySelectorAll('.react-resizable-handle')).toHaveLength(allHandles.length);
});
});
describe('event.persist handling', () => {
test('calls event.persist when available', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = { persist: jest.fn() };
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
expect(mockEvent.persist).toHaveBeenCalled();
});
test('works when event.persist is not available', () => {
const resizableRef = React.createRef();
render(
<Resizable {...props} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
// Event without persist method (like native DOM events)
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
// Should not throw
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
expect(() => startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 })).not.toThrow();
});
});
describe('component unmounting', () => {
test('resets data on unmount', () => {
const resizableRef = React.createRef();
const {unmount} = render(
<Resizable {...props} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
// Simulate some dragging to set internal state
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
dragHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
// Verify internal state exists before unmount
expect(resizableRef.current.lastHandleRect).not.toBeNull();
// Unmount should reset data
unmount();
// Note: We can't directly verify internal state after unmount,
// but the test ensures componentWillUnmount runs without error
});
});
describe('no callback provided', () => {
test('works without onResize callback', () => {
const resizableRef = React.createRef();
const propsWithoutCallback = { ...props };
delete propsWithoutCallback.onResize;
render(
<Resizable {...propsWithoutCallback} resizeHandles={['se']} ref={resizableRef}>
{resizableBoxChildren}
</Resizable>
);
const mockEvent = {};
const node = document.createElement('div');
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
// Should not throw even without callback
expect(() => dragHandler(mockEvent, { node, deltaX: 10, deltaY: 10 })).not.toThrow();
});
});
});