Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the State of a Functional Component with React Hooks when Testing with Enzyme

I was wondering how to test a state change of functional components with the useState hook with Enzyme. Usually the test would be something along the lines of expect(wrapper.state()).toEqual(expectedState) but I get the error:

ReactWrapper::state() can only be called on class components

I know that Hooks are still in the alpha stage and things like shallow still don't work but I was wondering is there a way to this yet?

like image 354
avatarhzh Avatar asked Sep 08 '25 10:09

avatarhzh


1 Answers

You should avoid testing implementation details. Instead, test if the behavior of the component, when triggering an action which would update your state, acts as you would expect.

like image 191
Daniel Afonso Avatar answered Sep 11 '25 07:09

Daniel Afonso