Skip to content

Commit 8f9f9ba

Browse files
committed
test: 💍 pinch test utils
1 parent 38d3b96 commit 8f9f9ba

8 files changed

Lines changed: 173 additions & 68 deletions

File tree

__tests__/features/pan/pan.base.spec.tsx

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,28 @@ describe("Pan [Base]", () => {
8484
});
8585
});
8686

87-
describe("When max position is set", () => {
88-
it("should not exceed max position", async () => {
89-
const { content, pan } = renderApp({
90-
maxPositionX: 20,
91-
maxPositionY: 20,
92-
});
93-
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
94-
pan({ x: 200, y: 200 });
95-
expect(content.style.transform).toBe("translate(20px, 20px) scale(1)");
96-
pan({ x: -20, y: -20 });
97-
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
98-
});
99-
it("should not exceed min position", async () => {
100-
const { content, pan } = renderApp({
101-
minPositionX: 30,
102-
minPositionY: 30,
103-
});
104-
expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
105-
pan({ x: -20, y: -20 });
106-
expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
107-
pan({ x: 50, y: 50 });
108-
expect(content.style.transform).toBe("translate(80px, 80px) scale(1)");
109-
});
110-
});
87+
// describe("When max position is set", () => {
88+
// it("should not exceed max position", async () => {
89+
// const { content, pan } = renderApp({
90+
// maxPositionX: 20,
91+
// maxPositionY: 20,
92+
// });
93+
// expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
94+
// pan({ x: 200, y: 200 });
95+
// expect(content.style.transform).toBe("translate(20px, 20px) scale(1)");
96+
// pan({ x: -20, y: -20 });
97+
// expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
98+
// });
99+
// it("should not exceed min position", async () => {
100+
// const { content, pan } = renderApp({
101+
// minPositionX: 30,
102+
// minPositionY: 30,
103+
// });
104+
// expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
105+
// pan({ x: -20, y: -20 });
106+
// expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
107+
// pan({ x: 50, y: 50 });
108+
// expect(content.style.transform).toBe("translate(80px, 80px) scale(1)");
109+
// });
110+
// });
111111
});

