Skip to content

Commit 525d594

Browse files
authored
Stub window.print recipe (#523)
1 parent bc457ee commit 525d594

File tree

10 files changed

+70
-0
lines changed

10 files changed

+70
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Recipe | Description
8383
[Stubbing Functions](./examples/stubbing-spying__functions) | Use `cy.spy()` and `cy.stub()` to test function calls
8484
[Stubbing `window.fetch`](./examples/stubbing-spying__window-fetch) | Work around the `window.fetch` limitation
8585
[Stubbing `window.open` and `console.log`](./examples/stubbing-spying__window) | Use `cy.stub()` and `cy.spy()` to test application behavior
86+
[Stubbing `window.print`](./examples/stubbing-spying__window-print) | Use `cy.stub()` to test `window.print` call made from the application
8687
[Stubbing Google Analytics](./examples/stubbing-spying__google-analytics) | Use `cy.stub()` to test Google Analytics calls
8788
[Spying and stubbing methods on `console` object](./examples/stubbing-spying__console) | Use `cy.spy()` and `cy.stub()` on `console.log`
8889
[Stub resource loading](./examples/stubbing__resources) | Use `MutationObserver` to stub resource loading like `img` tags

circle.yml

+6
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ jobs:
222222
<<: *defaults
223223
stubbing-spying__window-fetch:
224224
<<: *defaults
225+
stubbing-spying__window-print:
226+
<<: *defaults
225227
stubbing-spying__google-analytics:
226228
<<: *defaults
227229
stubbing-spying__navigator:
@@ -407,6 +409,9 @@ all_jobs: &all_jobs
407409
- stubbing-spying__window-fetch:
408410
requires:
409411
- build
412+
- stubbing-spying__window-print:
413+
requires:
414+
- build
410415
- stubbing-spying__console:
411416
requires:
412417
- build
@@ -536,6 +541,7 @@ all_jobs: &all_jobs
536541
- server-communication__stream-tests
537542
- stubbing-spying__functions
538543
- stubbing-spying__window-fetch
544+
- stubbing-spying__window-print
539545
- stubbing-spying__google-analytics
540546
- stubbing-spying__navigator
541547
- stubbing-spying__window
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Stubbing `window.print`
2+
3+
This is an example showing how to stub `window.print` method call
4+
5+
## The application
6+
7+
The page [index.html](index.html) calls `window.print` on button click. Without stubbing, the test would click and then a system print dialog would block the rest of the test.
8+
9+
![System print dialog](images/print-dialog.png)
10+
11+
By stubbing the `window.print` the [spec.js](cypress/integration/spec.js) can confirm the call has happened.
12+
13+
![Stub window print test](images/stub-print.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-env browser */
2+
document.getElementById('print-window')
3+
.addEventListener('click', () => {
4+
window.print()
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"fixturesFolder": false,
3+
"pluginsFile": false,
4+
"supportFile": false
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference types="cypress" />
2+
3+
describe('window.print', () => {
4+
it('can be stubbed', () => {
5+
cy.visit('index.html')
6+
cy.window().then((win) => {
7+
cy.stub(win, 'print').as('print')
8+
})
9+
10+
cy.contains('button', 'Print').click()
11+
cy.get('@print').should('have.been.calledOnce')
12+
})
13+
})
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Stubbing Window Print</title>
6+
</head>
7+
<body>
8+
<h1>Application</h1>
9+
<ul>
10+
<li>
11+
<button id="print-window">Print 🖨</button>
12+
</li>
13+
</ul>
14+
15+
<script src="./app.js"></script>
16+
</body>
17+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "stubbing-window-print",
3+
"version": "1.0.0",
4+
"description": "Stubbing window.print from test",
5+
"scripts": {
6+
"cypress:open": "../../node_modules/.bin/cypress open",
7+
"cypress:run": "../../node_modules/.bin/cypress run",
8+
"test:ci": "npm run cypress:run"
9+
}
10+
}

0 commit comments

Comments
 (0)