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

Commit 30bf202

Browse files
committed
add timers tests
1 parent b2b10fe commit 30bf202

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example from https://reactjs.org/docs/testing-recipes.html#timers
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference types="cypress" />
2+
import Card from './card.jsx'
3+
import React from 'react'
4+
import { mount } from 'cypress-react-unit-tests'
5+
6+
// NOTE: seems our clock control does not work with effect hooks
7+
it.skip('should select null after timing out', () => {
8+
const onSelect = cy.stub()
9+
cy.clock()
10+
mount(<Card onSelect={onSelect} />)
11+
cy.tick(100).then(() => {
12+
expect(onSelect).to.not.have.been.called
13+
})
14+
cy.tick(5000).then(() => {
15+
expect(onSelect).to.have.been.calledWith(null)
16+
})
17+
})
18+
19+
it('should accept selections', () => {
20+
const onSelect = cy.stub()
21+
mount(<Card onSelect={onSelect} />)
22+
cy.get("[data-testid='2']")
23+
.click()
24+
.then(() => {
25+
expect(onSelect).to.have.been.calledWith(2)
26+
})
27+
})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference types="cypress" />
2+
import Card from './card-without-effect.jsx'
3+
import React from 'react'
4+
import { mount } from 'cypress-react-unit-tests'
5+
import { unmountComponentAtNode } from 'react-dom'
6+
7+
it('should select null after timing out', () => {
8+
const onSelect = cy.stub()
9+
cy.clock()
10+
mount(<Card onSelect={onSelect} />)
11+
cy.tick(100).then(() => {
12+
expect(onSelect).to.not.have.been.called
13+
})
14+
cy.tick(5000).then(() => {
15+
expect(onSelect).to.have.been.calledWith(null)
16+
})
17+
})
18+
19+
it('should cleanup on being removed', () => {
20+
const onSelect = cy.stub()
21+
cy.clock()
22+
mount(<Card onSelect={onSelect} />)
23+
cy.tick(100).then(() => {
24+
expect(onSelect).to.not.have.been.called
25+
})
26+
27+
cy.get('#cypress-jsdom').then($el => {
28+
unmountComponentAtNode($el[0])
29+
})
30+
31+
cy.tick(5000).then(() => {
32+
expect(onSelect).to.not.have.been.called
33+
})
34+
})
35+
36+
it('should accept selections', () => {
37+
const onSelect = cy.stub()
38+
mount(<Card onSelect={onSelect} />)
39+
cy.get("[data-testid='2']")
40+
.click()
41+
.then(() => {
42+
expect(onSelect).to.have.been.calledWith(2)
43+
})
44+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { Component } from 'react'
2+
3+
export default class Card extends Component {
4+
componentDidMount() {
5+
this._timeoutID = setTimeout(() => {
6+
this.props.onSelect(null)
7+
}, 5000)
8+
}
9+
10+
componentWillUnmount() {
11+
clearTimeout(this._timeoutID)
12+
}
13+
14+
render() {
15+
return [1, 2, 3, 4].map(choice => (
16+
<button
17+
key={choice}
18+
data-testid={choice}
19+
onClick={() => this.props.onSelect(choice)}
20+
>
21+
{choice}
22+
</button>
23+
))
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { useEffect } from 'react'
2+
3+
export default function Card(props) {
4+
useEffect(() => {
5+
const timeoutID = setTimeout(() => {
6+
props.onSelect(null)
7+
}, 5000)
8+
return () => {
9+
clearTimeout(timeoutID)
10+
}
11+
}, [props.onSelect])
12+
13+
return [1, 2, 3, 4].map(choice => (
14+
<button
15+
key={choice}
16+
data-testid={choice}
17+
onClick={() => props.onSelect(choice)}
18+
>
19+
{choice}
20+
</button>
21+
))
22+
}

0 commit comments

Comments
 (0)