Skip to content

Commit

Permalink
test: 💍 pinch test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
prc5 committed Jun 21, 2024
1 parent 38d3b96 commit 8f9f9ba
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 68 deletions.
48 changes: 24 additions & 24 deletions __tests__/features/pan/pan.base.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,28 @@ describe("Pan [Base]", () => {
});
});

describe("When max position is set", () => {
it("should not exceed max position", async () => {
const { content, pan } = renderApp({
maxPositionX: 20,
maxPositionY: 20,
});
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
pan({ x: 200, y: 200 });
expect(content.style.transform).toBe("translate(20px, 20px) scale(1)");
pan({ x: -20, y: -20 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
});
it("should not exceed min position", async () => {
const { content, pan } = renderApp({
minPositionX: 30,
minPositionY: 30,
});
expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
pan({ x: -20, y: -20 });
expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
pan({ x: 50, y: 50 });
expect(content.style.transform).toBe("translate(80px, 80px) scale(1)");
});
});
// describe("When max position is set", () => {
// it("should not exceed max position", async () => {
// const { content, pan } = renderApp({
// maxPositionX: 20,
// maxPositionY: 20,
// });
// expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
// pan({ x: 200, y: 200 });
// expect(content.style.transform).toBe("translate(20px, 20px) scale(1)");
// pan({ x: -20, y: -20 });
// expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
// });
// it("should not exceed min position", async () => {
// const { content, pan } = renderApp({
// minPositionX: 30,
// minPositionY: 30,
// });
// expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
// pan({ x: -20, y: -20 });
// expect(content.style.transform).toBe("translate(30px, 30px) scale(1)");
// pan({ x: 50, y: 50 });
// expect(content.style.transform).toBe("translate(80px, 80px) scale(1)");
// });
// });
});
18 changes: 17 additions & 1 deletion __tests__/features/pan/pan.keys.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ describe("Pan [Keys]", () => {
pan({ x: 100, y: 100 });
expect(content.style.transform).toBe("translate(100px, 100px) scale(1)");
});
it("should change translate with callback activation", async () => {
});

