actionStyles.test.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import ExcalidrawApp from "../excalidraw-app";
  2. import { t } from "../i18n";
  3. import { CODES } from "../keys";
  4. import { API } from "../tests/helpers/api";
  5. import { Keyboard, Pointer, UI } from "../tests/helpers/ui";
  6. import { fireEvent, render, screen } from "../tests/test-utils";
  7. import { copiedStyles } from "./actionStyles";
  8. const { h } = window;
  9. const mouse = new Pointer("mouse");
  10. describe("actionStyles", () => {
  11. beforeEach(async () => {
  12. await render(<ExcalidrawApp />);
  13. });
  14. it("should copy & paste styles via keyboard", () => {
  15. UI.clickTool("rectangle");
  16. mouse.down(10, 10);
  17. mouse.up(20, 20);
  18. UI.clickTool("rectangle");
  19. mouse.down(10, 10);
  20. mouse.up(20, 20);
  21. // Change some styles of second rectangle
  22. UI.clickLabeledElement("Stroke");
  23. UI.clickLabeledElement(t("colors.c92a2a"));
  24. UI.clickLabeledElement("Background");
  25. UI.clickLabeledElement(t("colors.e64980"));
  26. // Fill style
  27. fireEvent.click(screen.getByTitle("Cross-hatch"));
  28. // Stroke width
  29. fireEvent.click(screen.getByTitle("Bold"));
  30. // Stroke style
  31. fireEvent.click(screen.getByTitle("Dotted"));
  32. // Roughness
  33. fireEvent.click(screen.getByTitle("Cartoonist"));
  34. // Opacity
  35. fireEvent.change(screen.getByLabelText("Opacity"), {
  36. target: { value: "60" },
  37. });
  38. mouse.reset();
  39. API.setSelectedElements([h.elements[1]]);
  40. Keyboard.withModifierKeys({ ctrl: true, alt: true }, () => {
  41. Keyboard.codeDown(CODES.C);
  42. });
  43. const secondRect = JSON.parse(copiedStyles)[0];
  44. expect(secondRect.id).toBe(h.elements[1].id);
  45. mouse.reset();
  46. // Paste styles to first rectangle
  47. API.setSelectedElements([h.elements[0]]);
  48. Keyboard.withModifierKeys({ ctrl: true, alt: true }, () => {
  49. Keyboard.codeDown(CODES.V);
  50. });
  51. const firstRect = API.getSelectedElement();
  52. expect(firstRect.id).toBe(h.elements[0].id);
  53. expect(firstRect.strokeColor).toBe("#c92a2a");
  54. expect(firstRect.backgroundColor).toBe("#e64980");
  55. expect(firstRect.fillStyle).toBe("cross-hatch");
  56. expect(firstRect.strokeWidth).toBe(2); // Bold: 2
  57. expect(firstRect.strokeStyle).toBe("dotted");
  58. expect(firstRect.roughness).toBe(2); // Cartoonist: 2
  59. expect(firstRect.opacity).toBe(60);
  60. });
  61. });