Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate click event on react element

I'm trying to simulate a .click() event on a React element but I can't figure out why it is not working (It's not reacting when I'm firing the event).

I would like to post a Facebook comment using only JavaScript but I'm stuck at the first step (do a .click() on div[class="UFIInputContainer"] element).

My code is:

document.querySelector('div[class="UFIInputContainer"]').click(); 

And here's the URL where I'm trying to do it: https://www.facebook.com/plugins/feedback.php...

P.S. I'm not experienced with React and I don't know really if this is technically possible. It's possible?

EDIT: I'm trying to do this from Chrome DevTools Console.

like image 568
Alfonso M. García Astorga Avatar asked Oct 17 '16 16:10

Alfonso M. García Astorga


People also ask

How do you trigger a click event in React?

To add the click event in React using plain JavaScript, you need to use addEventListener() to assign the click event to an element. Create one <button> element as ref props so that it can be accessed to trigger the click event.

How do you simulate a click on an element?

Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.

How do you simulate button click in React?

To simulate a button click in Jest and JavaScript, we call the Enzyme simulate method. import React from "react"; import { shallow } from "enzyme"; import Button from "./Button"; describe("Test Button component", () => { it("Test click event", () => { const mockCallBack = jest.

How to detect mouse click in React React?

React tracks the mousedown and mouseup events for detecting mouse clicks, instead of the click event like most everything else. So instead of calling the click method directly or dispatching the click event, you have to dispatch the down and up events. For good measure I'm also sending the click event but I think that's unnecessary for React:

How to simulate browser events in react?

We will look at the most simple, built-in, and official way: using the ReactTestUtils from the react-dom library. The utilities that will allow us to simulate browser events, such as click and change, are in the ReactTestUtils.Simulate module.

What is the react onClick event handler?

What is the React onClick Event Handler? Whenever you need to perform an action after clicking a button, link, or pretty much any element, you’ll use the onClick event handler. Therefore, the onClick event handler is one of the most powerful and most used tools in your React tool belt.

How to test Dom events in react?

Here is how we can test it: Don’t forget that dispatching DOM events only works when the DOM container is added to the document. You can use a library like React Testing Library to reduce the boilerplate code.


1 Answers

React tracks the mousedown and mouseup events for detecting mouse clicks, instead of the click event like most everything else. So instead of calling the click method directly or dispatching the click event, you have to dispatch the down and up events. For good measure I'm also sending the click event but I think that's unnecessary for React:

const mouseClickEvents = ['mousedown', 'click', 'mouseup']; function simulateMouseClick(element){   mouseClickEvents.forEach(mouseEventType =>     element.dispatchEvent(       new MouseEvent(mouseEventType, {           view: window,           bubbles: true,           cancelable: true,           buttons: 1       })     )   ); }  var element = document.querySelector('div[class="UFIInputContainer"]'); simulateMouseClick(element); 

This answer was inspired by Selenium Webdriver code.

like image 98
carlin.scott Avatar answered Oct 08 '22 17:10

carlin.scott