title: Intercept navigation change with jest.js (or how to override and restore location.href)
body: <p>Application code is calling <code>location.href = "some-url"</code>.
  I want to write a test that verify the navigation redirect has happened.</p>
  
  <p>Using jest on jsdom, I tried to do it with overriding location.href setter using jest mock function and it is working.</p>
  
  <p>But now I can't seems to restore the location.href property at the test cleanup, and it failing the rest of the tests which relay on 'location.href'.</p>
  
  <pre><code>it('test navigation happened', () =&gt; {
    const originalLocationHref = Object.getOwnPropertyDescriptor(window.location, 'href'); // returns undefined
  
    spyFn = jest.fn();
    Object.defineProperty(window.location, 'href', {
      set: spyFn,
      enumerable: true,
      configurable: true
    });
  
    someAppCodeThatShouldRedirectToSomeUrl();
  
    expect(spyFn).toBeCalledWith('http://some-url'); // this is working
  
    // Cleanup code, not working, because originalLocationHref is undefined
    Object.defineProperty(window.location, 'href', originalLocationHref);  
  });
  </code></pre>
  
  <p>What am I missing? Why <code>Object.getOwnPropertyDescriptor(window.location, 'href');</code> is <code>undefined</code>? </p>
  
  <p>Is there a better way to intercept navigation events in order to test it? </p>
  
  <p>Thanks</p>