title: Jest mocking document.referrer
body: <p>I have the following method in a react app service which requires Unit Testing.</p>
  
  <pre><code>onDeclineCallback = () =&gt; {
      console.log("boom " + document.referrer);
  
      if (document.referrer === "") {
        console.log("redirect to our age policy page");
      } else {
        history.back();
      }
    };
  </code></pre>
  
  <p>My unit test currently looks like:</p>
  
  <pre class="lang-js prettyprint-override"><code>history.back = jest.fn(); // Mocking history.back as jest fn
  
  describe('age verifiction service test', () =&gt; {
    it('returns user to referrer if declined and referrer is available', () =&gt; {
      document = {
        ...document,
        referrer: 'Refferer Test', // My hacky attempt at mocking the entire document object
      };
  
      ageVerification.onDeclineCallback();
      expect(history.back).toHaveBeenCalledTimes(1);
    });
  });
  </code></pre>
  
  <p>I am trying to find a way of mocking <code>document.referrer</code> in order to write a unit test for each case. Can anyone provide an approach for this?</p>