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

109
Views
Test if a function was called

I have a simple React hook:

const useHook = () => {
   useEffect(() => {
      window.addEventListener('click', handleClick);
   }, []);

   const handleClick = () => {};

   return null;
};

Am I able to somehow test if the handleClick function was called on click event?

I can of course simulate click event in Jest. However how to check if this fn was called?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

How about using a count wrapper ?

function countFn(fn) {
    let wrapper = function() {
        wrapper.called ++;
        return fn.apply(this, arguments);
    }
    wrapper.called = 0;
    return wrapper;
}

const handleClick = countFn((e) => {console.log(e)});

handleClick(1);
handleClick(2);
handleClick(3);

console.log(`handleClick called ${handleClick.called} times`);

1
2
3
handleClick called 3 times
about 4 years ago · Juan Pablo Isaza Report
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!