sizeHelpers.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { vi } from "vitest";
  2. import * as constants from "@excalidraw/common";
  3. import { getPerfectElementSize } from "../src/sizeHelpers";
  4. const EPSILON_DIGITS = 3;
  5. // Needed so that we can mock the value of constants which is done in
  6. // below tests. In Jest this wasn't needed as global override was possible
  7. // but vite doesn't allow that hence we need to mock
  8. vi.mock(
  9. "@excalidraw/common",
  10. //@ts-ignore
  11. async (importOriginal) => {
  12. const module: any = await importOriginal();
  13. return { ...module };
  14. },
  15. );
  16. describe("getPerfectElementSize", () => {
  17. it("should return height:0 if `elementType` is line and locked angle is 0", () => {
  18. const { height, width } = getPerfectElementSize("line", 149, 10);
  19. expect(width).toBeCloseTo(149, EPSILON_DIGITS);
  20. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  21. });
  22. it("should return width:0 if `elementType` is line and locked angle is 90 deg (Math.PI/2)", () => {
  23. const { height, width } = getPerfectElementSize("line", 10, 140);
  24. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  25. expect(height).toBeCloseTo(140, EPSILON_DIGITS);
  26. });
  27. it("should return height:0 if `elementType` is arrow and locked angle is 0", () => {
  28. const { height, width } = getPerfectElementSize("arrow", 200, 20);
  29. expect(width).toBeCloseTo(200, EPSILON_DIGITS);
  30. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  31. });
  32. it("should return width:0 if `elementType` is arrow and locked angle is 90 deg (Math.PI/2)", () => {
  33. const { height, width } = getPerfectElementSize("arrow", 10, 100);
  34. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  35. expect(height).toBeCloseTo(100, EPSILON_DIGITS);
  36. });
  37. it("should return adjust height to be width * tan(locked angle)", () => {
  38. const { height, width } = getPerfectElementSize("arrow", 120, 185);
  39. expect(width).toBeCloseTo(120, EPSILON_DIGITS);
  40. expect(height).toBeCloseTo(207.846, EPSILON_DIGITS);
  41. });
  42. it("should return height equals to width if locked angle is 45 deg", () => {
  43. const { height, width } = getPerfectElementSize("arrow", 135, 145);
  44. expect(width).toBeCloseTo(135, EPSILON_DIGITS);
  45. expect(height).toBeCloseTo(135, EPSILON_DIGITS);
  46. });
  47. it("should return height:0 and width:0 when width and height are 0", () => {
  48. const { height, width } = getPerfectElementSize("arrow", 0, 0);
  49. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  50. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  51. });
  52. describe("should respond to SHIFT_LOCKING_ANGLE constant", () => {
  53. it("should have only 2 locking angles per section if SHIFT_LOCKING_ANGLE = 45 deg (Math.PI/4)", () => {
  54. (constants as any).SHIFT_LOCKING_ANGLE = Math.PI / 4;
  55. const { height, width } = getPerfectElementSize("arrow", 120, 185);
  56. expect(width).toBeCloseTo(120, EPSILON_DIGITS);
  57. expect(height).toBeCloseTo(120, EPSILON_DIGITS);
  58. });
  59. });
  60. });