newElement.test.ts 2.1 KB

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