2020-02-12 02:19:43 +04:00
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
2021-04-04 15:05:16 +05:30
|
|
|
import {
|
|
|
|
render,
|
|
|
|
fireEvent,
|
|
|
|
mockBoundingClientRect,
|
|
|
|
restoreOriginalGetBoundingClientRect,
|
|
|
|
} 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";
|
|
|
|
import { KEYS } from "../keys";
|
2020-03-17 20:55:40 +01:00
|
|
|
import { ExcalidrawLinearElement } from "../element/types";
|
2020-03-23 16:38:41 -07:00
|
|
|
import { reseed } from "../random";
|
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("remove shape in non linear elements", () => {
|
2021-04-04 15:05:16 +05:30
|
|
|
beforeAll(() => {
|
|
|
|
mockBoundingClientRect();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
restoreOriginalGetBoundingClientRect();
|
|
|
|
});
|
|
|
|
|
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
|
|
|
// select tool
|
|
|
|
const tool = getByToolName("rectangle");
|
|
|
|
fireEvent.click(tool);
|
|
|
|
|
|
|
|
const canvas = container.querySelector("canvas")!;
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 20 });
|
|
|
|
fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
expect(renderScene).toHaveBeenCalledTimes(6);
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(0);
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
it("ellipse", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
// select tool
|
|
|
|
const tool = getByToolName("ellipse");
|
|
|
|
fireEvent.click(tool);
|
|
|
|
|
|
|
|
const canvas = container.querySelector("canvas")!;
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 20 });
|
|
|
|
fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
expect(renderScene).toHaveBeenCalledTimes(6);
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(0);
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
it("diamond", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
// select tool
|
|
|
|
const tool = getByToolName("diamond");
|
|
|
|
fireEvent.click(tool);
|
|
|
|
|
|
|
|
const canvas = container.querySelector("canvas")!;
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 20 });
|
|
|
|
fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
expect(renderScene).toHaveBeenCalledTimes(6);
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(0);
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
describe("multi point mode in linear elements", () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
it("arrow", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
// select tool
|
|
|
|
const tool = getByToolName("arrow");
|
|
|
|
fireEvent.click(tool);
|
|
|
|
|
|
|
|
const canvas = container.querySelector("canvas")!;
|
2020-02-21 08:17:20 -05:00
|
|
|
// first point is added on pointer down
|
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 30 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// second point, enable multi point
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 50, clientY: 60 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// third point
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 50, clientY: 60 });
|
|
|
|
fireEvent.pointerUp(canvas);
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 100, clientY: 140 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// done
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas);
|
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
fireEvent.keyDown(document, { key: KEYS.ENTER });
|
|
|
|
|
2021-04-04 15:05:16 +05:30
|
|
|
expect(renderScene).toHaveBeenCalledTimes(14);
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(1);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
const element = h.elements[0] as ExcalidrawLinearElement;
|
|
|
|
|
|
|
|
expect(element.type).toEqual("arrow");
|
|
|
|
expect(element.x).toEqual(30);
|
|
|
|
expect(element.y).toEqual(30);
|
|
|
|
expect(element.points).toEqual([
|
2020-02-12 02:19:43 +04:00
|
|
|
[0, 0],
|
|
|
|
[20, 30],
|
|
|
|
[70, 110],
|
|
|
|
]);
|
2020-03-23 16:38:41 -07:00
|
|
|
|
|
|
|
h.elements.forEach((element) => expect(element).toMatchSnapshot());
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
it("line", async () => {
|
|
|
|
const { getByToolName, container } = await render(<ExcalidrawApp />);
|
2020-02-12 02:19:43 +04:00
|
|
|
// select tool
|
|
|
|
const tool = getByToolName("line");
|
|
|
|
fireEvent.click(tool);
|
|
|
|
|
|
|
|
const canvas = container.querySelector("canvas")!;
|
2020-02-21 08:17:20 -05:00
|
|
|
// first point is added on pointer down
|
|
|
|
fireEvent.pointerDown(canvas, { clientX: 30, clientY: 30 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// second point, enable multi point
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 });
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 50, clientY: 60 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// third point
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas, { clientX: 50, clientY: 60 });
|
|
|
|
fireEvent.pointerUp(canvas);
|
|
|
|
fireEvent.pointerMove(canvas, { clientX: 100, clientY: 140 });
|
2020-02-12 02:19:43 +04:00
|
|
|
|
|
|
|
// done
|
2020-02-21 08:17:20 -05:00
|
|
|
fireEvent.pointerDown(canvas);
|
|
|
|
fireEvent.pointerUp(canvas);
|
2020-02-12 02:19:43 +04:00
|
|
|
fireEvent.keyDown(document, { key: KEYS.ENTER });
|
|
|
|
|
2021-04-04 15:05:16 +05:30
|
|
|
expect(renderScene).toHaveBeenCalledTimes(14);
|
2020-03-07 10:20:38 -05:00
|
|
|
expect(h.elements.length).toEqual(1);
|
2020-02-12 02:19:43 +04:00
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
const element = h.elements[0] as ExcalidrawLinearElement;
|
|
|
|
|
|
|
|
expect(element.type).toEqual("line");
|
|
|
|
expect(element.x).toEqual(30);
|
|
|
|
expect(element.y).toEqual(30);
|
|
|
|
expect(element.points).toEqual([
|
2020-02-12 02:19:43 +04:00
|
|
|
[0, 0],
|
|
|
|
[20, 30],
|
|
|
|
[70, 110],
|
|
|
|
]);
|
2020-03-23 16:38:41 -07:00
|
|
|
|
|
|
|
h.elements.forEach((element) => expect(element).toMatchSnapshot());
|
2020-02-12 02:19:43 +04:00
|
|
|
});
|
|
|
|
});
|