title: Testing components that contain react router Link with Enzyme
body: <p>I am using enzyme. I need to test a component that has react router Link as a child. I need the following</p>
  
  <ul>
  <li>mount the component using mount() of enzyme since I need to test the whole component tree </li>
  <li>test behaviours of component when it properties change.</li>
  </ul>
  
  <p>I cannot wrap my component with StaticRouter or MemoryRouter since enzyme only allows setProps() at root level.</p>
  
  <p>My current solution is to stub the Link render method with sinon. Here is a short example.</p>
  
  <pre><code>import {Link} from 'react-router-dom';
  import sinon from 'sinon';
  
  // ....
  // ....
  
  describe('test',() =&gt; {
     before(() =&gt; {
        sinon.stub(Link.prototype, 'render').callsFake(function reactRouterLinkRender() {
             const {innerRef, replace, to } = this.props;
             const _props = {href: to, ref: innerRef, replace, onClick: this.handleClick};
             return (&lt;a {..._props}&gt;this.props.children&lt;/a&gt;);
         });
  
      });
  });
  </code></pre>
  
  <p>Is there a better way to avoid the error "<em>Invariant Violation: You should not use Link outside a Router</em>"?</p>
  
  <p>Thanks</p>