newElement.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { duplicateElement } from "./newElement";
  2. import { mutateElement } from "./mutateElement";
  3. import { API } from "../tests/helpers/api";
  4. const isPrimitive = (val: any) => {
  5. const type = typeof val;
  6. return val == null || (type !== "object" && type !== "function");
  7. };
  8. const assertCloneObjects = (source: any, clone: any) => {
  9. for (const key in clone) {
  10. if (clone.hasOwnProperty(key) && !isPrimitive(clone[key])) {
  11. expect(clone[key]).not.toBe(source[key]);
  12. if (source[key]) {
  13. assertCloneObjects(source[key], clone[key]);
  14. }
  15. }
  16. }
  17. };
  18. it("clones arrow element", () => {
  19. const element = API.createElement({
  20. type: "arrow",
  21. x: 0,
  22. y: 0,
  23. strokeColor: "#000000",
  24. backgroundColor: "transparent",
  25. fillStyle: "hachure",
  26. strokeWidth: 1,
  27. strokeStyle: "solid",
  28. strokeSharpness: "round",
  29. roughness: 1,
  30. opacity: 100,
  31. });
  32. // @ts-ignore
  33. element.__proto__ = { hello: "world" };
  34. mutateElement(element, {
  35. points: [
  36. [1, 2],
  37. [3, 4],
  38. ],
  39. });
  40. const copy = duplicateElement(null, new Map(), element);
  41. assertCloneObjects(element, copy);
  42. // @ts-ignore
  43. expect(copy.__proto__).toEqual({ hello: "world" });
  44. expect(copy.hasOwnProperty("hello")).toBe(false);
  45. expect(copy.points).not.toBe(element.points);
  46. expect(copy).not.toHaveProperty("shape");
  47. expect(copy.id).not.toBe(element.id);
  48. expect(typeof copy.id).toBe("string");
  49. expect(copy.seed).not.toBe(element.seed);
  50. expect(typeof copy.seed).toBe("number");
  51. expect(copy).toEqual({
  52. ...element,
  53. id: copy.id,
  54. seed: copy.seed,
  55. });
  56. });
  57. it("clones text element", () => {
  58. const element = API.createElement({
  59. type: "text",
  60. x: 0,
  61. y: 0,
  62. strokeColor: "#000000",
  63. backgroundColor: "transparent",
  64. fillStyle: "hachure",
  65. strokeWidth: 1,
  66. strokeStyle: "solid",
  67. strokeSharpness: "round",
  68. roughness: 1,
  69. opacity: 100,
  70. text: "hello",
  71. fontSize: 20,
  72. fontFamily: 1,
  73. textAlign: "left",
  74. verticalAlign: "top",
  75. });
  76. const copy = duplicateElement(null, new Map(), element);
  77. assertCloneObjects(element, copy);
  78. expect(copy).not.toHaveProperty("points");
  79. expect(copy).not.toHaveProperty("shape");
  80. expect(copy.id).not.toBe(element.id);
  81. expect(typeof copy.id).toBe("string");
  82. expect(typeof copy.seed).toBe("number");
  83. });