boblauer / react-onclickout

An ES6-friendly on-click-outside React component.
MIT License
92 stars 14 forks source link

Deferred binding to window click causing strange testing effects #6

Closed grassick closed 8 years ago

grassick commented 8 years ago

If a synchronous unit test is done that involves the component in any way, it will leave behind click events that will fire later. It's because the unmount is called before the deferred binding to the window.

The reason is that the binding to the window is deferred, but it should check that the component hasn't already unmounted before binding.

boblauer commented 8 years ago

I just merged your pull a little bit ago, but then it dawned on me that for sync tests, you can just override setTimeout to execute the callback immediately. By doing that, binding to the window is not deferred, and so all of the async problems go away.

If you check out the test file, I've implemented it in the beforeEach at the top. Only one test requires the bindings to be deferred, so in that case I restore the actual setTimeout before running the test.

grassick commented 8 years ago

Oh, now I understand. The issue though is that my unit tests now need to use a hack on setTimeout even though they are testing some completely unrelated component which just happens to have react-onclickout buried deep inside. I can't really hack setTimeout for all my tests in that way.

boblauer commented 8 years ago

Ah, I see. I hadn't thought about testing components that use the OnClickOut component inside of them. Fair enough, I'll add that code back in. Sorry about that.

boblauer commented 8 years ago

Published on npm @2.0.4

grassick commented 8 years ago

Thanks!