title: How to change components state with Jest and Testing Library?
body: <p>I am trying to change the state of my component in order to test the changed UI.
  But I have no luck.</p>
  <pre><code>const { getByText } = render(&lt;MyComponent/&gt;);
  
  // Cache original functionality
  const realUseState = React.useState;
  
  // Stub the initial state
  const stubInitialState = {
    showBrokers: true
  };
  
  // Mock useState before rendering your component
  await jest
    .spyOn(React, 'useState')
    .mockImplementationOnce(() =&gt; realUseState(stubInitialState));
  
  expect(getByText(&quot;Brokers&quot;)).toBeInTheDocument(); // &lt;&lt;&lt;&lt;---- Here I am getting Customers instead Brokers
  </code></pre>