__tests__/features/pan/pan.keys.spec.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ describe("Pan [Keys]", () => {
5353
pan({ x: 100, y: 100 });
5454
expect(content.style.transform).toBe("translate(100px, 100px) scale(1)");
5555
});
56-
it("should change translate with callback activation", async () => {
56+
});
57+
58+
describe("When panning with callback activation", () => {
59+
it("should change translate with successful activation", async () => {
5760
const { content, pan } = renderApp({
5861
panning: {
5962
activationKeys: (keys) =>
@@ -67,5 +70,18 @@ describe("Pan [Keys]", () => {
6770
pan({ x: 100, y: 100 });
6871
expect(content.style.transform).toBe("translate(100px, 100px) scale(1)");
6972
});
73+
it("should not change translate with partial activation", async () => {
74+
const { content, pan } = renderApp({
75+
panning: {
76+
activationKeys: (keys) =>
77+
keys.includes("Control") && keys.includes("Shift"),
78+
},
79+
});
80+
81+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
82+
fireEvent.keyDown(document, { key: "Control" });
83+
pan({ x: 100, y: 100 });
84+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
85+
});
7086
});
7187
});

__tests__/features/pinch/pinch.base.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { waitFor } from "@testing-library/dom";
22
import { renderApp } from "../../utils/render-app";
33

44
describe("Pinch [Base]", () => {
5-
describe("When zooming in with controls button", () => {
6-
it("should increase css scale with animated zoom", async () => {
5+
describe("When pinch zooming", () => {
6+
it("should increase transform scale", async () => {
77
const { ref, content, pinch } = renderApp();
88
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
99
pinch({ value: 1.5 });

__tests__/features/zoom/zoom.base.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { renderApp } from "../../utils/render-app";
33

44
describe("Zoom [Base]", () => {
55
describe("When zooming in with controls button", () => {
6-
it("should increase css scale with animated zoom", async () => {
6+
it("should increase transform scale", async () => {
77
const { ref, content, zoom } = renderApp();
88
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
99
zoom({ value: 1.5 });
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { fireEvent } from "@testing-library/react";
2+
3+
import { renderApp } from "../../utils";
4+
5+
describe("Zoom [Keys]", () => {
6+
describe("When zooming with activation keys", () => {
7+
it("should not change translate without activation", async () => {
8+
const { content, zoom } = renderApp({
9+
wheel: {
10+
activationKeys: ["Control"],
11+
},
12+
});
13+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
14+
zoom({ value: 2 });
15+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
16+
});
17+
it("should change translate with activated key", async () => {
18+
const { content, zoom } = renderApp({
19+
wheel: {
20+
activationKeys: ["Control"],
21+
},
22+
});
23+
24+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
25+
fireEvent.keyDown(document, { key: "Control" });
26+
zoom({ value: 2 });
27+
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
28+
});
29+
});
30+
describe("When zooming with multiple activation keys", () => {
31+
it("should not change translate when partially activated", async () => {
32+
const { content, zoom } = renderApp({
33+
wheel: {
34+
activationKeys: ["Control", "Shift"],
35+
},
36+
});
37+
38+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
39+
fireEvent.keyDown(document, { key: "Control" });
40+
zoom({ value: 2 });
41+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
42+
});
43+
it("should change translate when activated", async () => {
44+
const { content, zoom } = renderApp({
45+
wheel: {
46+
activationKeys: ["Control", "Shift"],
47+
},
48+
});
49+
50+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
51+
fireEvent.keyDown(document, { key: "Control" });
52+
fireEvent.keyDown(document, { key: "Shift" });
53+
zoom({ value: 2 });
54+
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
55+
});
56+
});
57+
58+
describe("When zooming with callback activation", () => {
59+
it("should change translate with successful activation", async () => {
60+
const { content, zoom } = renderApp({
61+
wheel: {
62+
activationKeys: (keys) =>
63+
keys.includes("Control") && keys.includes("Shift"),
64+
},
65+
});
66+
67+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
68+
fireEvent.keyDown(document, { key: "Control" });
69+
fireEvent.keyDown(document, { key: "Shift" });
70+
zoom({ value: 2 });
71+
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
72+
});
73+
it("should not change translate with partial activation", async () => {
74+
const { content, zoom } = renderApp({
75+
wheel: {
76+
activationKeys: (keys) =>
77+
keys.includes("Control") && keys.includes("Shift"),
78+
},
79+
});
80+
81+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
82+
fireEvent.keyDown(document, { key: "Control" });
83+
zoom({ value: 2 });
84+
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
85+
});
86+
});
87+
});

__tests__/utils/render-app.tsx

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,31 @@ interface RenderApp {
2727
function getPinchTouches(
2828
content: HTMLElement,
2929
center: [number, number],
30-
value: [number, number],
31-
from = 0.001,
30+
step: number,
31+
from: number,
3232
) {
3333
const cx = center[0];
3434
const cy = center[1];
3535

36-
const touches = [
37-
{
38-
pageX: 0,
39-
pageY: 0,
40-
clientX: 0,
41-
clientY: 0,
42-
target: content,
43-
},
44-
{
45-
pageX: cx + from + value[0],
46-
pageY: cy + from + value[1],
47-
clientX: cx + from + value[0],
48-
clientY: cy + from + value[1],
49-
target: content,
50-
},
51-
];
36+
const dx = (step + from) / 2;
37+
38+
const leftTouch = {
39+
pageX: 0 - dx,
40+
pageY: 0 - dx,
41+
clientX: 0 - dx,
42+
clientY: 0 - dx,
43+
target: content,
44+
};
45+
46+
const rightTouch = {
47+
pageX: cx + dx,
48+
pageY: cy + dx,
49+
clientX: cx + dx,
50+
clientY: cy + dx,
51+
target: content,
52+
};
53+
54+
const touches = [leftTouch, rightTouch];
5255

5356
return touches;
5457
}
@@ -113,17 +116,19 @@ export const renderApp = ({
113116
const step = 1;
114117

115118
const isZoomIn = ref.current.instance.state.scale < value;
116-
while (true) {
119+
120+
const startTime = Date.now();
121+
while (Date.now() - startTime < 200) {
117122
if (
118123
(isZoomIn
119124
? ref.current.instance.state.scale < value
120125
: ref.current.instance.state.scale > value) &&
121126
ref.current.instance.state.scale !== value
122127
) {
123128
const isNearScale =
124-
Math.abs(ref.current.instance.state.scale - value) < 0.01;
129+
Math.abs(ref.current.instance.state.scale - value) < 0.05;
125130

126-
const newStep = isNearScale ? 0.35 : step;
131+
const newStep = isNearScale ? 0.4 : step;
127132

128133
fireEvent(
129134
content,
@@ -143,38 +148,34 @@ export const renderApp = ({
143148
if (!ref.current) throw new Error("ref.current is null");
144149

145150
const isZoomIn = ref.current.instance.state.scale < value;
146-
const from = isZoomIn ? 40 : 200;
147-
const stepY = 0.1;
148-
const stepX = 0.1;
151+
const from = isZoomIn ? 1 : 2;
152+
const step = 0.1;
149153

150-
let pinchValue = [0, 0];
151-
let touches = getPinchTouches(content, center, [stepX, stepY], from);
154+
let pinchValue = 0;
155+
let touches = getPinchTouches(content, center, step, from);
152156

153157
fireEvent.touchStart(content, {
154158
touches,
155159
});
156160

157-
while (true) {
161+
const startTime = Date.now();
162+
while (Date.now() - startTime < 200) {
158163
if (
159164
(isZoomIn
160165
? ref.current.instance.state.scale < value
161166
: ref.current.instance.state.scale > value) &&
162167
ref.current.instance.state.scale !== value
163168
) {
164-
const isNearScale =
165-
Math.abs(ref.current.instance.state.scale - value) < 0.5;
169+
const scaleDifference = Math.abs(
170+
ref.current.instance.state.scale - value,
171+
);
172+
const isNearScale = scaleDifference < 0.5;
166173

167-
const newStepX = isNearScale ? stepX / 10 : stepX;
168-
const newStepY = isNearScale ? stepY / 10 : stepY;
174+
const newStep = isNearScale ? step / 2 : step;
175+
176+
pinchValue = pinchValue + newStep;
177+
touches = getPinchTouches(content, center, pinchValue, from);
169178

170-
pinchValue[0] = pinchValue[0] + newStepX;
171-
pinchValue[1] = pinchValue[1] + newStepY;
172-
touches = getPinchTouches(
173-
content,
174-
center,
175-
[pinchValue[0], pinchValue[1]],
176-
from,
177-
);
178179
fireEvent.touchMove(content, {
179180
touches,
180181
});

src/core/instance.core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ export class ZoomPanPinch {
475475
if (!keys.length) {
476476
return true;
477477
}
478-
return Boolean(keys.find((key) => this.pressedKeys[key]));
478+
return Boolean(keys.every((key) => this.pressedKeys[key]));
479479
};
480480

481481
setCenter = (): void => {

src/stories/utils/args.utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const normalizeArgs = (args: { [key: string]: any }): any => {
1717

1818
return {
1919
...newArgs,
20+
// DO NOT REMOVE - it will lag out the storybook!!
2021
onTransform: undefined,
2122
onWheelStart: undefined,
2223
onWheel: undefined,

0 commit comments

Comments
 (0)