button_handleClick setTimeout not triggered for Enzyme test
See original GitHub issuedownshift
version: 1.31.12node
version: 8.xnpm
(oryarn
) version: 6.x
Relevant code or config
What you did:
We have unit test that simulates a click and test the dropdown populates the right options.
enzymeWrapper.find('#randomButtonID').simulate('click');
What happened:
Our getToggleButtonProps passed in onClick
function is working as designed but the button_handleClick fuction is not working right and isOpen state is never updated. https://github.com/paypal/downshift/blob/master/src/downshift.js#L548
onClick: composeEventHandlers(onClick, this.button_handleClick)
I debugged the latest few releases then found that the setTimeout function added 12 days ago is never triggered. If I take the setTimeout out, the test will immediately pass. https://github.com/paypal/downshift/blob/master/src/downshift.js#L574
setTimeout(() =>
this.toggleMenu({type: Downshift.stateChangeTypes.clickButton}),
)
Reproduction repository:
Problem description:
Since toggleMenu is never called, the isOpen state is always false and no drop-downs are populated in our unit test. This has worked in v1.31.7 but fails after v1.31.9.
Suggested solution:
I did manage to bypass the test with mock timer setup, something like
jest.useFakeTimers();
enzymeWrapper.find('#randomButtonID').simulate('click');
jest.runAllTimers();
enzymeWrapper.update();
but is the setTimeout necessary? just to unit test a simple click do we have to always mock the timer? I’m not familiar with ios blur or not sure why a setTimeout is needed in this case, maybe another solution https://codereview.stackexchange.com/questions/47889/alternative-to-setinterval-and-settimeout?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Ok I will draft up a PR tomorrow, unless someone figure out an alternative in the next couple hours 😃
@aMollusk, if you can get that to work that’d be swell. @nicoxxie made a PR which I just merged to resolve this using the
NODE_ENV
check. We can move to something better if that comes along 😃 Thanks!