Skip to content

Commit

Permalink
test: refactor a menu test case to have it pass on CI env
Browse files Browse the repository at this point in the history
For some unknown reason, scroll events were triggered upon mouse wheel, even though scroll
was prevented. It didn't happen when running locally headless with `cypress run`, so it seems like the only differentiating factor is the OS being linux in the CI env.

The test is changed to compare actual scrollY of the window, instead of checking if scroll event handlers are called.
  • Loading branch information
alirezamirian committed Dec 13, 2024
1 parent c30fdd5 commit b3123b7
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions packages/jui/src/Menu/Menu.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -889,51 +889,44 @@ describe("ContextMenu", () => {
});

it("disables the scroll of the document when the menu is open", () => {
const ScrollListener = (props: { onScroll: () => void }) => {
React.useEffect(() => {
const onScroll = () => {
props.onScroll();
};
document.addEventListener("scroll", onScroll);
return () => {
document.removeEventListener("scroll", onScroll);
};
});
return null;
};
const onScroll = cy.stub().as("onScroll");
cy.mount(
<>
<ScrollListener onScroll={onScroll} />
<div style={{ height: "10rem" }} id="element-above" />
<ContextMenuContainer
id="container"
style={{ margin: "10rem 0", height: "100vh", background: "#aaa" }}
style={{
height: "100vh",
overflow: "auto",
background: "#aaa",
}}
renderMenu={() => (
<Menu aria-label="Test Context Menu">
<Item>Menu item</Item>
</Menu>
)}
>
{" "}
scrollable container
<div style={{ height: "150vh" }}></div>
</ContextMenuContainer>
</>
);
cy.get("#container").rightclick("top", {
scrollBehavior: false,
});
cy.findByRole("menu");

// onScroll was called in a run of the pipeline on the CI env.
// Not sure why but trying to work around the flakiness by a wait.
// https://cloud.cypress.io/projects/o1ooqz/runs/255/overview/0aa04a42-1030-4ab6-a8b2-1fe08069a705?roarHideRunsWithDiffGroupsAndTags=1
cy.wait(100);

cy.get("#container").realMouseWheel({
deltaY: 5,
scrollBehavior: false, // we don't want cypress to do any extra scrolling
});
cy.wait(200); // maybe a bug in realMouseWheel, but before the wait, the next command runs before scrolling happens
cy.wrap(onScroll).should("not.be.called");
cy.window()
.its("scrollY")
.then((scrollBefore) => {
cy.get("#element-above").realMouseWheel({
deltaY: 5,
scrollBehavior: false, // we don't want cypress to do any extra scrolling
});
cy.wait(200); // maybe a bug in realMouseWheel, but before the wait, the next command runs before scrolling happens
cy.window()
.its("scrollY")
.then((scrollAfter) => {
expect(scrollAfter).to.eq(scrollBefore);
});
});
});

it("disables the scroll when the menu is open", () => {
Expand Down

0 comments on commit b3123b7

Please sign in to comment.