sizeHelpers.test.ts 2.3 KB

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