Skip to content

Commit 8b99980

Browse files
committed
chore(test): improve coverage
1 parent 9082fec commit 8b99980

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

__tests__/Resizable.test.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,20 @@ describe('render Resizable', () => {
144144
node.getBoundingClientRect = () => ({ ...mockClientRect });
145145
const mockEvent = { };
146146
const element = shallow(<Resizable {...customProps}>{resizableBoxChildren}</Resizable>);
147-
const nwHandle = element.find('DraggableCore').first();
147+
function findHandle(element, axis) {
148+
return element.find(`.react-resizable-handle-${axis}`).parent();
149+
}
148150

149151
test('Gradual resizing without movement between does not modify callback', () => {
150152
expect(props.onResize).not.toHaveBeenCalled();
151-
nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
153+
const seHandle = element.find('.react-resizable-handle-se').parent();
154+
seHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
152155
expect(props.onResize).lastCalledWith(
153156
mockEvent,
154157
expect.objectContaining({
155158
size: {
156-
height: 40,
157-
width: 45,
159+
height: 60,
160+
width: 55,
158161
},
159162
})
160163
);
@@ -163,6 +166,7 @@ describe('render Resizable', () => {
163166
test('Movement between callbacks modifies response values', () => {
164167
expect(props.onResize).not.toHaveBeenCalled();
165168

169+
const nwHandle = findHandle(element, 'nw');
166170
mockClientRect.top = -10; // Object moves between callbacks
167171
nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
168172
expect(props.onResize).lastCalledWith(
@@ -195,7 +199,7 @@ describe('render Resizable', () => {
195199

196200
mockClientRect.left -= 10; // Object moves between callbacks
197201
mockClientRect.top -= 10; // Object moves between callbacks
198-
const swHandle = element.find('DraggableCore').at(1);
202+
const swHandle = findHandle(element, 'sw');
199203
swHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
200204
expect(props.onResize).lastCalledWith(
201205
mockEvent,
@@ -209,7 +213,7 @@ describe('render Resizable', () => {
209213

210214
mockClientRect.left -= 10; // Object moves between callbacks
211215
mockClientRect.top -= 10; // Object moves between callbacks
212-
const neHandle = element.find('DraggableCore').at(2);
216+
const neHandle = findHandle(element, 'ne');
213217
neHandle.prop('onDrag')(mockEvent, { node, deltaX: 10, deltaY: 10 });
214218
expect(props.onResize).lastCalledWith(
215219
mockEvent,
@@ -238,8 +242,8 @@ describe('render Resizable', () => {
238242

239243
test('use of < 1 transformScale', () => {
240244
const element = shallow(<Resizable {...customProps} transformScale={0.5}>{resizableBoxChildren}</Resizable>);
241-
const nwHandle = element.find('DraggableCore').first();
242245
expect(props.onResize).not.toHaveBeenCalled();
246+
const nwHandle = findHandle(element, 'nw');
243247
nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
244248
expect(props.onResize).lastCalledWith(
245249
mockEvent,
@@ -267,8 +271,8 @@ describe('render Resizable', () => {
267271

268272
test('use of > 1 transformScale', () => {
269273
const element = shallow(<Resizable {...customProps} transformScale={2}>{resizableBoxChildren}</Resizable>);
270-
const nwHandle = element.find('DraggableCore').first();
271274
expect(props.onResize).not.toHaveBeenCalled();
275+
const nwHandle = findHandle(element, 'nw');
272276
nwHandle.prop('onDrag')(mockEvent, { node, deltaX: 5, deltaY: 10 });
273277
expect(props.onResize).lastCalledWith(
274278
mockEvent,
@@ -281,5 +285,26 @@ describe('render Resizable', () => {
281285
})
282286
);
283287
});
288+
289+
describe('lockAspectRatio', () => {
290+
291+
[[5, 0], [0, 5]].forEach(([w, h]) => {
292+
test(`drags with aspect ratio preserved w:${w} h:${h}`, () => {
293+
const element = shallow(<Resizable {...customProps} lockAspectRatio={true}>{resizableBoxChildren}</Resizable>);
294+
expect(props.onResize).not.toHaveBeenCalled();
295+
const seHandle = findHandle(element, 'se');
296+
seHandle.prop('onDrag')(mockEvent, { node, deltaX: w, deltaY: h });
297+
expect(props.onResize).lastCalledWith(
298+
mockEvent,
299+
expect.objectContaining({
300+
size: {
301+
height: 50 + Math.max(w, h),
302+
width: 50 + Math.max(w, h),
303+
},
304+
})
305+
);
306+
});
307+
});
308+
});
284309
});
285310
});

lib/Resizable.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,18 @@ export default class Resizable extends React.Component<Props, void> {
2929
this.resetData();
3030
}
3131

32-
lockAspectRatio(width: number, height: number, aspectRatio: number): [number, number] {
33-
height = width / aspectRatio;
34-
width = height * aspectRatio;
35-
return [width, height];
36-
}
37-
3832
resetData() {
3933
this.lastHandleRect = this.slack = null;
4034
}
4135

4236
// Clamp width and height within provided constraints
4337
runConstraints(width: number, height: number): [number, number] {
44-
const [min, max] = [this.props.minConstraints, this.props.maxConstraints];
45-
if (!min && !max) return [width, height];
38+
const {minConstraints, maxConstraints, lockAspectRatio} = this.props;
39+
// short circuit
40+
if (!minConstraints && !maxConstraints && !lockAspectRatio) return [width, height];
4641

4742
// If constraining to min and max, we need to also fit width and height to aspect ratio.
48-
if (this.props.lockAspectRatio) {
43+
if (lockAspectRatio) {
4944
const resizingHorizontally = height === this.props.height;
5045
if (resizingHorizontally) {
5146
const ratio = this.props.width / this.props.height;
@@ -69,13 +64,13 @@ export default class Resizable extends React.Component<Props, void> {
6964
width += slackW;
7065
height += slackH;
7166

72-
if (min) {
73-
width = Math.max(min[0], width);
74-
height = Math.max(min[1], height);
67+
if (minConstraints) {
68+
width = Math.max(minConstraints[0], width);
69+
height = Math.max(minConstraints[1], height);
7570
}
76-
if (max) {
77-
width = Math.min(max[0], width);
78-
height = Math.min(max[1], height);
71+
if (maxConstraints) {
72+
width = Math.min(maxConstraints[0], width);
73+
height = Math.min(maxConstraints[1], height);
7974
}
8075

8176
// If the width or height changed, we must have introduced some slack. Record it for the next iteration.

0 commit comments

Comments
 (0)