Skip to content

Commit d2c4f55

Browse files
committed
test: 💍 added pinch panning tests
1 parent 6b48bc0 commit d2c4f55

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { waitFor } from "@testing-library/dom";
2+
import { renderApp } from "../../utils/render-app";
3+
4+
describe("Pinch [Panning]", () => {
5+
describe("When pinch zooming", () => {
6+
it("should allow for panning during pinch gesture", async () => {
7+
const { ref, content, pinch } = renderApp();
8+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
9+
pinch({ value: 1.5, targetCenter: [-20, -20] });
10+
await waitFor(() => {
11+
expect(content.style.transform).toBe(
12+
"translate(-20px, -20px) scale(1.5)",
13+
);
14+
expect(ref.current?.instance.state.scale).toBe(1.5);
15+
});
16+
});
17+
});
18+
});

__tests__/utils/render-app.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ interface RenderApp {
2121
pan: (options: { x: number; y: number; steps?: number }) => void;
2222
touchPan: (options: { x: number; y: number; steps?: number }) => void;
2323
zoom: (options: { value: number; center?: [number, number] }) => void;
24-
pinch: (options: { value: number; center?: [number, number] }) => void;
24+
pinch: (options: {
25+
value: number;
26+
center?: [number, number];
27+
targetCenter?: [number, number];
28+
}) => void;
2529
}
2630

2731
const waitForPreviousActionToEnd = () => {
@@ -42,10 +46,10 @@ function getPinchTouches(
4246
const dx = (step + from) / 2;
4347

4448
const leftTouch = {
45-
pageX: 0 - dx,
46-
pageY: 0 - dx,
47-
clientX: 0 - dx,
48-
clientY: 0 - dx,
49+
pageX: cx - dx,
50+
pageY: cy - dx,
51+
clientX: cx - dx,
52+
clientY: cy - dx,
4953
target: content,
5054
};
5155

@@ -85,6 +89,9 @@ export const renderApp = ({
8589
doubleClick: {
8690
disabled: true,
8791
},
92+
velocityAnimation: {
93+
disabled: true,
94+
},
8895
autoAlignment: {
8996
disabled: true,
9097
},
@@ -155,15 +162,15 @@ export const renderApp = ({
155162

156163
waitForPreviousActionToEnd();
157164

165+
const targetCenter = options?.targetCenter || center;
158166
const isZoomIn = ref.current.instance.state.scale < value;
159167
const from = isZoomIn ? 1 : 2;
160168
const step = 0.1;
161169

162170
let pinchValue = 0;
163-
let touches = getPinchTouches(content, center, step, from);
164171

165172
fireEvent.touchStart(content, {
166-
touches,
173+
touches: getPinchTouches(content, center, step, from),
167174
});
168175

169176
const startTime = Date.now();
@@ -182,18 +189,21 @@ export const renderApp = ({
182189
const newStep = isNearScale ? step / 6 : step;
183190

184191
pinchValue = pinchValue + newStep;
185-
touches = getPinchTouches(content, center, pinchValue, from);
186192

187193
fireEvent.touchMove(content, {
188-
touches,
194+
touches: getPinchTouches(content, center, pinchValue, from),
189195
});
190196
} else {
191197
break;
192198
}
193199
}
194200

201+
fireEvent.touchMove(content, {
202+
touches: getPinchTouches(content, targetCenter, pinchValue, from),
203+
});
204+
195205
fireEvent.touchEnd(content, {
196-
touches,
206+
touches: getPinchTouches(content, center, pinchValue, from),
197207
});
198208
};
199209

0 commit comments

Comments
 (0)