distribute.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {
  2. distributeHorizontally,
  3. distributeVertically,
  4. } from "@excalidraw/excalidraw/actions";
  5. import { defaultLang, setLanguage } from "@excalidraw/excalidraw/i18n";
  6. import { Excalidraw } from "@excalidraw/excalidraw";
  7. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  8. import { UI, Pointer, Keyboard } from "@excalidraw/excalidraw/tests/helpers/ui";
  9. import {
  10. act,
  11. unmountComponent,
  12. render,
  13. } from "@excalidraw/excalidraw/tests/test-utils";
  14. const mouse = new Pointer("mouse");
  15. // Scenario: three rectangles that will be distributed with gaps
  16. const createAndSelectThreeRectanglesWithGap = () => {
  17. UI.clickTool("rectangle");
  18. mouse.down();
  19. mouse.up(100, 100);
  20. mouse.reset();
  21. UI.clickTool("rectangle");
  22. mouse.down(10, 10);
  23. mouse.up(100, 100);
  24. mouse.reset();
  25. UI.clickTool("rectangle");
  26. mouse.down(300, 300);
  27. mouse.up(100, 100);
  28. mouse.reset();
  29. // Last rectangle is selected by default
  30. Keyboard.withModifierKeys({ shift: true }, () => {
  31. mouse.click(0, 10);
  32. mouse.click(10, 0);
  33. });
  34. };
  35. // Scenario: three rectangles that will be distributed by their centers
  36. const createAndSelectThreeRectanglesWithoutGap = () => {
  37. UI.clickTool("rectangle");
  38. mouse.down();
  39. mouse.up(100, 100);
  40. mouse.reset();
  41. UI.clickTool("rectangle");
  42. mouse.down(10, 10);
  43. mouse.up(200, 200);
  44. mouse.reset();
  45. UI.clickTool("rectangle");
  46. mouse.down(200, 200);
  47. mouse.up(100, 100);
  48. mouse.reset();
  49. // Last rectangle is selected by default
  50. Keyboard.withModifierKeys({ shift: true }, () => {
  51. mouse.click(0, 10);
  52. mouse.click(10, 0);
  53. });
  54. };
  55. describe("distributing", () => {
  56. beforeEach(async () => {
  57. unmountComponent();
  58. mouse.reset();
  59. await act(() => {
  60. return setLanguage(defaultLang);
  61. });
  62. await render(<Excalidraw handleKeyboardGlobally={true} />);
  63. });
  64. it("should distribute selected elements horizontally", async () => {
  65. createAndSelectThreeRectanglesWithGap();
  66. expect(API.getSelectedElements()[0].x).toEqual(0);
  67. expect(API.getSelectedElements()[1].x).toEqual(10);
  68. expect(API.getSelectedElements()[2].x).toEqual(300);
  69. API.executeAction(distributeHorizontally);
  70. expect(API.getSelectedElements()[0].x).toEqual(0);
  71. expect(API.getSelectedElements()[1].x).toEqual(150);
  72. expect(API.getSelectedElements()[2].x).toEqual(300);
  73. });
  74. it("should distribute selected elements vertically", async () => {
  75. createAndSelectThreeRectanglesWithGap();
  76. expect(API.getSelectedElements()[0].y).toEqual(0);
  77. expect(API.getSelectedElements()[1].y).toEqual(10);
  78. expect(API.getSelectedElements()[2].y).toEqual(300);
  79. API.executeAction(distributeVertically);
  80. expect(API.getSelectedElements()[0].y).toEqual(0);
  81. expect(API.getSelectedElements()[1].y).toEqual(150);
  82. expect(API.getSelectedElements()[2].y).toEqual(300);
  83. });
  84. it("should distribute selected elements horizontally based on their centers", async () => {
  85. createAndSelectThreeRectanglesWithoutGap();
  86. expect(API.getSelectedElements()[0].x).toEqual(0);
  87. expect(API.getSelectedElements()[1].x).toEqual(10);
  88. expect(API.getSelectedElements()[2].x).toEqual(200);
  89. API.executeAction(distributeHorizontally);
  90. expect(API.getSelectedElements()[0].x).toEqual(0);
  91. expect(API.getSelectedElements()[1].x).toEqual(50);
  92. expect(API.getSelectedElements()[2].x).toEqual(200);
  93. });
  94. it("should distribute selected elements vertically with based on their centers", async () => {
  95. createAndSelectThreeRectanglesWithoutGap();
  96. expect(API.getSelectedElements()[0].y).toEqual(0);
  97. expect(API.getSelectedElements()[1].y).toEqual(10);
  98. expect(API.getSelectedElements()[2].y).toEqual(200);
  99. API.executeAction(distributeVertically);
  100. expect(API.getSelectedElements()[0].y).toEqual(0);
  101. expect(API.getSelectedElements()[1].y).toEqual(50);
  102. expect(API.getSelectedElements()[2].y).toEqual(200);
  103. });
  104. });