fix: add safe check for arrow points length in tranformToExcalidrawElements (#7863)

* fix: add safe check for arrow points length in tranformToExcalidrawElements

* add spec

* throw error only for dev mode

* fix lint
This commit is contained in:
Aakansha Doshi
2024-04-09 09:56:21 +05:30
committed by GitHub
parent 8a162a4cb4
commit a33a400f01
5 changed files with 721 additions and 39 deletions

View File

@@ -152,14 +152,14 @@ describe("Test Transform", () => {
strokeStyle: "dotted",
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(4);
expect(excalidrawElements.length).toBe(4);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -235,14 +235,14 @@ describe("Test Transform", () => {
},
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(12);
expect(excalidrawElements.length).toBe(12);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -293,14 +293,14 @@ describe("Test Transform", () => {
},
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(8);
expect(excalidrawElements.length).toBe(8);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -338,13 +338,13 @@ describe("Test Transform", () => {
name: "My frame",
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elementsSkeleton,
opts,
);
expect(excaldrawElements.length).toBe(4);
expect(excalidrawElements.length).toBe(4);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchObject({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -383,11 +383,11 @@ describe("Test Transform", () => {
height: 100,
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elementsSkeleton,
opts,
);
const frame = excaldrawElements.find((ele) => ele.type === "frame")!;
const frame = excalidrawElements.find((ele) => ele.type === "frame")!;
expect(frame.width).toBe(800);
expect(frame.height).toBe(126);
});
@@ -411,13 +411,13 @@ describe("Test Transform", () => {
},
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(4);
const [arrow, text, rectangle, ellipse] = excaldrawElements;
expect(excalidrawElements.length).toBe(4);
const [arrow, text, rectangle, ellipse] = excalidrawElements;
expect(arrow).toMatchObject({
type: "arrow",
x: 255,
@@ -466,7 +466,7 @@ describe("Test Transform", () => {
],
});
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -495,13 +495,13 @@ describe("Test Transform", () => {
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(4);
const [arrow, text1, text2, text3] = excaldrawElements;
expect(excalidrawElements.length).toBe(4);
const [arrow, text1, text2, text3] = excalidrawElements;
expect(arrow).toMatchObject({
type: "arrow",
@@ -551,7 +551,7 @@ describe("Test Transform", () => {
],
});
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -611,14 +611,14 @@ describe("Test Transform", () => {
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(5);
expect(excalidrawElements.length).toBe(5);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -660,14 +660,14 @@ describe("Test Transform", () => {
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(4);
expect(excalidrawElements.length).toBe(4);
excaldrawElements.forEach((ele) => {
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
@@ -714,13 +714,13 @@ describe("Test Transform", () => {
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(4);
const [, , arrow, text] = excaldrawElements;
expect(excalidrawElements.length).toBe(4);
const [, , arrow, text] = excalidrawElements;
expect(arrow).toMatchObject({
type: "arrow",
x: 255,
@@ -765,12 +765,12 @@ describe("Test Transform", () => {
backgroundColor: "#bac8ff",
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(2);
const [arrow, rect] = excaldrawElements;
expect(excalidrawElements.length).toBe(2);
const [arrow, rect] = excalidrawElements;
expect((arrow as ExcalidrawArrowElement).endBinding).toStrictEqual({
elementId: "rect-1",
focus: 0,
@@ -808,13 +808,13 @@ describe("Test Transform", () => {
height: 200,
},
];
const excaldrawElements = convertToExcalidrawElements(
const excalidrawElements = convertToExcalidrawElements(
elements as ExcalidrawElementSkeleton[],
opts,
);
expect(excaldrawElements.length).toBe(1);
expect(excaldrawElements[0]).toMatchSnapshot({
expect(excalidrawElements.length).toBe(1);
expect(excalidrawElements[0]).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
});
@@ -840,4 +840,130 @@ describe("Test Transform", () => {
createdBy: "user01",
});
});
it("should transform the elements correctly when linear elements have single point", () => {
const elements: ExcalidrawElementSkeleton[] = [
{
id: "B",
type: "rectangle",
groupIds: ["subgraph_group_B"],
x: 0,
y: 0,
width: 166.03125,
height: 163,
label: {
groupIds: ["subgraph_group_B"],
text: "B",
fontSize: 20,
verticalAlign: "top",
},
},
{
id: "A",
type: "rectangle",
groupIds: ["subgraph_group_A"],
x: 364.546875,
y: 0,
width: 120.265625,
height: 114,
label: {
groupIds: ["subgraph_group_A"],
text: "A",
fontSize: 20,
verticalAlign: "top",
},
},
{
id: "Alice",
type: "rectangle",
groupIds: ["subgraph_group_A"],
x: 389.546875,
y: 35,
width: 70.265625,
height: 44,
strokeWidth: 2,
label: {
groupIds: ["subgraph_group_A"],
text: "Alice",
fontSize: 20,
},
link: null,
},
{
id: "Bob",
type: "rectangle",
groupIds: ["subgraph_group_B"],
x: 54.76953125,
y: 35,
width: 56.4921875,
height: 44,
strokeWidth: 2,
label: {
groupIds: ["subgraph_group_B"],
text: "Bob",
fontSize: 20,
},
link: null,
},
{
id: "Bob_Alice",
type: "arrow",
groupIds: [],
x: 111.262,
y: 57,
strokeWidth: 2,
points: [
[0, 0],
[272.985, 0],
],
label: {
text: "How are you?",
fontSize: 20,
groupIds: [],
},
roundness: {
type: 2,
},
start: {
id: "Bob",
},
end: {
id: "Alice",
},
},
{
id: "Bob_B",
type: "arrow",
groupIds: [],
x: 77.017,
y: 79,
strokeWidth: 2,
points: [[0, 0]],
label: {
text: "Friendship",
fontSize: 20,
groupIds: [],
},
roundness: {
type: 2,
},
start: {
id: "Bob",
},
end: {
id: "B",
},
},
];
const excalidrawElements = convertToExcalidrawElements(elements, opts);
expect(excalidrawElements.length).toBe(12);
excalidrawElements.forEach((ele) => {
expect(ele).toMatchSnapshot({
seed: expect.any(Number),
versionNonce: expect.any(Number),
id: expect.any(String),
});
});
});
});