0

I've tried reading around quite a lot for this, a bit confused if this is fixed or not now in Enzyme/React. However I've got some code which makes an API call (using Axios) then once it's successfully called the API updates a state value using of showResults to true using setShowResults() which is using useState().

In my unit tests which I'm using Jest and Enzyme, I can't test the contents of my app to see data being rendered as the state is set to false. How do I set a state value in Enzyme when using using wrapper.setState({ showResults: true }); doesn't work?

halfer
  • 19,824
  • 17
  • 99
  • 186
JS_Dev
  • 427
  • 4
  • 14
  • Check this page: https://airbnb.io/enzyme/docs/api/ReactWrapper/setState.html , and also it's duplicated question. you can find a lot of question like this into stackoverlow – Alex Jun 30 '19 at 12:09
  • using `wrapper.setState` better to avoid even if it works(I mean, for class-based components too) since it'd make your test unreliable. instead you need to mock call to `axios` and validate result rendered. depending on where you make your call(`useEffect`?) and how you render your component(`mount`? `shallow`?) it still may not work since Enzyme is on [its way to fully support hooks](https://github.com/airbnb/enzyme/issues/2011). So it is matter of your code which we don't see here. – skyboyer Jun 30 '19 at 12:38
  • take a look into https://stackoverflow.com/questions/55342181/set-state-when-testing-functional-component-with-usestate-hook – skyboyer Jun 30 '19 at 12:40

0 Answers0