scroll.test.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. const { h } = window;
  10. describe("appState", () => {
  11. it("scroll-to-content on init works with non-zero offsets", async () => {
  12. const WIDTH = 200;
  13. const HEIGHT = 100;
  14. const OFFSET_LEFT = 20;
  15. const OFFSET_TOP = 10;
  16. const ELEM_WIDTH = 100;
  17. const ELEM_HEIGHT = 60;
  18. mockBoundingClientRect();
  19. await render(
  20. <div>
  21. <Excalidraw
  22. initialData={{
  23. elements: [
  24. API.createElement({
  25. type: "rectangle",
  26. id: "A",
  27. width: ELEM_WIDTH,
  28. height: ELEM_HEIGHT,
  29. }),
  30. ],
  31. scrollToContent: true,
  32. }}
  33. />
  34. </div>,
  35. );
  36. await waitFor(() => {
  37. expect(h.state.width).toBe(200);
  38. expect(h.state.height).toBe(100);
  39. expect(h.state.offsetLeft).toBe(OFFSET_LEFT);
  40. expect(h.state.offsetTop).toBe(OFFSET_TOP);
  41. // assert scroll is in center
  42. expect(h.state.scrollX).toBe(WIDTH / 2 - ELEM_WIDTH / 2);
  43. expect(h.state.scrollY).toBe(HEIGHT / 2 - ELEM_HEIGHT / 2);
  44. });
  45. restoreOriginalGetBoundingClientRect();
  46. });
  47. });