12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781 |
- import { reseed } from "../random";
- import React from "react";
- import ReactDOM from "react-dom";
- import * as Renderer from "../renderer/renderScene";
- import { waitFor, render, screen, fireEvent } from "./test-utils";
- import App from "../components/App";
- import { setLanguage } from "../i18n";
- import { ToolName } from "./queries/toolQueries";
- import { KEYS, Key } from "../keys";
- import { setDateTimeForTests } from "../utils";
- import { ExcalidrawElement } from "../element/types";
- import { getTransformHandles as _getTransformHandles } from "../element";
- import { queryByText } from "@testing-library/react";
- import { copiedStyles } from "../actions/actionStyles";
- const { h } = window;
- const renderScene = jest.spyOn(Renderer, "renderScene");
- let getByToolName: (name: string) => HTMLElement = null!;
- let canvas: HTMLCanvasElement = null!;
- const clickTool = (toolName: ToolName) => {
- fireEvent.click(getByToolName(toolName));
- };
- const createElement = (
- type: ToolName,
- {
- x = 0,
- y = x,
- size = 10,
- }: {
- x?: number;
- y?: number;
- size?: number;
- },
- ) => {
- clickTool(type);
- mouse.reset();
- mouse.down(x, y);
- mouse.reset();
- mouse.up(x + size, y + size);
- return h.elements[h.elements.length - 1];
- };
- const group = (elements: ExcalidrawElement[]) => {
- mouse.select(elements);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- };
- const assertSelectedElements = (...elements: ExcalidrawElement[]) => {
- expect(
- getSelectedElements().map((element) => {
- return element.id;
- }),
- ).toEqual(expect.arrayContaining(elements.map((element) => element.id)));
- };
- const clearSelection = () => {
- // @ts-ignore
- h.app.clearSelection(null);
- expect(getSelectedElements().length).toBe(0);
- };
- let altKey = false;
- let shiftKey = false;
- let ctrlKey = false;
- function withModifierKeys(
- modifiers: { alt?: boolean; shift?: boolean; ctrl?: boolean },
- cb: () => void,
- ) {
- const prevAltKey = altKey;
- const prevShiftKey = shiftKey;
- const prevCtrlKey = ctrlKey;
- altKey = !!modifiers.alt;
- shiftKey = !!modifiers.shift;
- ctrlKey = !!modifiers.ctrl;
- try {
- cb();
- } finally {
- altKey = prevAltKey;
- shiftKey = prevShiftKey;
- ctrlKey = prevCtrlKey;
- }
- }
- const hotkeyDown = (hotkey: Key) => {
- const key = KEYS[hotkey];
- if (typeof key !== "string") {
- throw new Error("must provide a hotkey, not a key code");
- }
- keyDown(key);
- };
- const hotkeyUp = (hotkey: Key) => {
- const key = KEYS[hotkey];
- if (typeof key !== "string") {
- throw new Error("must provide a hotkey, not a key code");
- }
- keyUp(key);
- };
- const keyDown = (key: string) => {
- fireEvent.keyDown(document, {
- key,
- ctrlKey,
- shiftKey,
- altKey,
- keyCode: key.toUpperCase().charCodeAt(0),
- which: key.toUpperCase().charCodeAt(0),
- });
- };
- const keyUp = (key: string) => {
- fireEvent.keyUp(document, {
- key,
- ctrlKey,
- shiftKey,
- altKey,
- keyCode: key.toUpperCase().charCodeAt(0),
- which: key.toUpperCase().charCodeAt(0),
- });
- };
- const hotkeyPress = (key: Key) => {
- hotkeyDown(key);
- hotkeyUp(key);
- };
- const keyPress = (key: string) => {
- keyDown(key);
- keyUp(key);
- };
- class Pointer {
- private clientX = 0;
- private clientY = 0;
- constructor(
- private readonly pointerType: "mouse" | "touch" | "pen",
- private readonly pointerId = 1,
- ) {}
- reset() {
- this.clientX = 0;
- this.clientY = 0;
- }
- getPosition() {
- return [this.clientX, this.clientY];
- }
- restorePosition(x = 0, y = 0) {
- this.clientX = x;
- this.clientY = y;
- fireEvent.pointerMove(canvas, this.getEvent());
- }
- private getEvent() {
- return {
- clientX: this.clientX,
- clientY: this.clientY,
- pointerType: this.pointerType,
- pointerId: this.pointerId,
- altKey,
- shiftKey,
- ctrlKey,
- };
- }
- move(dx: number, dy: number) {
- if (dx !== 0 || dy !== 0) {
- this.clientX += dx;
- this.clientY += dy;
- fireEvent.pointerMove(canvas, this.getEvent());
- }
- }
- down(dx = 0, dy = 0) {
- this.move(dx, dy);
- fireEvent.pointerDown(canvas, this.getEvent());
- }
- up(dx = 0, dy = 0) {
- this.move(dx, dy);
- fireEvent.pointerUp(canvas, this.getEvent());
- }
- click(dx = 0, dy = 0) {
- this.down(dx, dy);
- this.up();
- }
- doubleClick(dx = 0, dy = 0) {
- this.move(dx, dy);
- fireEvent.doubleClick(canvas, this.getEvent());
- }
- select(
- /** if multiple elements supplied, they're shift-selected */
- elements: ExcalidrawElement | ExcalidrawElement[],
- ) {
- clearSelection();
- withModifierKeys({ shift: true }, () => {
- elements = Array.isArray(elements) ? elements : [elements];
- elements.forEach((element) => {
- mouse.reset();
- mouse.click(element.x, element.y);
- });
- });
- mouse.reset();
- }
- clickOn(element: ExcalidrawElement) {
- mouse.reset();
- mouse.click(element.x, element.y);
- mouse.reset();
- }
- }
- const mouse = new Pointer("mouse");
- const finger1 = new Pointer("touch", 1);
- const finger2 = new Pointer("touch", 2);
- const clickLabeledElement = (label: string) => {
- const element = document.querySelector(`[aria-label='${label}']`);
- if (!element) {
- throw new Error(`No labeled element found: ${label}`);
- }
- fireEvent.click(element);
- };
- const getSelectedElements = (): ExcalidrawElement[] => {
- return h.elements.filter((element) => h.state.selectedElementIds[element.id]);
- };
- const getSelectedElement = (): ExcalidrawElement => {
- const selectedElements = getSelectedElements();
- if (selectedElements.length !== 1) {
- throw new Error(
- `expected 1 selected element; got ${selectedElements.length}`,
- );
- }
- return selectedElements[0];
- };
- function getStateHistory() {
- // @ts-ignore
- return h.history.stateHistory;
- }
- type HandlerRectanglesRet = keyof ReturnType<typeof _getTransformHandles>;
- const getTransformHandles = (pointerType: "mouse" | "touch" | "pen") => {
- const rects = _getTransformHandles(
- getSelectedElement(),
- h.state.zoom,
- pointerType,
- ) as {
- [T in HandlerRectanglesRet]: [number, number, number, number];
- };
- const rv: { [K in keyof typeof rects]: [number, number] } = {} as any;
- for (const handlePos in rects) {
- const [x, y, width, height] = rects[handlePos as keyof typeof rects];
- rv[handlePos as keyof typeof rects] = [x + width / 2, y + height / 2];
- }
- return rv;
- };
- /**
- * This is always called at the end of your test, so usually you don't need to call it.
- * However, if you have a long test, you might want to call it during the test so it's easier
- * to debug where a test failure came from.
- */
- const checkpoint = (name: string) => {
- expect(renderScene.mock.calls.length).toMatchSnapshot(
- `[${name}] number of renders`,
- );
- expect(h.state).toMatchSnapshot(`[${name}] appState`);
- expect(h.history.getSnapshotForTest()).toMatchSnapshot(`[${name}] history`);
- expect(h.elements.length).toMatchSnapshot(`[${name}] number of elements`);
- h.elements.forEach((element, i) =>
- expect(element).toMatchSnapshot(`[${name}] element ${i}`),
- );
- };
- beforeEach(async () => {
- // Unmount ReactDOM from root
- ReactDOM.unmountComponentAtNode(document.getElementById("root")!);
- localStorage.clear();
- renderScene.mockClear();
- h.history.clear();
- reseed(7);
- setDateTimeForTests("201933152653");
- mouse.reset();
- finger1.reset();
- finger2.reset();
- altKey = ctrlKey = shiftKey = false;
- await setLanguage("en.json");
- const renderResult = render(<App />);
- getByToolName = renderResult.getByToolName;
- canvas = renderResult.container.querySelector("canvas")!;
- });
- afterEach(() => {
- checkpoint("end of test");
- });
- describe("regression tests", () => {
- it("draw every type of shape", () => {
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(20, 10);
- clickTool("diamond");
- mouse.down(10, -10);
- mouse.up(20, 10);
- clickTool("ellipse");
- mouse.down(10, -10);
- mouse.up(20, 10);
- clickTool("arrow");
- mouse.down(40, -10);
- mouse.up(50, 10);
- clickTool("line");
- mouse.down(40, -10);
- mouse.up(50, 10);
- clickTool("arrow");
- mouse.click(40, -10);
- mouse.click(50, 10);
- mouse.click(30, 10);
- hotkeyPress("ENTER");
- clickTool("line");
- mouse.click(40, -20);
- mouse.click(50, 10);
- mouse.click(30, 10);
- hotkeyPress("ENTER");
- clickTool("draw");
- mouse.down(40, -20);
- mouse.up(50, 10);
- expect(h.elements.map((element) => element.type)).toEqual([
- "rectangle",
- "diamond",
- "ellipse",
- "arrow",
- "line",
- "arrow",
- "line",
- "draw",
- ]);
- });
- it("click to select a shape", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- const firstRectPos = mouse.getPosition();
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- const prevSelectedId = getSelectedElement().id;
- mouse.restorePosition(...firstRectPos);
- mouse.click();
- expect(getSelectedElement().id).not.toEqual(prevSelectedId);
- });
- for (const [keys, shape] of [
- ["2r", "rectangle"],
- ["3d", "diamond"],
- ["4e", "ellipse"],
- ["5a", "arrow"],
- ["6l", "line"],
- ["7x", "draw"],
- ] as [string, ExcalidrawElement["type"]][]) {
- for (const key of keys) {
- it(`hotkey ${key} selects ${shape} tool`, () => {
- keyPress(key);
- mouse.down(10, 10);
- mouse.up(10, 10);
- expect(getSelectedElement().type).toBe(shape);
- });
- }
- }
- it("change the properties of a shape", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickLabeledElement("Background");
- clickLabeledElement("#fa5252");
- clickLabeledElement("Stroke");
- clickLabeledElement("#5f3dc4");
- expect(getSelectedElement().backgroundColor).toBe("#fa5252");
- expect(getSelectedElement().strokeColor).toBe("#5f3dc4");
- });
- it("resize an element, trying every resize handle", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- const transformHandles = getTransformHandles("mouse");
- delete transformHandles.rotation; // exclude rotation handle
- for (const handlePos in transformHandles) {
- const [x, y] = transformHandles[
- handlePos as keyof typeof transformHandles
- ];
- const { width: prevWidth, height: prevHeight } = getSelectedElement();
- mouse.restorePosition(x, y);
- mouse.down();
- mouse.up(-5, -5);
- const {
- width: nextWidthNegative,
- height: nextHeightNegative,
- } = getSelectedElement();
- expect(
- prevWidth !== nextWidthNegative || prevHeight !== nextHeightNegative,
- ).toBeTruthy();
- checkpoint(`resize handle ${handlePos} (-5, -5)`);
- mouse.down();
- mouse.up(5, 5);
- const { width, height } = getSelectedElement();
- expect(width).toBe(prevWidth);
- expect(height).toBe(prevHeight);
- checkpoint(`unresize handle ${handlePos} (-5, -5)`);
- mouse.restorePosition(x, y);
- mouse.down();
- mouse.up(5, 5);
- const {
- width: nextWidthPositive,
- height: nextHeightPositive,
- } = getSelectedElement();
- expect(
- prevWidth !== nextWidthPositive || prevHeight !== nextHeightPositive,
- ).toBeTruthy();
- checkpoint(`resize handle ${handlePos} (+5, +5)`);
- mouse.down();
- mouse.up(-5, -5);
- const { width: finalWidth, height: finalHeight } = getSelectedElement();
- expect(finalWidth).toBe(prevWidth);
- expect(finalHeight).toBe(prevHeight);
- checkpoint(`unresize handle ${handlePos} (+5, +5)`);
- }
- });
- it("click on an element and drag it", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- const { x: prevX, y: prevY } = getSelectedElement();
- mouse.down(-10, -10);
- mouse.up(10, 10);
- const { x: nextX, y: nextY } = getSelectedElement();
- expect(nextX).toBeGreaterThan(prevX);
- expect(nextY).toBeGreaterThan(prevY);
- checkpoint("dragged");
- mouse.down();
- mouse.up(-10, -10);
- const { x, y } = getSelectedElement();
- expect(x).toBe(prevX);
- expect(y).toBe(prevY);
- });
- it("alt-drag duplicates an element", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- expect(
- h.elements.filter((element) => element.type === "rectangle").length,
- ).toBe(1);
- withModifierKeys({ alt: true }, () => {
- mouse.down(-10, -10);
- mouse.up(10, 10);
- });
- expect(
- h.elements.filter((element) => element.type === "rectangle").length,
- ).toBe(2);
- });
- it("click-drag to select a group", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- const finalPosition = mouse.getPosition();
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- mouse.restorePosition(0, 0);
- mouse.down();
- mouse.restorePosition(...finalPosition);
- mouse.up(5, 5);
- expect(
- h.elements.filter((element) => h.state.selectedElementIds[element.id])
- .length,
- ).toBe(2);
- });
- it("shift-click to multiselect, then drag", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- const prevRectsXY = h.elements
- .filter((element) => element.type === "rectangle")
- .map((element) => ({ x: element.x, y: element.y }));
- mouse.reset();
- mouse.click(10, 10);
- withModifierKeys({ shift: true }, () => {
- mouse.click(20, 0);
- });
- mouse.down();
- mouse.up(10, 10);
- h.elements
- .filter((element) => element.type === "rectangle")
- .forEach((element, i) => {
- expect(element.x).toBeGreaterThan(prevRectsXY[i].x);
- expect(element.y).toBeGreaterThan(prevRectsXY[i].y);
- });
- });
- it("pinch-to-zoom works", () => {
- expect(h.state.zoom).toBe(1);
- finger1.down(50, 50);
- finger2.down(60, 50);
- finger1.move(-10, 0);
- expect(h.state.zoom).toBeGreaterThan(1);
- const zoomed = h.state.zoom;
- finger1.move(5, 0);
- finger2.move(-5, 0);
- expect(h.state.zoom).toBeLessThan(zoomed);
- });
- it("two-finger scroll works", () => {
- const startScrollY = h.state.scrollY;
- finger1.down(50, 50);
- finger2.down(60, 50);
- finger1.up(0, -10);
- finger2.up(0, -10);
- expect(h.state.scrollY).toBeLessThan(startScrollY);
- const startScrollX = h.state.scrollX;
- finger1.restorePosition(50, 50);
- finger2.restorePosition(50, 60);
- finger1.down();
- finger2.down();
- finger1.up(10, 0);
- finger2.up(10, 0);
- expect(h.state.scrollX).toBeGreaterThan(startScrollX);
- });
- it("spacebar + drag scrolls the canvas", () => {
- const { scrollX: startScrollX, scrollY: startScrollY } = h.state;
- hotkeyDown("SPACE");
- mouse.down(50, 50);
- mouse.up(60, 60);
- hotkeyUp("SPACE");
- const { scrollX, scrollY } = h.state;
- expect(scrollX).not.toEqual(startScrollX);
- expect(scrollY).not.toEqual(startScrollY);
- });
- it("arrow keys", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- hotkeyPress("ARROW_LEFT");
- hotkeyPress("ARROW_LEFT");
- hotkeyPress("ARROW_RIGHT");
- hotkeyPress("ARROW_UP");
- hotkeyPress("ARROW_UP");
- hotkeyPress("ARROW_DOWN");
- expect(h.elements[0].x).toBe(9);
- expect(h.elements[0].y).toBe(9);
- });
- it("undo/redo drawing an element", () => {
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(20, 10);
- clickTool("rectangle");
- mouse.down(10, 0);
- mouse.up(30, 20);
- clickTool("arrow");
- mouse.click(60, -10);
- mouse.click(60, 10);
- mouse.click(40, 10);
- hotkeyPress("ENTER");
- expect(h.elements.filter((element) => !element.isDeleted).length).toBe(3);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("z");
- keyPress("z");
- });
- expect(h.elements.filter((element) => !element.isDeleted).length).toBe(2);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("z");
- });
- expect(h.elements.filter((element) => !element.isDeleted).length).toBe(1);
- withModifierKeys({ ctrl: true, shift: true }, () => {
- keyPress("z");
- });
- expect(h.elements.filter((element) => !element.isDeleted).length).toBe(2);
- });
- it("noop interaction after undo shouldn't create history entry", () => {
- // NOTE: this will fail if this test case is run in isolation. There's
- // some leaking state or race conditions in initialization/teardown
- // (couldn't figure out)
- expect(getStateHistory().length).toBe(0);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- const firstElementEndPoint = mouse.getPosition();
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- const secondElementEndPoint = mouse.getPosition();
- expect(getStateHistory().length).toBe(2);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("z");
- });
- expect(getStateHistory().length).toBe(1);
- // clicking an element shouldn't add to history
- mouse.restorePosition(...firstElementEndPoint);
- mouse.click();
- expect(getStateHistory().length).toBe(1);
- withModifierKeys({ shift: true, ctrl: true }, () => {
- keyPress("z");
- });
- expect(getStateHistory().length).toBe(2);
- // clicking an element shouldn't add to history
- mouse.click();
- expect(getStateHistory().length).toBe(2);
- const firstSelectedElementId = getSelectedElement().id;
- // same for clicking the element just redo-ed
- mouse.restorePosition(...secondElementEndPoint);
- mouse.click();
- expect(getStateHistory().length).toBe(2);
- expect(getSelectedElement().id).not.toEqual(firstSelectedElementId);
- });
- it("zoom hotkeys", () => {
- expect(h.state.zoom).toBe(1);
- fireEvent.keyDown(document, { code: "Equal", ctrlKey: true });
- fireEvent.keyUp(document, { code: "Equal", ctrlKey: true });
- expect(h.state.zoom).toBeGreaterThan(1);
- fireEvent.keyDown(document, { code: "Minus", ctrlKey: true });
- fireEvent.keyUp(document, { code: "Minus", ctrlKey: true });
- expect(h.state.zoom).toBe(1);
- });
- it("rerenders UI on language change", async () => {
- // select rectangle tool to show properties menu
- clickTool("rectangle");
- // english lang should display `hachure` label
- expect(screen.queryByText(/hachure/i)).not.toBeNull();
- fireEvent.change(document.querySelector(".dropdown-select__language")!, {
- target: { value: "de-DE" },
- });
- // switching to german, `hachure` label should no longer exist
- await waitFor(() => expect(screen.queryByText(/hachure/i)).toBeNull());
- // reset language
- fireEvent.change(document.querySelector(".dropdown-select__language")!, {
- target: { value: "en" },
- });
- // switching back to English
- await waitFor(() => expect(screen.queryByText(/hachure/i)).not.toBeNull());
- });
- it("make a group and duplicate it", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- const end = mouse.getPosition();
- mouse.reset();
- mouse.down();
- mouse.restorePosition(...end);
- mouse.up();
- expect(h.elements.length).toBe(3);
- for (const element of h.elements) {
- expect(element.groupIds.length).toBe(0);
- expect(h.state.selectedElementIds[element.id]).toBe(true);
- }
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- for (const element of h.elements) {
- expect(element.groupIds.length).toBe(1);
- }
- withModifierKeys({ alt: true }, () => {
- mouse.restorePosition(...end);
- mouse.down();
- mouse.up(10, 10);
- });
- expect(h.elements.length).toBe(6);
- const groups = new Set();
- for (const element of h.elements) {
- for (const groupId of element.groupIds) {
- groups.add(groupId);
- }
- }
- expect(groups.size).toBe(2);
- });
- it("double click to edit a group", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("a");
- keyPress("g");
- });
- expect(getSelectedElements().length).toBe(3);
- expect(h.state.editingGroupId).toBe(null);
- mouse.doubleClick();
- expect(getSelectedElements().length).toBe(1);
- expect(h.state.editingGroupId).not.toBe(null);
- });
- it("adjusts z order when grouping", () => {
- const positions = [];
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- const ids = h.elements.map((element) => element.id);
- mouse.restorePosition(...positions[0]);
- mouse.click();
- mouse.restorePosition(...positions[2]);
- withModifierKeys({ shift: true }, () => {
- mouse.click();
- });
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- expect(h.elements.map((element) => element.id)).toEqual([
- ids[1],
- ids[0],
- ids[2],
- ]);
- });
- it("supports nested groups", () => {
- const positions: number[][] = [];
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- positions.push(mouse.getPosition());
- withModifierKeys({ ctrl: true }, () => {
- keyPress("a");
- keyPress("g");
- });
- mouse.doubleClick();
- withModifierKeys({ shift: true }, () => {
- mouse.restorePosition(...positions[0]);
- mouse.click();
- });
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- const groupIds = h.elements[2].groupIds;
- expect(groupIds.length).toBe(2);
- expect(h.elements[1].groupIds).toEqual(groupIds);
- expect(h.elements[0].groupIds).toEqual(groupIds.slice(1));
- mouse.click(50, 50);
- expect(getSelectedElements().length).toBe(0);
- mouse.restorePosition(...positions[0]);
- mouse.click();
- expect(getSelectedElements().length).toBe(3);
- expect(h.state.editingGroupId).toBe(null);
- mouse.doubleClick();
- expect(getSelectedElements().length).toBe(2);
- expect(h.state.editingGroupId).toBe(groupIds[1]);
- mouse.doubleClick();
- expect(getSelectedElements().length).toBe(1);
- expect(h.state.editingGroupId).toBe(groupIds[0]);
- // click out of the group
- mouse.restorePosition(...positions[1]);
- mouse.click();
- expect(getSelectedElements().length).toBe(0);
- mouse.click();
- expect(getSelectedElements().length).toBe(3);
- mouse.doubleClick();
- expect(getSelectedElements().length).toBe(1);
- });
- it("updates fontSize & fontFamily appState", () => {
- clickTool("text");
- expect(h.state.currentItemFontFamily).toEqual(1); // Virgil
- fireEvent.click(screen.getByText(/code/i));
- expect(h.state.currentItemFontFamily).toEqual(3); // Cascadia
- });
- it("shows context menu for canvas", () => {
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- const options = contextMenu?.querySelectorAll(".context-menu-option");
- const expectedOptions = ["Select all", "Toggle grid mode"];
- expect(contextMenu).not.toBeNull();
- expect(options?.length).toBe(2);
- expect(options?.item(0).textContent).toBe(expectedOptions[0]);
- });
- it("shows context menu for element", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- const options = contextMenu?.querySelectorAll(".context-menu-option");
- const expectedOptions = [
- "Copy styles",
- "Paste styles",
- "Delete",
- "Add to library",
- "Send backward",
- "Bring forward",
- "Send to back",
- "Bring to front",
- "Duplicate",
- ];
- expect(contextMenu).not.toBeNull();
- expect(contextMenu?.children.length).toBe(9);
- options?.forEach((opt, i) => {
- expect(opt.textContent).toBe(expectedOptions[i]);
- });
- });
- it("shows 'Group selection' in context menu for multiple selected elements", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- mouse.reset();
- mouse.click(10, 10);
- withModifierKeys({ shift: true }, () => {
- mouse.click(20, 0);
- });
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- const options = contextMenu?.querySelectorAll(".context-menu-option");
- const expectedOptions = [
- "Copy styles",
- "Paste styles",
- "Delete",
- "Group selection",
- "Add to library",
- "Send backward",
- "Bring forward",
- "Send to back",
- "Bring to front",
- "Duplicate",
- ];
- expect(contextMenu).not.toBeNull();
- expect(contextMenu?.children.length).toBe(10);
- options?.forEach((opt, i) => {
- expect(opt.textContent).toBe(expectedOptions[i]);
- });
- });
- it("shows 'Ungroup selection' in context menu for group inside selected elements", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(10, 10);
- clickTool("rectangle");
- mouse.down(10, -10);
- mouse.up(10, 10);
- mouse.reset();
- mouse.click(10, 10);
- withModifierKeys({ shift: true }, () => {
- mouse.click(20, 0);
- });
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- const options = contextMenu?.querySelectorAll(".context-menu-option");
- const expectedOptions = [
- "Copy styles",
- "Paste styles",
- "Delete",
- "Ungroup selection",
- "Add to library",
- "Send backward",
- "Bring forward",
- "Send to back",
- "Bring to front",
- "Duplicate",
- ];
- expect(contextMenu).not.toBeNull();
- expect(contextMenu?.children.length).toBe(10);
- options?.forEach((opt, i) => {
- expect(opt.textContent).toBe(expectedOptions[i]);
- });
- });
- it("selecting 'Copy styles' in context menu copies styles", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- expect(copiedStyles).toBe("{}");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Copy styles")!);
- expect(copiedStyles).not.toBe("{}");
- const element = JSON.parse(copiedStyles);
- expect(element).toEqual(getSelectedElement());
- });
- it("selecting 'Paste styles' in context menu pastes styles", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- // Change some styles of second rectangle
- clickLabeledElement("Stroke");
- clickLabeledElement("#c92a2a");
- clickLabeledElement("Background");
- clickLabeledElement("#e64980");
- // Fill style
- fireEvent.click(screen.getByLabelText("Cross-hatch"));
- // Stroke width
- fireEvent.click(screen.getByLabelText("Bold"));
- // Stroke style
- fireEvent.click(screen.getByLabelText("Dotted"));
- // Roughness
- fireEvent.click(screen.getByLabelText("Cartoonist"));
- // Opacity
- fireEvent.change(screen.getByLabelText("Opacity"), {
- target: { value: "60" },
- });
- mouse.reset();
- // Copy styles of second rectangle
- fireEvent.contextMenu(canvas, { button: 2, clientX: 40, clientY: 40 });
- let contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Copy styles")!);
- const secondRect = JSON.parse(copiedStyles);
- expect(secondRect.id).toBe(h.elements[1].id);
- mouse.reset();
- // Paste styles to first rectangle
- fireEvent.contextMenu(canvas, { button: 2, clientX: 10, clientY: 10 });
- contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Paste styles")!);
- const firstRect = getSelectedElement();
- expect(firstRect.id).toBe(h.elements[0].id);
- expect(firstRect.strokeColor).toBe("#c92a2a");
- expect(firstRect.backgroundColor).toBe("#e64980");
- expect(firstRect.fillStyle).toBe("cross-hatch");
- expect(firstRect.strokeWidth).toBe(2); // Bold: 2
- expect(firstRect.strokeStyle).toBe("dotted");
- expect(firstRect.roughness).toBe(2); // Cartoonist: 2
- expect(firstRect.opacity).toBe(60);
- });
- it("selecting 'Delete' in context menu deletes element", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Delete")!);
- expect(getSelectedElements()).toHaveLength(0);
- expect(h.elements[0].isDeleted).toBe(true);
- });
- it("selecting 'Add to library' in context menu adds element to library", async () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Add to library")!);
- await waitFor(() => {
- const library = localStorage.getItem("excalidraw-library");
- expect(library).not.toBeNull();
- const addedElement = JSON.parse(library!)[0][0];
- expect(addedElement).toEqual(h.elements[0]);
- });
- });
- it("selecting 'Duplicate' in context menu duplicates element", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Duplicate")!);
- expect(h.elements).toHaveLength(2);
- const { id: _id0, seed: _seed0, x: _x0, y: _y0, ...rect1 } = h.elements[0];
- const { id: _id1, seed: _seed1, x: _x1, y: _y1, ...rect2 } = h.elements[1];
- expect(rect1).toEqual(rect2);
- });
- it("selecting 'Send backward' in context menu sends element backward", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- fireEvent.contextMenu(canvas, { button: 2, clientX: 40, clientY: 40 });
- const contextMenu = document.querySelector(".context-menu");
- const elementsBefore = h.elements;
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Send backward")!);
- expect(elementsBefore[0].id).toEqual(h.elements[1].id);
- expect(elementsBefore[1].id).toEqual(h.elements[0].id);
- });
- it("selecting 'Bring forward' in context menu brings element forward", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- fireEvent.contextMenu(canvas, { button: 2, clientX: 10, clientY: 10 });
- const contextMenu = document.querySelector(".context-menu");
- const elementsBefore = h.elements;
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Bring forward")!);
- expect(elementsBefore[0].id).toEqual(h.elements[1].id);
- expect(elementsBefore[1].id).toEqual(h.elements[0].id);
- });
- it("selecting 'Send to back' in context menu sends element to back", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- fireEvent.contextMenu(canvas, { button: 2, clientX: 40, clientY: 40 });
- const contextMenu = document.querySelector(".context-menu");
- const elementsBefore = h.elements;
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Send to back")!);
- expect(elementsBefore[1].id).toEqual(h.elements[0].id);
- });
- it("selecting 'Bring to front' in context menu brings element to front", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- fireEvent.contextMenu(canvas, { button: 2, clientX: 10, clientY: 10 });
- const contextMenu = document.querySelector(".context-menu");
- const elementsBefore = h.elements;
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Bring to front")!);
- expect(elementsBefore[0].id).toEqual(h.elements[1].id);
- });
- it("selecting 'Group selection' in context menu groups selected elements", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(10, 10);
- });
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.click(
- queryByText(contextMenu as HTMLElement, "Group selection")!,
- );
- const selectedGroupIds = Object.keys(h.state.selectedGroupIds);
- expect(h.elements[0].groupIds).toEqual(selectedGroupIds);
- expect(h.elements[1].groupIds).toEqual(selectedGroupIds);
- });
- it("selecting 'Ungroup selection' in context menu ungroups selected group", () => {
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(20, 20);
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(10, 10);
- });
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- fireEvent.contextMenu(canvas, { button: 2, clientX: 1, clientY: 1 });
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.click(
- queryByText(contextMenu as HTMLElement, "Ungroup selection")!,
- );
- const selectedGroupIds = Object.keys(h.state.selectedGroupIds);
- expect(selectedGroupIds).toHaveLength(0);
- expect(h.elements[0].groupIds).toHaveLength(0);
- expect(h.elements[1].groupIds).toHaveLength(0);
- });
- it("deselects selected element, on pointer up, when click hits element bounding box but doesn't hit the element", () => {
- clickTool("ellipse");
- mouse.down();
- mouse.up(100, 100);
- // hits bounding box without hitting element
- mouse.down();
- expect(getSelectedElements().length).toBe(1);
- mouse.up();
- expect(getSelectedElements().length).toBe(0);
- });
- it("switches selected element on pointer down", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(10, 10);
- mouse.up(10, 10);
- expect(getSelectedElement().type).toBe("ellipse");
- // pointer down on rectangle
- mouse.reset();
- mouse.down();
- expect(getSelectedElement().type).toBe("rectangle");
- });
- it("can drag element that covers another element, while another elem is selected", () => {
- clickTool("rectangle");
- mouse.down(100, 100);
- mouse.up(200, 200);
- clickTool("rectangle");
- mouse.reset();
- mouse.down(100, 100);
- mouse.up(200, 200);
- clickTool("ellipse");
- mouse.reset();
- mouse.down(300, 300);
- mouse.up(350, 350);
- expect(getSelectedElement().type).toBe("ellipse");
- // pointer down on rectangle
- mouse.reset();
- mouse.down(100, 100);
- mouse.up(200, 200);
- expect(getSelectedElement().type).toBe("rectangle");
- });
- it("deselects selected element on pointer down when pointer doesn't hit any element", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- expect(getSelectedElements().length).toBe(1);
- // pointer down on space without elements
- mouse.down(100, 100);
- expect(getSelectedElements().length).toBe(0);
- });
- it("Drags selected element when hitting only bounding box and keeps element selected", () => {
- clickTool("ellipse");
- mouse.down();
- mouse.up(10, 10);
- const { x: prevX, y: prevY } = getSelectedElement();
- // drag element from point on bounding box that doesn't hit element
- mouse.reset();
- mouse.down();
- mouse.up(25, 25);
- expect(getSelectedElement().x).toEqual(prevX + 25);
- expect(getSelectedElement().y).toEqual(prevY + 25);
- });
- it(
- "given selected element A with lower z-index than unselected element B and given B is partially over A " +
- "when clicking intersection between A and B " +
- "B should be selected on pointer up",
- () => {
- clickTool("rectangle");
- // change background color since default is transparent
- // and transparent elements can't be selected by clicking inside of them
- clickLabeledElement("Background");
- clickLabeledElement("#fa5252");
- mouse.down();
- mouse.up(1000, 1000);
- // draw ellipse partially over rectangle.
- // since ellipse was created after rectangle it has an higher z-index.
- // we don't need to change background color again since change above
- // affects next drawn elements.
- clickTool("ellipse");
- mouse.reset();
- mouse.down(500, 500);
- mouse.up(1000, 1000);
- // select rectangle
- mouse.reset();
- mouse.click();
- // pointer down on intersection between ellipse and rectangle
- mouse.down(900, 900);
- expect(getSelectedElement().type).toBe("rectangle");
- mouse.up();
- expect(getSelectedElement().type).toBe("ellipse");
- },
- );
- it(
- "given selected element A with lower z-index than unselected element B and given B is partially over A " +
- "when dragging on intersection between A and B " +
- "A should be dragged and keep being selected",
- () => {
- clickTool("rectangle");
- // change background color since default is transparent
- // and transparent elements can't be selected by clicking inside of them
- clickLabeledElement("Background");
- clickLabeledElement("#fa5252");
- mouse.down();
- mouse.up(1000, 1000);
- // draw ellipse partially over rectangle.
- // since ellipse was created after rectangle it has an higher z-index.
- // we don't need to change background color again since change above
- // affects next drawn elements.
- clickTool("ellipse");
- mouse.reset();
- mouse.down(500, 500);
- mouse.up(1000, 1000);
- // select rectangle
- mouse.reset();
- mouse.click();
- const { x: prevX, y: prevY } = getSelectedElement();
- // pointer down on intersection between ellipse and rectangle
- mouse.down(900, 900);
- mouse.up(100, 100);
- expect(getSelectedElement().type).toBe("rectangle");
- expect(getSelectedElement().x).toEqual(prevX + 100);
- expect(getSelectedElement().y).toEqual(prevY + 100);
- },
- );
- it("deselects group of selected elements on pointer down when pointer doesn't hit any element", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(100, 100);
- mouse.up(10, 10);
- // Selects first element without deselecting the second element
- // Second element is already selected because creating it was our last action
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(5, 5);
- });
- expect(getSelectedElements().length).toBe(2);
- // pointer down on space without elements
- mouse.reset();
- mouse.down(500, 500);
- expect(getSelectedElements().length).toBe(0);
- });
- it("switches from group of selected elements to another element on pointer down", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(100, 100);
- mouse.up(100, 100);
- clickTool("diamond");
- mouse.down(100, 100);
- mouse.up(100, 100);
- // Selects ellipse without deselecting the diamond
- // Diamond is already selected because creating it was our last action
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(110, 160);
- });
- expect(getSelectedElements().length).toBe(2);
- // select rectangle
- mouse.reset();
- mouse.down();
- expect(getSelectedElement().type).toBe("rectangle");
- });
- it("deselects group of selected elements on pointer up when pointer hits common bounding box without hitting any element", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(100, 100);
- mouse.up(10, 10);
- // Selects first element without deselecting the second element
- // Second element is already selected because creating it was our last action
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(5, 5);
- });
- // pointer down on common bounding box without hitting any of the elements
- mouse.reset();
- mouse.down(50, 50);
- expect(getSelectedElements().length).toBe(2);
- mouse.up();
- expect(getSelectedElements().length).toBe(0);
- });
- it(
- "drags selected elements from point inside common bounding box that doesn't hit any element " +
- "and keeps elements selected after dragging",
- () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(100, 100);
- mouse.up(10, 10);
- // Selects first element without deselecting the second element
- // Second element is already selected because creating it was our last action
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click(5, 5);
- });
- expect(getSelectedElements().length).toBe(2);
- const {
- x: firstElementPrevX,
- y: firstElementPrevY,
- } = getSelectedElements()[0];
- const {
- x: secondElementPrevX,
- y: secondElementPrevY,
- } = getSelectedElements()[1];
- // drag elements from point on common bounding box that doesn't hit any of the elements
- mouse.reset();
- mouse.down(50, 50);
- mouse.up(25, 25);
- expect(getSelectedElements()[0].x).toEqual(firstElementPrevX + 25);
- expect(getSelectedElements()[0].y).toEqual(firstElementPrevY + 25);
- expect(getSelectedElements()[1].x).toEqual(secondElementPrevX + 25);
- expect(getSelectedElements()[1].y).toEqual(secondElementPrevY + 25);
- expect(getSelectedElements().length).toBe(2);
- },
- );
- it(
- "given a group of selected elements with an element that is not selected inside the group common bounding box " +
- "when element that is not selected is clicked " +
- "should switch selection to not selected element on pointer up",
- () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- clickTool("ellipse");
- mouse.down(100, 100);
- mouse.up(100, 100);
- clickTool("diamond");
- mouse.down(100, 100);
- mouse.up(100, 100);
- // Selects rectangle without deselecting the diamond
- // Diamond is already selected because creating it was our last action
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click();
- });
- // pointer down on ellipse
- mouse.down(110, 160);
- expect(getSelectedElements().length).toBe(2);
- mouse.up();
- expect(getSelectedElement().type).toBe("ellipse");
- },
- );
- it(
- "given a selected element A and a not selected element B with higher z-index than A " +
- "and given B partialy overlaps A " +
- "when there's a shift-click on the overlapped section B is added to the selection",
- () => {
- clickTool("rectangle");
- // change background color since default is transparent
- // and transparent elements can't be selected by clicking inside of them
- clickLabeledElement("Background");
- clickLabeledElement("#fa5252");
- mouse.down();
- mouse.up(1000, 1000);
- // draw ellipse partially over rectangle.
- // since ellipse was created after rectangle it has an higher z-index.
- // we don't need to change background color again since change above
- // affects next drawn elements.
- clickTool("ellipse");
- mouse.reset();
- mouse.down(500, 500);
- mouse.up(1000, 1000);
- // select rectangle
- mouse.reset();
- mouse.click();
- // click on intersection between ellipse and rectangle
- withModifierKeys({ shift: true }, () => {
- mouse.click(900, 900);
- });
- expect(getSelectedElements().length).toBe(2);
- },
- );
- it("shift click on selected element should deselect it on pointer up", () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(10, 10);
- // Rectangle is already selected since creating
- // it was our last action
- withModifierKeys({ shift: true }, () => {
- mouse.down();
- });
- expect(getSelectedElements().length).toBe(1);
- withModifierKeys({ shift: true }, () => {
- mouse.up();
- });
- expect(getSelectedElements().length).toBe(0);
- });
- it("single-clicking on a subgroup of a selected group should not alter selection", () => {
- const rect1 = createElement("rectangle", { x: 10 });
- const rect2 = createElement("rectangle", { x: 50 });
- group([rect1, rect2]);
- const rect3 = createElement("rectangle", { x: 10, y: 50 });
- const rect4 = createElement("rectangle", { x: 50, y: 50 });
- group([rect3, rect4]);
- withModifierKeys({ ctrl: true }, () => {
- keyPress("a");
- keyPress("g");
- });
- const selectedGroupIds_prev = h.state.selectedGroupIds;
- const selectedElements_prev = getSelectedElements();
- mouse.clickOn(rect3);
- expect(h.state.selectedGroupIds).toEqual(selectedGroupIds_prev);
- expect(getSelectedElements()).toEqual(selectedElements_prev);
- });
- it("Cmd/Ctrl-click exclusively select element under pointer", () => {
- const rect1 = createElement("rectangle", { x: 0 });
- const rect2 = createElement("rectangle", { x: 30 });
- group([rect1, rect2]);
- assertSelectedElements(rect1, rect2);
- withModifierKeys({ ctrl: true }, () => {
- mouse.clickOn(rect1);
- });
- assertSelectedElements(rect1);
- clearSelection();
- withModifierKeys({ ctrl: true }, () => {
- mouse.clickOn(rect1);
- });
- assertSelectedElements(rect1);
- const rect3 = createElement("rectangle", { x: 60 });
- group([rect1, rect3]);
- assertSelectedElements(rect1, rect2, rect3);
- withModifierKeys({ ctrl: true }, () => {
- mouse.clickOn(rect1);
- });
- assertSelectedElements(rect1);
- clearSelection();
- withModifierKeys({ ctrl: true }, () => {
- mouse.clickOn(rect3);
- });
- assertSelectedElements(rect3);
- });
- });
- it(
- "given element A and group of elements B and given both are selected " +
- "when user clicks on B, on pointer up " +
- "only elements from B should be selected",
- () => {
- const rect1 = createElement("rectangle", { y: 0 });
- const rect2 = createElement("rectangle", { y: 30 });
- const rect3 = createElement("rectangle", { y: 60 });
- group([rect1, rect3]);
- expect(getSelectedElements().length).toBe(2);
- expect(Object.keys(h.state.selectedGroupIds).length).toBe(1);
- // Select second rectangle without deselecting group
- withModifierKeys({ shift: true }, () => {
- mouse.clickOn(rect2);
- });
- expect(getSelectedElements().length).toBe(3);
- // clicking on first rectangle that is part of the group should select
- // that group (exclusively)
- mouse.clickOn(rect1);
- expect(getSelectedElements().length).toBe(2);
- expect(Object.keys(h.state.selectedGroupIds).length).toBe(1);
- },
- );
- it(
- "given element A and group of elements B and given both are selected " +
- "when user shift-clicks on B, on pointer up " +
- "only element A should be selected",
- () => {
- clickTool("rectangle");
- mouse.down();
- mouse.up(100, 100);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(100, 100);
- clickTool("rectangle");
- mouse.down(10, 10);
- mouse.up(100, 100);
- // Select first rectangle while keeping third one selected.
- // Third rectangle is selected because it was the last element
- // to be created.
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.click();
- });
- // Create group with first and third rectangle
- withModifierKeys({ ctrl: true }, () => {
- keyPress("g");
- });
- expect(getSelectedElements().length).toBe(2);
- const selectedGroupIds = Object.keys(h.state.selectedGroupIds);
- expect(selectedGroupIds.length).toBe(1);
- // Select second rectangle without deselecting group
- withModifierKeys({ shift: true }, () => {
- mouse.click(110, 110);
- });
- expect(getSelectedElements().length).toBe(3);
- // pointer down o first rectangle that is
- // part of the group
- mouse.reset();
- withModifierKeys({ shift: true }, () => {
- mouse.down();
- });
- expect(getSelectedElements().length).toBe(3);
- withModifierKeys({ shift: true }, () => {
- mouse.up();
- });
- expect(getSelectedElements().length).toBe(1);
- },
- );
|