Skip to content

Commit

Permalink
WD-8541 - fix: Scroll to the top of Config Panel if error occurs (#1690)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-cucu authored Jan 30, 2024
1 parent 91b9686 commit ea18eb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 121 deletions.
21 changes: 21 additions & 0 deletions src/components/ScrollOnRender/ScrollOnRender.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,25 @@ describe("ScrollOnRender", () => {
render(<ScrollOnRender>kids</ScrollOnRender>);
expect(screen.getByText("kids")).toBeInTheDocument();
});

it("shouldn't scroll into view if no scroll area is provided", () => {
render(<ScrollOnRender>kids</ScrollOnRender>);
expect(window.HTMLElement.prototype.scrollIntoView).not.toHaveBeenCalled();
});

it("shouldn't scroll into view if scroll area is null", () => {
render(<ScrollOnRender scrollArea={null}>kids</ScrollOnRender>);
expect(window.HTMLElement.prototype.scrollIntoView).not.toHaveBeenCalled();
});

it("should scroll into view if scroll area is provided", () => {
render(
<ScrollOnRender scrollArea={document.createElement("div")}>
kids
</ScrollOnRender>,
);
expect(window.HTMLElement.prototype.scrollIntoView).toHaveBeenCalledWith({
behavior: "smooth",
});
});
});
12 changes: 7 additions & 5 deletions src/components/ScrollOnRender/ScrollOnRender.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { PropsWithChildren } from "react";

import { useScrollOnRender } from "hooks/useScrollOnRender";
import { useEffect, type PropsWithChildren } from "react";

type Props = {
scrollArea?: HTMLElement | null;
} & PropsWithChildren;

const ScrollOnRender = ({ children, scrollArea }: Props) => {
const onRenderRef = useScrollOnRender(scrollArea);
return <div ref={onRenderRef}>{children}</div>;
useEffect(() => {
if (scrollArea) {
scrollArea.scrollIntoView({ behavior: "smooth" });
}
}, [scrollArea]);
return <div>{children}</div>;
};

export default ScrollOnRender;
76 changes: 0 additions & 76 deletions src/hooks/useScrollOnRender.test.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/hooks/useScrollOnRender.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ if (!window.HTMLDivElement.prototype.animate) {
);
}

if (!window.HTMLElement.prototype.scrollIntoView) {
window.HTMLElement.prototype.scrollIntoView = jest.fn();
} else {
console.error(
"JSDOM appears to support scroll into view",
"you may now remove the mock",
);
}

class ResizeObserver {
observe = jest.fn();
unobserve = jest.fn();
Expand Down

0 comments on commit ea18eb1

Please sign in to comment.