Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 54355a5

Browse files
committed
explain the stubs better
1 parent 154559a commit 54355a5

File tree

5 files changed

+115
-18
lines changed

5 files changed

+115
-18
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Stub examples
2+
3+
You can pass a [cy.stub](https://on.cypress.io/stub) as a property to the mounted component and then assert it was invoked.
4+
5+
```js
6+
it('calls the click prop', () => {
7+
const onClick = cy.stub().as('clicker')
8+
mount(<Clicker click={onClick} />)
9+
cy.get('button')
10+
.click()
11+
.click()
12+
cy.get('@clicker').should('have.been.calledTwice')
13+
})
14+
```
15+
16+
![Stub](images/stub.png)
17+
18+
## Tests
19+
20+
- [clicker-spec.js](clicker-spec.js) passes a stub to the component and asserts it was called
21+
- [clicker-with-delay-spec.js](clicker-with-delay-spec.js) passes a stub to the component that is called on click, but only after a delay. Shows the difference between [.then](https://on.cypress.io/then) and [.should](https://on.cypress.io/should). Shows my favorite method of querying stubs via an alias
22+
23+
```js
24+
const onClick = cy.stub().as('clicker')
25+
mount(<Clicker click={onClick} />)
26+
...
27+
cy.get('@clicker').should('have.been.calledTwice')
28+
```
29+
30+
## More info
31+
32+
Watch [Assert that the stub was called twice](https://youtu.be/AlltFcsIFvc) showing these specs.
33+
34+
Read [Stubs, spies and clocks](https://on.cypress.io/stubs-spies-and-clocks) and [Retry-ability](https://on.cypress.io/retry-ability).
Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import React from 'react'
22
import { mount } from 'cypress-react-unit-test'
3-
import Clicker from './clicker.jsx'
4-
5-
it('passes stub to the component', () => {
6-
const onClick = cy.stub().as('clicker')
7-
mount(<Clicker click={onClick} />)
8-
cy.get('button')
9-
.click()
10-
.click()
11-
cy.get('@clicker').should('have.been.calledTwice')
3+
4+
describe('Clicker', () => {
5+
const Clicker = ({ click }) => (
6+
<div>
7+
<button onClick={click}>Click me</button>
8+
</div>
9+
)
10+
11+
it('calls the click prop twice', () => {
12+
const onClick = cy.stub()
13+
mount(<Clicker click={onClick} />)
14+
cy.get('button')
15+
.click()
16+
.click()
17+
.then(() => {
18+
// works in this case, but not recommended
19+
// because https://on.cypress.io/then does not retry
20+
expect(onClick).to.be.calledTwice
21+
})
22+
})
23+
24+
it('calls the click prop: best practice', () => {
25+
const onClick = cy.stub().as('clicker')
26+
mount(<Clicker click={onClick} />)
27+
cy.get('button')
28+
.click()
29+
.click()
30+
31+
// good practice 💡
32+
// auto-retry the stub until it was called twice
33+
cy.get('@clicker').should('have.been.calledTwice')
34+
})
1235
})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react'
2+
import { mount } from 'cypress-react-unit-test'
3+
4+
describe('Clicker with delay', () => {
5+
const Clicker = ({ click }) => (
6+
<div>
7+
<button onClick={() => setTimeout(click, 500)}>Click me</button>
8+
</div>
9+
)
10+
11+
// Skipped because .then does not retry
12+
// and will fail as soon as "expect" throws an error
13+
it.skip('calls the click prop: then', () => {
14+
const onClick = cy.stub()
15+
mount(<Clicker click={onClick} />)
16+
cy.get('button')
17+
.click()
18+
.click()
19+
.then(() => {
20+
expect(onClick).to.be.calledTwice
21+
})
22+
})
23+
24+
it('calls the click prop: should', () => {
25+
const onClick = cy.stub()
26+
mount(<Clicker click={onClick} />)
27+
cy.get('button')
28+
.click()
29+
.click()
30+
// test works because .should retries the assertion
31+
// and in this case it will not click multiple times
32+
// but just retry the assertion
33+
.should(() => {
34+
expect(onClick).to.be.calledTwice
35+
})
36+
})
37+
38+
it('calls the click prop', () => {
39+
const onClick = cy.stub().as('clicker')
40+
mount(<Clicker click={onClick} />)
41+
cy.get('button')
42+
.click()
43+
.click()
44+
45+
// good practice 💡
46+
// auto-retry the stub until it was called twice
47+
cy.get('@clicker').should('have.been.calledTwice')
48+
})
49+
})

cypress/component/basic/stub-example/clicker.jsx

Lines changed: 0 additions & 9 deletions
This file was deleted.
180 KB
Loading

0 commit comments

Comments
 (0)