title: How can I test that url search changed with react testing library?
body: <p>I have a component which reacts to url search changes, and a button that changes the url search. I want to test that when I click the button, the component reacts accordingly. Here's a codesandbox: <a href="https://codesandbox.io/s/react-testing-library-url-change-test-b5zq1" rel="nofollow noreferrer">https://codesandbox.io/s/react-testing-library-url-change-test-b5zq1</a>.</p>
  <p>App.js</p>
  <pre class="lang-js prettyprint-override"><code>export default function App() {
    const [urlChanged, setUrlChanged] = useState(false);
    const handleURLSearchChange = () =&gt; {
      window.location.search = new URLSearchParams({ foo: &quot;bar&quot; });
    };
    useEffect(() =&gt; {
      if (window.location.search.length !== 0) {
        setUrlChanged(true);
      }
    }, []);
    return (
      &lt;div&gt;
        &lt;button aria-label=&quot;change&quot; onClick={handleURLSearchChange}&gt;
          Change URL search
        &lt;/button&gt;
        &lt;p aria-label=&quot;URL Status&quot;&gt;{urlChanged ? &quot;Changed!&quot; : &quot;Not yet&quot;}&lt;/p&gt;
      &lt;/div&gt;
    );
  }
  
  </code></pre>
  <p>App.spec.js</p>
  <pre class="lang-js prettyprint-override"><code>describe(&quot;App&quot;, () =&gt; {
    it(&quot;Reacts to url changes when touching the button&quot;, async () =&gt; {
      render(&lt;App /&gt;);
      const button = await screen.findByLabelText(&quot;change&quot;);
      userEvent.click(button);
      const label = await screen.findByLabelText(&quot;URL Status&quot;);
      await waitFor(() =&gt; expect(label).toHaveTextContent(&quot;Changed!&quot;));
    });
  });
  
  </code></pre>
  <p>The problem is I'm getting:</p>
  <pre><code>    Error: Not implemented: navigation (except hash changes)
  
  </code></pre>
  <p>Note: I'm only able to see this error if I donwload the sandbox, and run npm install and npm test.</p>
  <p>Do I have to mock the window.location object with both setters and getters? What's the better way to do it?</p>