Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestUtils.Simulate.click doesn't bubble to window or document #5043

Closed
ken210 opened this issue Oct 2, 2015 · 1 comment

Comments

@ken210
Copy link

commented Oct 2, 2015

Possibly a dupe of #4766

test:

import React from "react";
import TestUtils from "react-addons-test-utils";

class TestElement extends React.Component {
  render() {
    return (
      <div onClick={() => {
          console.log('clicked on div!');
        }}>
        <button onClick={() => {
          console.log('clicked on button!');
        }}>Click me</button>
      </div>
    );
  }
}

describe('Foo', () => {
  it('bar', () => {
   var element = TestUtils.renderIntoDocument(
      <TestElement />
    );

    window.addEventListener('click', () => {
      console.log('click on window');
    });

    document.addEventListener('click', () => {
      console.log('click on document');
    });

    var button = TestUtils.findRenderedDOMComponentWithTag(element, 'button');
    TestUtils.Simulate.click(button);
  });
});

Logs:

clicked on button!
clicked on div!

The thing is: both window and document click handlers are never called.

Am I doing something wrong in here?

Thanks in advance!

@sophiebits

This comment has been minimized.

Copy link
Collaborator

commented Oct 3, 2015

Simulate only simulates an event within React's event system. You should use an ordinary .dispatchEvent or similar if you want to create and dispatch a real browser event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.