Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
jest test if the page scrolls down to a certain div when page loads

I have a page and it scrolls down to a certain div when the page loads. My implementation for that is as follows.

 const creditCardDetails = useRef(null);

 useEffect(() => {
    creditCardDetails.current.scrollIntoView();
  }, []);



<div
    data-testid="credit-card-details-container"
    ref={creditCardDetails}
    style={{
      height: '100px',
      backgroundColor: '#F6F6F6',
    }}
  >
    <span>***Test container***</span>
</div>

I want to write a test for this. So far i tried something like this

const scrollIntoViewMock = jest.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;

  it('should scroll down to card container', async () => {
    const { baseElement } = render(
      <Provider store={store}>
        <ThemeProvider theme={theme}>
          <Payment />
        </ThemeProvider>
      </Provider>,
    );
    expect(scrollIntoViewMock).toBeCalled();
  });

But it doesn't help the code coverage so i'm assuming it does not test properly. How do i test if the scrolling happens when the page loads. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!