Maybe it's an easy one, but my Cypress
tests fail to test locked scrolling functionality.
So I have this React
modal component which sets document.body.style.overflow = 'hidden'
upon mount (client side only)
I have the following Cypress
's test suite:
it('page cant scroll when modal is open', () => {
cy.get('[data-testid="button"]')
.first()
.click();
cy.window().scrollTo('bottom');
// scroll should be disabled
cy.window()
.its('scrollY')
.should('equal', 0);
// cy.get('[data-testid="modal"]')
// .scrollTo('bottom')
// .its('scrollY')
// .should('not.equal', 0);
});
Error message: Timed out retrying after 4000ms: expected 802 to equal 0
Looks like Cypress is able to scroll the window
even though this not possible when manually scrolling the page.
Any idea what the issue could be?
OP's edit: Scrolling the page programmatically still works (window.scrollTo()
). The overflow
property hides the scrollbar but this doesn't mean the scroll functionality is actually blocked (beyond a simple user interaction)
I was advised to use another approach for testing the scroll freeze through pagedown
and pageup
key navigation. Using the mentioned keys will be blocked on the actual page and user won't be able to scroll.
it('page cant scroll when modal is open', () => {
cy.get('[data-testid="button"]')
.first()
.click();
cy.get('body').type('{pagedown}');
cy.window()
.its('scrollY')
.should('equal', 0);
cy.get('[data-testid="overlay"]').click();
cy.get('body').type('{pagedown}');
cy.window()
.its('scrollY')
.should('not.equal', 0);
});
As with the previous code example, it makes sense for this snippet to throw error as well. Timed out retrying after 4000ms: expected 46 to equal 0
.