question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

button_handleClick setTimeout not triggered for Enzyme test

See original GitHub issue
  • downshift version: 1.31.12
  • node version: 8.x
  • npm (or yarn) 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:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoxxiecommented, Apr 30, 2018

Ok I will draft up a PR tomorrow, unless someone figure out an alternative in the next couple hours 😃

0reactions
kentcdoddscommented, May 1, 2018

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

If you get an error in a function passed to setTimeout the ...
Disclaimer: I'm not a React developer, so my answer has nothing to do with the React component part of this code.
Read more >
Testing React Tracking with Jest and Enzyme
A function that is executed after a short timeout, giving the browser time to make outbound requests first. Thus, we pass the tracking...
Read more >
Handling setTimeout & setInterval in jest/Enzyme - Medium
When the test setup was configured with the useFakeTimers in the outer describe block something was causing it to not have the desired...
Read more >
React 16: Testing Function Components With Hooks | by oshell
In this article I will tell you how you can test various cases of stateful components, using React 16, Jest, and Enzyme.
Read more >
react-timeout - npm
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found