react-pure-component-starter icon indicating copy to clipboard operation
react-pure-component-starter copied to clipboard

Simulating onClick when testing

Open TYRONEMICHAEL opened this issue 9 years ago • 4 comments

Thank you for this. I was actually busy implementing my own solution similar to this, but including Redux. Anyway, curious to know how you test components that require user interaction? An example would be onClick. I looked at shallowRender, but then you have to navigate to your children and call the method manually. Not ideal and not elegant.

TYRONEMICHAEL avatar Oct 17 '15 19:10 TYRONEMICHAEL

React has some built-in testing tools, including DOM event simulators that you can call. There's a very good chance I'll add examples of it here, since I've written them for other projects, anyway. =)

ericelliott avatar Oct 17 '15 23:10 ericelliott

That would be awesome. I did try to use TestUtils.Simulate.click together with Cheerio without any luck. React does not complain about the lack of a DOM when using Cheerio though.

TYRONEMICHAEL avatar Oct 18 '15 05:10 TYRONEMICHAEL

Yeah, the simulate tools need live DOM. You can use JSDom to construct real DOM with React virtual event support.

Test DOM Example Usage

ericelliott avatar Oct 20 '15 03:10 ericelliott

If you'd like to add a working example here, I'd welcome a PR. =)

ericelliott avatar Oct 20 '15 03:10 ericelliott