Add tests

This commit is contained in:
Aakansha Doshi 2023-03-06 16:47:08 +05:30
parent 357a1c47f6
commit 9025ad99fc
2 changed files with 265 additions and 106 deletions

View File

@ -6,6 +6,7 @@ import {
getMaxContainerWidth, getMaxContainerWidth,
getMaxContainerHeight, getMaxContainerHeight,
wrapText, wrapText,
computeContainerCoords,
} from "./textElement"; } from "./textElement";
import { FontString } from "./types"; import { FontString } from "./types";
@ -177,7 +178,6 @@ break it now`,
}); });
}); });
describe("Test measureText", () => {
describe("Test computeBoundTextElementCoords", () => { describe("Test computeBoundTextElementCoords", () => {
const params = { width: 200, height: 100, x: 10, y: 20 }; const params = { width: 200, height: 100, x: 10, y: 20 };
@ -215,6 +215,36 @@ describe("Test measureText", () => {
}); });
}); });
describe("Test computeContainerCoords", () => {
const boundTextElement = API.createElement({
type: "text",
width: 200,
height: 100,
x: 10,
y: 20,
});
it("should compute coords correctly when ellipse", () => {
expect(computeContainerCoords(boundTextElement, "ellipse")).toEqual({
x: -24.289321881345245,
y: 0.3553390593273775,
});
});
it("should compute coords correctly when rectangle", () => {
expect(computeContainerCoords(boundTextElement, "rectangle")).toEqual({
x: 5,
y: 15,
});
});
it("should compute coords correctly when diamond", () => {
expect(computeContainerCoords(boundTextElement, "diamond")).toEqual({
x: -45,
y: -10,
});
});
});
describe("Test computeContainerDimensionForBoundText", () => { describe("Test computeContainerDimensionForBoundText", () => {
const params = { const params = {
width: 178, width: 178,
@ -295,4 +325,3 @@ describe("Test measureText", () => {
expect(getMaxContainerHeight(container)).toBe(87); expect(getMaxContainerHeight(container)).toBe(87);
}); });
}); });
});

View File

@ -989,6 +989,136 @@ describe("textWysiwyg", () => {
]); ]);
}); });
describe.only("when using shift resize", () => {
it("should wrap text correctly when resizing using shift from 'ne' handle", async () => {
Keyboard.keyPress(KEYS.ENTER);
const editor = document.querySelector(
".excalidraw-textEditorContainer > textarea",
) as HTMLTextAreaElement;
await new Promise((r) => setTimeout(r, 0));
fireEvent.change(editor, {
target: { value: "Excalidraw is an opensource virtual whiteboard" },
});
editor.blur();
const textElement = h.elements[1] as ExcalidrawTextElement;
expect(rectangle.width).toBe(90);
expect(rectangle.height).toBe(202);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excalid
raw is
an
opensou
rce
virtual
whitebo
ard`,
);
resize(rectangle, "ne", [rectangle.x + 100, rectangle.y - 50], {
shift: true,
});
expect(rectangle.width).toBe(200);
expect(rectangle.height).toBe(449);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excalidraw is an
opensource virtual
whiteboard`,
);
});
it("should wrap text correctly when resizing using shift vertically using 'n' handle", async () => {
Keyboard.keyPress(KEYS.ENTER);
const editor = document.querySelector(
".excalidraw-textEditorContainer > textarea",
) as HTMLTextAreaElement;
await new Promise((r) => setTimeout(r, 0));
fireEvent.change(editor, {
target: { value: "Excalidraw is an opensource virtual whiteboard" },
});
editor.blur();
const textElement = h.elements[1] as ExcalidrawTextElement;
expect(rectangle.width).toBe(90);
expect(rectangle.height).toBe(202);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excalid
raw is
an
opensou
rce
virtual
whitebo
ard`,
);
resize(rectangle, "n", [rectangle.x + 30, rectangle.y - 50], {
shift: true,
});
expect(rectangle.width).toBe(104);
expect(rectangle.height).toBe(232);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excalidra
w is an
opensourc
e virtual
whiteboar
d`,
);
});
it("should wrap text correctly when resizing using shift horizontally and text overflows", async () => {
Keyboard.keyPress(KEYS.ENTER);
const editor = document.querySelector(
".excalidraw-textEditorContainer > textarea",
) as HTMLTextAreaElement;
await new Promise((r) => setTimeout(r, 0));
fireEvent.change(editor, {
target: { value: "Excalidraw is an opensource virtual whiteboard" },
});
editor.blur();
const textElement = h.elements[1] as ExcalidrawTextElement;
expect(rectangle.width).toBe(90);
expect(rectangle.height).toBe(202);
expect(rectangle.y).toBe(20);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excalid
raw is
an
opensou
rce
virtual
whitebo
ard`,
);
resize(rectangle, "e", [rectangle.x - 30, rectangle.y + 30], {
shift: true,
});
expect(rectangle.width).toBe(70);
expect(rectangle.height).toBe(226);
expect(rectangle.y).toBe(8);
expect(textElement.fontSize).toBe(20);
expect(textElement.text).toBe(
`Excal
idraw
is an
opens
ource
virtu
al
white
board`,
);
});
});
it("should bind text correctly when container duplicated with alt-drag", async () => { it("should bind text correctly when container duplicated with alt-drag", async () => {
Keyboard.keyPress(KEYS.ENTER); Keyboard.keyPress(KEYS.ENTER);