2020-03-17 20:55:40 +01:00
|
|
|
import {
|
|
|
|
newTextElement,
|
|
|
|
duplicateElement,
|
|
|
|
newLinearElement,
|
|
|
|
} from "./newElement";
|
|
|
|
import { mutateElement } from "./mutateElement";
|
2020-02-19 22:28:11 +01:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const isPrimitive = (val: any) => {
|
2020-02-19 22:28:11 +01:00
|
|
|
const type = typeof val;
|
|
|
|
return val == null || (type !== "object" && type !== "function");
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-19 22:28:11 +01:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const assertCloneObjects = (source: any, clone: any) => {
|
2020-02-19 22:28:11 +01:00
|
|
|
for (const key in clone) {
|
|
|
|
if (clone.hasOwnProperty(key) && !isPrimitive(clone[key])) {
|
|
|
|
expect(clone[key]).not.toBe(source[key]);
|
|
|
|
if (source[key]) {
|
|
|
|
assertCloneObjects(source[key], clone[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-19 22:28:11 +01:00
|
|
|
|
|
|
|
it("clones arrow element", () => {
|
2020-03-17 20:55:40 +01:00
|
|
|
const element = newLinearElement({
|
|
|
|
type: "arrow",
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
strokeColor: "#000000",
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
fillStyle: "hachure",
|
|
|
|
strokeWidth: 1,
|
2020-05-14 17:04:33 +02:00
|
|
|
strokeStyle: "solid",
|
2020-03-17 20:55:40 +01:00
|
|
|
roughness: 1,
|
|
|
|
opacity: 100,
|
|
|
|
});
|
2020-02-19 22:28:11 +01:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
element.__proto__ = { hello: "world" };
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
mutateElement(element, {
|
|
|
|
points: [
|
|
|
|
[1, 2],
|
|
|
|
[3, 4],
|
|
|
|
],
|
|
|
|
});
|
2020-02-19 22:28:11 +01:00
|
|
|
|
|
|
|
const copy = duplicateElement(element);
|
|
|
|
|
|
|
|
assertCloneObjects(element, copy);
|
|
|
|
|
2020-03-14 21:12:39 -07:00
|
|
|
// @ts-ignore
|
2020-02-19 22:28:11 +01:00
|
|
|
expect(copy.__proto__).toEqual({ hello: "world" });
|
|
|
|
expect(copy.hasOwnProperty("hello")).toBe(false);
|
|
|
|
|
|
|
|
expect(copy.points).not.toBe(element.points);
|
|
|
|
expect(copy).not.toHaveProperty("shape");
|
|
|
|
expect(copy.id).not.toBe(element.id);
|
|
|
|
expect(typeof copy.id).toBe("string");
|
|
|
|
expect(copy.seed).not.toBe(element.seed);
|
|
|
|
expect(typeof copy.seed).toBe("number");
|
|
|
|
expect(copy).toEqual({
|
|
|
|
...element,
|
|
|
|
id: copy.id,
|
|
|
|
seed: copy.seed,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("clones text element", () => {
|
2020-03-17 20:55:40 +01:00
|
|
|
const element = newTextElement({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
strokeColor: "#000000",
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
fillStyle: "hachure",
|
|
|
|
strokeWidth: 1,
|
2020-05-14 17:04:33 +02:00
|
|
|
strokeStyle: "solid",
|
2020-03-17 20:55:40 +01:00
|
|
|
roughness: 1,
|
|
|
|
opacity: 100,
|
|
|
|
text: "hello",
|
|
|
|
font: "Arial 20px",
|
2020-04-08 21:00:27 +01:00
|
|
|
textAlign: "left",
|
2020-03-17 20:55:40 +01:00
|
|
|
});
|
2020-02-19 22:28:11 +01:00
|
|
|
|
|
|
|
const copy = duplicateElement(element);
|
|
|
|
|
|
|
|
assertCloneObjects(element, copy);
|
|
|
|
|
2020-03-17 20:55:40 +01:00
|
|
|
expect(copy).not.toHaveProperty("points");
|
2020-02-19 22:28:11 +01:00
|
|
|
expect(copy).not.toHaveProperty("shape");
|
|
|
|
expect(copy.id).not.toBe(element.id);
|
|
|
|
expect(typeof copy.id).toBe("string");
|
|
|
|
expect(typeof copy.seed).toBe("number");
|
|
|
|
});
|