Skip to content

Commit 1483828

Browse files
committed
fix: update tests for RTL and stale props fix
- Rewrite stale props tests using RTL refs instead of Enzyme - Update onResizeStop test to include onResize call (new behavior) - Fix test expectations for west handle position tracking
1 parent 2bf017a commit 1483828

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

__tests__/Resizable.test.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,22 @@ describe('render Resizable', () => {
412412
// in onResizeStop. See: https://github.com/react-grid-layout/react-grid-layout/pull/2224
413413

414414
test('onResizeStop reports correct size even when props are stale', () => {
415-
// Create a fresh element with fresh mocks for this test
416415
const onResizeStop = jest.fn();
417416
const onResize = jest.fn();
418-
const testProps = {
419-
...customProps,
420-
onResize,
421-
onResizeStop,
422-
};
423-
const element = shallow(<Resizable {...testProps}>{resizableBoxChildren}</Resizable>);
424-
const seHandle = findHandle(element, 'se');
417+
const resizableRef = React.createRef();
418+
render(
419+
<Resizable {...customProps} onResize={onResize} onResizeStop={onResizeStop} ref={resizableRef}>
420+
{resizableBoxChildren}
421+
</Resizable>
422+
);
425423

426424
// Simulate onResizeStart
427-
seHandle.prop('onStart')(mockEvent, { node, deltaX: 0, deltaY: 0 });
425+
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
426+
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
428427

429428
// Simulate dragging - this calls onResize with the new size
430-
seHandle.prop('onDrag')(mockEvent, { node, deltaX: 20, deltaY: 30 });
429+
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
430+
dragHandler(mockEvent, { node, deltaX: 20, deltaY: 30 });
431431
expect(onResize).toHaveBeenLastCalledWith(
432432
mockEvent,
433433
expect.objectContaining({
@@ -439,7 +439,8 @@ describe('render Resizable', () => {
439439
// so props.width/height would still be 50. The deltaX/deltaY from DraggableCore's
440440
// onStop is typically 0 or very small since the mouse hasn't moved since the last
441441
// drag event. Without the fix, this would incorrectly report size: {width: 50, height: 50}.
442-
seHandle.prop('onStop')(mockEvent, { node, deltaX: 0, deltaY: 0 });
442+
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'se');
443+
stopHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
443444

444445
// With the fix, onResizeStop should report the same size as the last onResize
445446
expect(onResizeStop).toHaveBeenCalledWith(
@@ -453,27 +454,27 @@ describe('render Resizable', () => {
453454
test('onResizeStop reports correct size for west handle with stale props', () => {
454455
const onResizeStop = jest.fn();
455456
const onResize = jest.fn();
456-
const testProps = {
457-
...customProps,
458-
onResize,
459-
onResizeStop,
460-
};
457+
const resizableRef = React.createRef();
461458
const testMockClientRect = { left: 0, top: 0 };
462459
const testNode = document.createElement('div');
463-
// $FlowIgnore
464460
testNode.getBoundingClientRect = () => ({ ...testMockClientRect });
465461

466-
const element = shallow(<Resizable {...testProps}>{resizableBoxChildren}</Resizable>);
467-
const wHandle = findHandle(element, 'w');
462+
render(
463+
<Resizable {...customProps} onResize={onResize} onResizeStop={onResizeStop} ref={resizableRef}>
464+
{resizableBoxChildren}
465+
</Resizable>
466+
);
468467

469468
// Simulate onResizeStart - this sets lastHandleRect to {left: 0, top: 0}
470-
wHandle.prop('onStart')(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
469+
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'w');
470+
startHandler(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
471471

472472
// Simulate dragging west (left)
473473
// deltaX = -15 from drag, plus position adjustment of -15 (handle moved from 0 to -15)
474474
// Total deltaX = -30, reversed for 'w' = +30, so width = 50 + 30 = 80
475+
const dragHandler = resizableRef.current.resizeHandler('onResize', 'w');
475476
testMockClientRect.left = -15;
476-
wHandle.prop('onDrag')(mockEvent, { node: testNode, deltaX: -15, deltaY: 0 });
477+
dragHandler(mockEvent, { node: testNode, deltaX: -15, deltaY: 0 });
477478
expect(onResize).toHaveBeenLastCalledWith(
478479
mockEvent,
479480
expect.objectContaining({
@@ -482,21 +483,24 @@ describe('render Resizable', () => {
482483
);
483484

484485
// Continue dragging - element moves further left
486+
// position adjustment: -25 - (-15) = -10, deltaX becomes -10 + (-10) = -20
487+
// reversed for 'w' = +20, width = 50 + 20 = 70
485488
testMockClientRect.left = -25;
486-
wHandle.prop('onDrag')(mockEvent, { node: testNode, deltaX: -10, deltaY: 0 });
489+
dragHandler(mockEvent, { node: testNode, deltaX: -10, deltaY: 0 });
487490
expect(onResize).toHaveBeenLastCalledWith(
488491
mockEvent,
489492
expect.objectContaining({
490-
size: { width: 100, height: 50 },
493+
size: { width: 70, height: 50 },
491494
})
492495
);
493496

494-
// onResizeStop with stale props - should use stored lastSize
495-
wHandle.prop('onStop')(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
497+
// onResizeStop with stale props - should use stored lastSize (70x50 from last onResize)
498+
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'w');
499+
stopHandler(mockEvent, { node: testNode, deltaX: 0, deltaY: 0 });
496500
expect(onResizeStop).toHaveBeenCalledWith(
497501
mockEvent,
498502
expect.objectContaining({
499-
size: { width: 100, height: 50 },
503+
size: { width: 70, height: 50 },
500504
})
501505
);
502506
});
@@ -685,14 +689,17 @@ describe('render Resizable', () => {
685689
const node = document.createElement('div');
686690
node.getBoundingClientRect = () => ({ left: 0, top: 0 });
687691

688-
// First start
692+
// Start resize
689693
const startHandler = resizableRef.current.resizeHandler('onResizeStart', 'se');
690694
startHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
691695

692-
// Then stop with a delta - Resizable is stateless so size is calculated from
693-
// props.width/height + delta at time of stop
696+
// Drag to resize - this stores the size for onResizeStop to use
697+
const dragHandler = resizableRef.current.resizeHandler('onResize', 'se');
698+
dragHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
699+
700+
// Stop resize - uses the stored size from the last onResize
694701
const stopHandler = resizableRef.current.resizeHandler('onResizeStop', 'se');
695-
stopHandler(mockEvent, { node, deltaX: 10, deltaY: 10 });
702+
stopHandler(mockEvent, { node, deltaX: 0, deltaY: 0 });
696703

697704
expect(props.onResizeStop).toHaveBeenCalledWith(
698705
mockEvent,

0 commit comments

Comments
 (0)