How to test mouse clientY in React testing (JEST)

gopi mudumal
useEffect(() => {
    const maybeHandler = (event: MouseEvent) => {
      menuData.forEach((el) => {
        if (el.hasActiveDropdown && event.clientY > 50) {
          handleCloseDropDown();
          // handleDropDown('0');
        }
      });
    };
    document.addEventListener('mousedown', maybeHandler);
    return () => document.removeEventListener('mousedown', maybeHandler);
  }, [handleCloseDropDown, menuData]);

I am used this useEffect to handle mulltip dropdowns in navbar component, navbar has fix height 50px so my logic is whenver use click outside the navbar the drop downs all are close.

I am unadble to test in JEST this clientY propery

Muhammad Salman

If you want to test the useEffect hook with the clientY property in Jest, you could consider using Jest's spyOn and mock functions to mock the addEventListener and removeEventListener methods on the document object, and then specify the return value for the clientY property in the mocked MouseEvent object that is passed to the event handler.

Here is an example of how you could do this:

const maybeHandler = jest.fn();

const mockAddEventListener = jest.spyOn(document, 'addEventListener');
const mockRemoveEventListener = jest.spyOn(document, 'removeEventListener');

useEffect(() => {
  maybeHandler(new MouseEvent('mousedown', {
    clientY: 100, // specify the return value for the clientY property
  }));

  document.addEventListener('mousedown', maybeHandler);

  return () => {
    document.removeEventListener('mousedown', maybeHandler);
  };
}, []);

// assert that the event listener was added and removed
expect(mockAddEventListener).toHaveBeenCalledWith('mousedown', maybeHandler);
expect(mockRemoveEventListener).toHaveBeenCalledWith('mousedown', maybeHandler);

// assert that the event handler was called
expect(maybeHandler).toHaveBeenCalled();

// assert the value of the clientY property in the mocked MouseEvent object
expect(maybeHandler.mock.calls[0][0].clientY).toBe(100);

You can then use this mocked event handler in your test cases to test the logic of your component that uses the useEffect hook.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to test a className with Jest and React testing library

How to test a className with the Jest and React testing library

How to test react component with onClick function Jest/React testing library

How to test a component with reduce method in react using react testing and jest?

How to test react-dropzone with Jest and react-testing-library?

How to test useRef with Jest and react-testing-library?

React testing library + Jest: How to test a component that receives a number then format it

How can i test the promise function at jest with react testing library?

how to test onChange write a test for a functional component jest + react testing library

How to test this react component with jest?

How do I test the Elements value in React Native unit testing using Enzyme (Jest)

How do you test for the non-existence of an element using jest and react-testing-library?

How can I test functions that are provided by context using jest and react-testing-library?

How to get test coverage from Jest while testing React.js App?

How to write a test with Jest and React testing library to check if anchor-tag <a /> exist

How can I test a function of a function component that uses React.useState() using jest enzyme testing?

how can I test if useState hook has been called with jest and react testing library?

Test failing cases with React, Jest, React-Testing-Library

How to do jest testing in react native?

How to use Jest for testing a react component with localStorage?

Error in running jest test with react-testing-library

Test the function inside click handler - Jest, React testing Library

When testing react project npm test works but jest doesn't

Isolate child component in test - react-testing-library & Jest

React Jest Testing onSubmit

Meteor react jest testing

React native testing with Jest

I am new to typescript, react and unit testing. How can I write test case for the following function with jest?

How to test typescript application using jest with testing libraray?