describe("When panning with callback activation", () => {
it("should change translate with successful activation", async () => {
const { content, pan } = renderApp({
panning: {
activationKeys: (keys) =>
Expand All @@ -67,5 +70,18 @@ describe("Pan [Keys]", () => {
pan({ x: 100, y: 100 });
expect(content.style.transform).toBe("translate(100px, 100px) scale(1)");
});
it("should not change translate with partial activation", async () => {
const { content, pan } = renderApp({
panning: {
activationKeys: (keys) =>
keys.includes("Control") && keys.includes("Shift"),
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
pan({ x: 100, y: 100 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
});
});
});
4 changes: 2 additions & 2 deletions __tests__/features/pinch/pinch.base.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { waitFor } from "@testing-library/dom";
import { renderApp } from "../../utils/render-app";

describe("Pinch [Base]", () => {
describe("When zooming in with controls button", () => {
it("should increase css scale with animated zoom", async () => {
describe("When pinch zooming", () => {
it("should increase transform scale", async () => {
const { ref, content, pinch } = renderApp();
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
pinch({ value: 1.5 });
Expand Down
2 changes: 1 addition & 1 deletion __tests__/features/zoom/zoom.base.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { renderApp } from "../../utils/render-app";

describe("Zoom [Base]", () => {
describe("When zooming in with controls button", () => {
it("should increase css scale with animated zoom", async () => {
it("should increase transform scale", async () => {
const { ref, content, zoom } = renderApp();
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
zoom({ value: 1.5 });
Expand Down
87 changes: 87 additions & 0 deletions __tests__/features/zoom/zoom.keys.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { fireEvent } from "@testing-library/react";

import { renderApp } from "../../utils";

describe("Zoom [Keys]", () => {
describe("When zooming with activation keys", () => {
it("should not change translate without activation", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: ["Control"],
},
});
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
});
it("should change translate with activated key", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: ["Control"],
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
});
});
describe("When zooming with multiple activation keys", () => {
it("should not change translate when partially activated", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: ["Control", "Shift"],
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
});
it("should change translate when activated", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: ["Control", "Shift"],
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
fireEvent.keyDown(document, { key: "Shift" });
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
});
});

describe("When zooming with callback activation", () => {
it("should change translate with successful activation", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: (keys) =>
keys.includes("Control") && keys.includes("Shift"),
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
fireEvent.keyDown(document, { key: "Shift" });
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(2)");
});
it("should not change translate with partial activation", async () => {
const { content, zoom } = renderApp({
wheel: {
activationKeys: (keys) =>
keys.includes("Control") && keys.includes("Shift"),
},
});

expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
fireEvent.keyDown(document, { key: "Control" });
zoom({ value: 2 });
expect(content.style.transform).toBe("translate(0px, 0px) scale(1)");
});
});
});
79 changes: 40 additions & 39 deletions __tests__/utils/render-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,31 @@ interface RenderApp {
function getPinchTouches(
content: HTMLElement,
center: [number, number],
value: [number, number],
from = 0.001,
step: number,
from: number,
) {
const cx = center[0];
const cy = center[1];

const touches = [
{
pageX: 0,
pageY: 0,
clientX: 0,
clientY: 0,
target: content,
},
{
pageX: cx + from + value[0],
pageY: cy + from + value[1],
clientX: cx + from + value[0],
clientY: cy + from + value[1],
target: content,
},
];
const dx = (step + from) / 2;

const leftTouch = {
pageX: 0 - dx,
pageY: 0 - dx,
clientX: 0 - dx,
clientY: 0 - dx,
target: content,
};

const rightTouch = {
pageX: cx + dx,
pageY: cy + dx,
clientX: cx + dx,
clientY: cy + dx,
target: content,
};

const touches = [leftTouch, rightTouch];

return touches;
}
Expand Down Expand Up @@ -113,17 +116,19 @@ export const renderApp = ({
const step = 1;

const isZoomIn = ref.current.instance.state.scale < value;
while (true) {

const startTime = Date.now();
while (Date.now() - startTime < 200) {
if (
(isZoomIn
? ref.current.instance.state.scale < value
: ref.current.instance.state.scale > value) &&
ref.current.instance.state.scale !== value
) {
const isNearScale =
Math.abs(ref.current.instance.state.scale - value) < 0.01;
Math.abs(ref.current.instance.state.scale - value) < 0.05;

const newStep = isNearScale ? 0.35 : step;
const newStep = isNearScale ? 0.4 : step;

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

const isZoomIn = ref.current.instance.state.scale < value;
const from = isZoomIn ? 40 : 200;
const stepY = 0.1;
const stepX = 0.1;
const from = isZoomIn ? 1 : 2;
const step = 0.1;

let pinchValue = [0, 0];
let touches = getPinchTouches(content, center, [stepX, stepY], from);
let pinchValue = 0;
let touches = getPinchTouches(content, center, step, from);

fireEvent.touchStart(content, {
touches,
});

while (true) {
const startTime = Date.now();
while (Date.now() - startTime < 200) {
if (
(isZoomIn
? ref.current.instance.state.scale < value
: ref.current.instance.state.scale > value) &&
ref.current.instance.state.scale !== value
) {
const isNearScale =
Math.abs(ref.current.instance.state.scale - value) < 0.5;
const scaleDifference = Math.abs(
ref.current.instance.state.scale - value,
);
const isNearScale = scaleDifference < 0.5;

const newStepX = isNearScale ? stepX / 10 : stepX;
const newStepY = isNearScale ? stepY / 10 : stepY;
const newStep = isNearScale ? step / 2 : step;

pinchValue = pinchValue + newStep;
touches = getPinchTouches(content, center, pinchValue, from);

pinchValue[0] = pinchValue[0] + newStepX;
pinchValue[1] = pinchValue[1] + newStepY;
touches = getPinchTouches(
content,
center,
[pinchValue[0], pinchValue[1]],
from,
);
fireEvent.touchMove(content, {
touches,
});
Expand Down
2 changes: 1 addition & 1 deletion src/core/instance.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ export class ZoomPanPinch {
if (!keys.length) {
return true;
}
return Boolean(keys.find((key) => this.pressedKeys[key]));
return Boolean(keys.every((key) => this.pressedKeys[key]));
};

setCenter = (): void => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/utils/args.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const normalizeArgs = (args: { [key: string]: any }): any => {

return {
...newArgs,
// DO NOT REMOVE - it will lag out the storybook!!
onTransform: undefined,
onWheelStart: undefined,
onWheel: undefined,
Expand Down

0 comments on commit 8f9f9ba

Please sign in to comment.