title: How to stub contentwindow.print with cypress/test printJS with cypress
body: <p>My program uses <a href="https://github.com/crabbly/Print.js" rel="noreferrer">printJS</a> which a library that helps format the content of the page for printing. I wanted to write tests with cypress to test that the print preview has been called. Currently I have a button that calls printJS when clicked and since cypress cannot interact with the print preview window, I thought it would be a good idea to stub the call to printJS then write an assertion that it was called once. I understand that this works with window.print() as you can stub that with this  code.</p>
  
  <pre><code>cy.visit('http://127.0.0.1/',{
      onBeforeLoad: (win) =&gt; {
          cy.stub(win, 'print')
      }
  })
  </code></pre>
  
  <p>Then assert with this</p>
  
  <pre><code>cy.contains('print').click()
  cy.window().then((win) =&gt; {
      expect(win.print).to.be.calledOnce
  })
  </code></pre>
  
  <p>My old button </p>
  
  <pre><code>&lt;button type="button" class="btn btn-secnodary" onclick="window.print()"&gt;
      Print
  &lt;/button&gt;
  </code></pre>
  
  <p>But instead I used printJS which means my button now looks like this </p>
  
  <pre><code>&lt;button type="button" onclick="printJS({printable: 'id_preview_modal_body', type: 'html'})" data-dismiss="modal"&gt;
      Print
  &lt;/button&gt;
  </code></pre>
  
  <p>The javascript gets loaded in as print.min.js which can be found <a href="https://printjs-4de6.kxcdn.com/print.min.js" rel="noreferrer">here</a>. I tried to stub the contentwindow but that doesn't seem to work so far. In the code for printJS, the printing happens here </p>
  
  <pre><code>frameElement.contentWindow.print()
  </code></pre>
  
  <p>from <a href="https://github.com/crabbly/Print.js/blob/master/src/js/print.js" rel="noreferrer">their github page</a>, line 63</p>
  
  <p>The way im stubbing it gives this issue</p>
  
  <pre><code>cy.visit('http://127.0.0.1:8000/notices/new/',{
      onBeforeLoad: (win) =&gt; {
          cy.stub(win, 'printJS')
      }
  })
  
  Uncaught TypeError: Cannot stub non-existent own property printJS
  </code></pre>
  
  <p>The assertion also gives this error </p>
  
  <pre><code>cy.window().then((win) =&gt; {
      expect(win.printJS).to.be.calledOnce
  })
  
  TypeError: [Function: init] is not a spy or a call to a spy!
  </code></pre>
  
  <p>I think the <code>[Function: init]</code> is a referring to <code>const printJS = print.init</code> from their <code>index.js</code> <a href="https://github.com/crabbly/Print.js/blob/master/src/index.js" rel="noreferrer">file</a>. But i don't know how to proceed further in debugging this issue. Any help would be appreciated. Thanks!</p>