2020-02-12 02:19:43 +04:00
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
|
|
|
import { render, fireEvent } from "./test-utils";
|
2020-12-05 20:00:53 +05:30
|
|
|
import ExcalidrawApp from "../excalidraw-app";
|
2020-02-12 02:19:43 +04:00
|
|
|
import * as Renderer from "../renderer/renderScene";
|
2020-03-23 16:38:41 -07:00
|
|
|
import { reseed } from "../random";
|
2020-08-29 15:12:28 +03:00
|
|
|
import { bindOrUnbindLinearElement } from "../element/binding";
|
|
|
|
import {
|
|
|
|
ExcalidrawLinearElement,
|
|
|
|
NonDeleted,
|
|
|
|
ExcalidrawRectangleElement,
|
|
|
|
} from "../element/types";
|
|
|
|
import { UI, Pointer, Keyboard } from "./helpers/ui";
|
2020-12-01 23:36:06 +02:00
|
|
|
import { KEYS } from "../keys";
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// Unmount ReactDOM from root
|
|
|
|
ReactDOM.unmountComponentAtNode(document.getElementById("root")!);
|
|
|
|
|
|
|
|
const renderScene = jest.spyOn(Renderer, "renderScene");
|
|
|
|
beforeEach(() => {
|
|
|
|
localStorage.clear();
|
|
|
|
renderScene.mockClear();
|
2020-03-23 16:38:41 -07:00
|
|
|
reseed(7);
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
|
2020-03-18 20:44:05 +01:00
|
|
|
const { h } = window;
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
describe("move element", () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
it("rectangle", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
const canvas = container.querySelector("canvas")!;
|
|
|
|
|
|
|
|
{
|
|
|
|
// create element
|
|
|
|
const tool = getByToolName("rectangle");
|
|
|
|
fireEvent.click(tool);
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 20 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 60, clientY: 70 });
|
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
expect(renderScene).toHaveBeenCalledTimes(6);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectionElement).toBeNull();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(1);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect([h.elements[0].x, h.elements[0].y]).toEqual([30, 20]);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
renderScene.mockClear();
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 50, clientY: 20 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 20, clientY: 40 });
|
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
expect(renderScene).toHaveBeenCalledTimes(3);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectionElement).toBeNull();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(1);
|
|
|
|
expect([h.elements[0].x, h.elements[0].y]).toEqual([0, 40]);
|
2020-03-23 16:38:41 -07:00
|
|
|
|
|
|
|
h.elements.forEach((element) => expect(element).toMatchSnapshot());
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
2020-08-29 15:12:28 +03:00
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
it("rectangles with binding arrow", async () => {
|
|
|
|
await render(<ExcalidrawApp />);
|
2020-08-29 15:12:28 +03:00
|
|
|
|
|
|
|
// create elements
|
|
|
|
const rectA = UI.createElement("rectangle", { size: 100 });
|
|
|
|
const rectB = UI.createElement("rectangle", { x: 200, y: 0, size: 300 });
|
|
|
|
const line = UI.createElement("line", { x: 110, y: 50, size: 80 });
|
|
|
|
|
|
|
|
// bind line to two rectangles
|
|
|
|
bindOrUnbindLinearElement(
|
|
|
|
line as NonDeleted<ExcalidrawLinearElement>,
|
|
|
|
rectA as ExcalidrawRectangleElement,
|
|
|
|
rectB as ExcalidrawRectangleElement,
|
|
|
|
);
|
|
|
|
|
|
|
|
// select the second rectangles
|
|
|
|
new Pointer("mouse").clickOn(rectB);
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
expect(renderScene).toHaveBeenCalledTimes(20);
|
2020-08-29 15:12:28 +03:00
|
|
|
expect(h.state.selectionElement).toBeNull();
|
|
|
|
expect(h.elements.length).toEqual(3);
|
|
|
|
expect(h.state.selectedElementIds[rectB.id]).toBeTruthy();
|
|
|
|
expect([rectA.x, rectA.y]).toEqual([0, 0]);
|
|
|
|
expect([rectB.x, rectB.y]).toEqual([200, 0]);
|
|
|
|
expect([line.x, line.y]).toEqual([110, 50]);
|
|
|
|
expect([line.width, line.height]).toEqual([80, 80]);
|
|
|
|
|
|
|
|
renderScene.mockClear();
|
|
|
|
|
|
|
|
// Move selected rectangle
|
2020-12-01 23:36:06 +02:00
|
|
|
Keyboard.keyDown(KEYS.ARROW_RIGHT);
|
|
|
|
Keyboard.keyDown(KEYS.ARROW_DOWN);
|
|
|
|
Keyboard.keyDown(KEYS.ARROW_DOWN);
|
2020-08-29 15:12:28 +03:00
|
|
|
|
|
|
|
// Check that the arrow size has been changed according to moving the rectangle
|
|
|
|
expect(renderScene).toHaveBeenCalledTimes(3);
|
|
|
|
expect(h.state.selectionElement).toBeNull();
|
|
|
|
expect(h.elements.length).toEqual(3);
|
|
|
|
expect(h.state.selectedElementIds[rectB.id]).toBeTruthy();
|
|
|
|
expect([rectA.x, rectA.y]).toEqual([0, 0]);
|
|
|
|
expect([rectB.x, rectB.y]).toEqual([201, 2]);
|
|
|
|
expect([Math.round(line.x), Math.round(line.y)]).toEqual([110, 50]);
|
|
|
|
expect([Math.round(line.width), Math.round(line.height)]).toEqual([81, 81]);
|
|
|
|
|
|
|
|
h.elements.forEach((element) => expect(element).toMatchSnapshot());
|
|
|
|
});
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
describe("duplicate element on move when ALT is clicked", () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
it("rectangle", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
const canvas = container.querySelector("canvas")!;
|
|
|
|
|
|
|
|
{
|
|
|
|
// create element
|
|
|
|
const tool = getByToolName("rectangle");
|
|
|
|
fireEvent.click(tool);
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 20 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 60, clientY: 70 });
|
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
expect(renderScene).toHaveBeenCalledTimes(6);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectionElement).toBeNull();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(1);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect([h.elements[0].x, h.elements[0].y]).toEqual([30, 20]);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
renderScene.mockClear();
|
|
|
|
}
|
|
|
|
|
2020-04-11 13:37:43 +02:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 50, clientY: 20 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 20, clientY: 40, altKey: true });
|
|
|
|
|
|
|
|
// firing another pointerMove event with alt key pressed should NOT trigger
|
|
|
|
// another duplication
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 20, clientY: 40, altKey: true });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 10, clientY: 60 });
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
// TODO: This used to be 4, but binding made it go up to 5. Do we need
|
|
|
|
// that additional render?
|
|
|
|
expect(renderScene).toHaveBeenCalledTimes(5);
|
2020-03-18 20:44:05 +01:00
|
|
|
expect(h.state.selectionElement).toBeNull();
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(2);
|
2020-03-08 10:20:55 -07:00
|
|
|
|
2020-02-12 02:19:43 +04:00
|
|
|
// previous element should stay intact
|
2020-03-07 10:20:38 -05:00
|
|
|
expect([h.elements[0].x, h.elements[0].y]).toEqual([30, 20]);
|
2020-04-11 13:37:43 +02:00
|
|
|
expect([h.elements[1].x, h.elements[1].y]).toEqual([-10, 60]);
|
2020-03-23 16:38:41 -07:00
|
|
|
|
|
|
|
h.elements.forEach((element) => expect(element).toMatchSnapshot());
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
});
|