sizeHelpers.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { getPerfectElementSize } from "./sizeHelpers";
  2. import * as constants from "../constants";
  3. const EPSILON_DIGITS = 3;
  4. describe("getPerfectElementSize", () => {
  5. it("should return height:0 if `elementType` is line and locked angle is 0", () => {
  6. const { height, width } = getPerfectElementSize("line", 149, 10);
  7. expect(width).toBeCloseTo(149, EPSILON_DIGITS);
  8. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  9. });
  10. it("should return width:0 if `elementType` is line and locked angle is 90 deg (Math.PI/2)", () => {
  11. const { height, width } = getPerfectElementSize("line", 10, 140);
  12. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  13. expect(height).toBeCloseTo(140, EPSILON_DIGITS);
  14. });
  15. it("should return height:0 if `elementType` is arrow and locked angle is 0", () => {
  16. const { height, width } = getPerfectElementSize("arrow", 200, 20);
  17. expect(width).toBeCloseTo(200, EPSILON_DIGITS);
  18. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  19. });
  20. it("should return width:0 if `elementType` is arrow and locked angle is 90 deg (Math.PI/2)", () => {
  21. const { height, width } = getPerfectElementSize("arrow", 10, 100);
  22. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  23. expect(height).toBeCloseTo(100, EPSILON_DIGITS);
  24. });
  25. it("should return adjust height to be width * tan(locked angle)", () => {
  26. const { height, width } = getPerfectElementSize("arrow", 120, 185);
  27. expect(width).toBeCloseTo(120, EPSILON_DIGITS);
  28. expect(height).toBeCloseTo(207.846, EPSILON_DIGITS);
  29. });
  30. it("should return height equals to width if locked angle is 45 deg", () => {
  31. const { height, width } = getPerfectElementSize("arrow", 135, 145);
  32. expect(width).toBeCloseTo(135, EPSILON_DIGITS);
  33. expect(height).toBeCloseTo(135, EPSILON_DIGITS);
  34. });
  35. it("should return height:0 and width:0 when width and height are 0", () => {
  36. const { height, width } = getPerfectElementSize("arrow", 0, 0);
  37. expect(width).toBeCloseTo(0, EPSILON_DIGITS);
  38. expect(height).toBeCloseTo(0, EPSILON_DIGITS);
  39. });
  40. describe("should respond to SHIFT_LOCKING_ANGLE constant", () => {
  41. it("should have only 2 locking angles per section if SHIFT_LOCKING_ANGLE = 45 deg (Math.PI/4)", () => {
  42. (constants as any).SHIFT_LOCKING_ANGLE = Math.PI / 4;
  43. const { height, width } = getPerfectElementSize("arrow", 120, 185);
  44. expect(width).toBeCloseTo(120, EPSILON_DIGITS);
  45. expect(height).toBeCloseTo(120, EPSILON_DIGITS);
  46. });
  47. });
  48. });