'Mocking a Util function that requires a React Dom node
I have this util function that scrolls into view that I'm trying to test. The function (below) checks for a react dom node. I can't seem to figure out in jest how to test this function by providing the Dom node.
export const scrollIntoView = (node, behavior, block) => {
if (!node) return;
const element = ReactDom.findDOMNode(node);
try {
if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
if ("parentIFrame" in window) {
window.parentIFrame.scrollToOffset(0, 0);
} else {
window.scrollTo({ top: 0, behavior: behavior || "smooth" });
}
} else {
element.scrollIntoView({
behavior: behavior || "smooth",
block: block || "start",
});
}
} catch (error) {
element.scrollIntoView(false);
}
};
Here is my test: When I do the following I get the error: Argument appears to not be a ReactComponent.
it("Should scroll up", () => {
const mockScrollIntoView = jest.fn(scrollIntoView);
const node = (document.body.innerHTML = '<App id="root"/>');
mockScrollIntoView(node);
expect(mockScrollIntoView).toBeCalledWith({
behavior: "smooth",
block: "start",
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
