scroll.test.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {
  2. mockBoundingClientRect,
  3. render,
  4. restoreOriginalGetBoundingClientRect,
  5. waitFor,
  6. } from "./test-utils";
  7. import { Excalidraw } from "../packages/excalidraw/index";
  8. import { API } from "./helpers/api";
  9. import { Keyboard } from "./helpers/ui";
  10. import { KEYS } from "../keys";
  11. import ExcalidrawApp from "../excalidraw-app";
  12. const { h } = window;
  13. describe("appState", () => {
  14. it("scroll-to-content on init works with non-zero offsets", async () => {
  15. const WIDTH = 200;
  16. const HEIGHT = 100;
  17. const OFFSET_LEFT = 20;
  18. const OFFSET_TOP = 10;
  19. const ELEM_WIDTH = 100;
  20. const ELEM_HEIGHT = 60;
  21. mockBoundingClientRect();
  22. await render(
  23. <div>
  24. <Excalidraw
  25. initialData={{
  26. elements: [
  27. API.createElement({
  28. type: "rectangle",
  29. id: "A",
  30. width: ELEM_WIDTH,
  31. height: ELEM_HEIGHT,
  32. }),
  33. ],
  34. scrollToContent: true,
  35. }}
  36. />
  37. </div>,
  38. );
  39. await waitFor(() => {
  40. expect(h.state.width).toBe(200);
  41. expect(h.state.height).toBe(100);
  42. expect(h.state.offsetLeft).toBe(OFFSET_LEFT);
  43. expect(h.state.offsetTop).toBe(OFFSET_TOP);
  44. // assert scroll is in center
  45. expect(h.state.scrollX).toBe(WIDTH / 2 - ELEM_WIDTH / 2);
  46. expect(h.state.scrollY).toBe(HEIGHT / 2 - ELEM_HEIGHT / 2);
  47. });
  48. restoreOriginalGetBoundingClientRect();
  49. });
  50. it("moving by page up/down/left/right", async () => {
  51. mockBoundingClientRect();
  52. await render(<ExcalidrawApp />, {});
  53. const scrollTest = () => {
  54. const initialScrollY = h.state.scrollY;
  55. const initialScrollX = h.state.scrollX;
  56. const pageStepY = h.state.height / h.state.zoom.value;
  57. const pageStepX = h.state.width / h.state.zoom.value;
  58. // Assert the following assertions have meaning
  59. expect(pageStepY).toBeGreaterThan(0);
  60. expect(pageStepX).toBeGreaterThan(0);
  61. // Assert we scroll up
  62. Keyboard.keyPress(KEYS.PAGE_UP);
  63. expect(h.state.scrollY).toBe(initialScrollY + pageStepY);
  64. // x-axis unchanged
  65. expect(h.state.scrollX).toBe(initialScrollX);
  66. // Assert we scroll down
  67. Keyboard.keyPress(KEYS.PAGE_DOWN);
  68. Keyboard.keyPress(KEYS.PAGE_DOWN);
  69. expect(h.state.scrollY).toBe(initialScrollY - pageStepY);
  70. // x-axis unchanged
  71. expect(h.state.scrollX).toBe(initialScrollX);
  72. // Assert we scroll left
  73. Keyboard.withModifierKeys({ shift: true }, () => {
  74. Keyboard.keyPress(KEYS.PAGE_UP);
  75. });
  76. expect(h.state.scrollX).toBe(initialScrollX + pageStepX);
  77. // y-axis unchanged
  78. expect(h.state.scrollY).toBe(initialScrollY - pageStepY);
  79. // Assert we scroll right
  80. Keyboard.withModifierKeys({ shift: true }, () => {
  81. Keyboard.keyPress(KEYS.PAGE_DOWN);
  82. Keyboard.keyPress(KEYS.PAGE_DOWN);
  83. });
  84. expect(h.state.scrollX).toBe(initialScrollX - pageStepX);
  85. // y-axis unchanged
  86. expect(h.state.scrollY).toBe(initialScrollY - pageStepY);
  87. };
  88. const zoom = h.state.zoom.value;
  89. // Assert we scroll properly when zoomed in
  90. h.setState({ zoom: { value: (zoom * 1.1) as typeof zoom } });
  91. scrollTest();
  92. // Assert we scroll properly when zoomed out
  93. h.setState({ zoom: { value: (zoom * 0.9) as typeof zoom } });
  94. scrollTest();
  95. // Assert we scroll properly with normal zoom
  96. h.setState({ zoom: { value: zoom } });
  97. scrollTest();
  98. restoreOriginalGetBoundingClientRect();
  99. });
  100. });