So I am writing some tests for code that adds a click event on the document. I am using JSDom, ReactJS, and Mocha/Chai setup. I have tried the following code in a test:
document.addEventListener('click', function() {
console.log('test');
});
React.addons.TestUtils.Simulate.click(document);
//also tried React.addons.TestUtils.Simulate.click(document.body);
however this code does not produce the echo that I am expecting.
Is there a way to simulate click, keyup, etc... on the document with JSDom and ReactJS?
UPDATE
To Nick answers, I have tried adding this code to the test:
document.body.addEventListener('click', function() {
console.log('test');
});
document.body.click();
and I till don't get the console log output. I am not sure if there is some issue with JSDom and doing this type of thing.
If I can't unit test this code, that's fine, there is already some code that I can't unit test right now (code that requires a real DOM to be able to get widths, heights, etc...) but I would like to be able to unit test most of the code (and I am not interested in using PhantomJS for unit testing). My integration tests will cover that type of code.
UPDATE2
Another thing to note is that is that when I console.log(document);
I see object attached to the _listeners
property for click
so I know the event is being attached, it just does not seem to be executing.