Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initial state value for useState Hook in jest and enzyme?

I know this question is already asked here: How to set initial state for useState Hook in jest and enzyme?

const [state, setState] = useState([]);

And I totally agree with Jimmy's Answer to mock the useState function from test file but I have some extended version of this question, "What if I have multiple useState statements into the hooks, How can I test them and assign the respective custom values to them?"

I have some JSX rendering with the condition of hook's state values and depending on the values of that state the JSX is rendering.

How Can I test those JSX by getting them into the wrapper of my test case code?

like image 345
Harshal Mahajan Avatar asked Feb 13 '20 11:02

Harshal Mahajan


People also ask

How do I set initial state in jest?

useState(); const [mySecondState, setMySecondState] = React. useState(); // in MyComponent. test. js test('useState mock', () => { const initialStateForFirstUseStateCall = 'My First Initial State' const initialStateForSecondUseStateCall = 'My Second Initial State' React.

How do you set initial value in useState?

To set a conditional initial value for useState in React:Pass a function to the useState hook. Use a condition to determine the correct initial value for the state variable. The function will only be invoked on the initial render.

How do you set the value of a useState hook?

If you want to set an initial value for the variable, pass the initial value as an argument to the useState function. When React first runs your component, useState will return the two-element array as usual but will assign the initial value to the first element of the array, as shown in figure 5.

How do you initialize a state in React hooks?

If you want to set initial state after loading data (fetch data from api) you can use "useEffect" in React hooks. it is equal to "componentWillReceiveProps" in class component. so when you set state value in useEffect make sure avoid infinity loop eg .. Save this answer.


1 Answers

Upon the answer you linked, you can return different values for each call of a mock function:

let myMock = jest.fn();

myMock
  .mockReturnValueOnce(10)
  .mockReturnValueOnce('x')
  .mockReturnValue(true);

In my opinion this is still brittle. You may modify the component and add another state later, and you would get confusing results.

Another way to test a React component is to test it like a user would by clicking things and setting values on inputs. This would fire the event handlers of the component, and React would update the state just as in real configuration. You may not be able to do shallow rendering though, or you may need to mock the child components.

If you prefer shallow rendering, maybe read initial state values from props like this:

function FooComponent({initialStateValue}) {
  const [state, setState] = useState(initialStateValue ?? []);
}
like image 154
sepeth Avatar answered Sep 28 '22 08:09

sepeth