MCPcopy Create free account
hub / github.com/testing-library/react-testing-library / StopWatch

Class StopWatch

src/__tests__/stopwatch.js:4–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import {render, fireEvent, screen} from '../'
3
4class StopWatch extends React.Component {
5 state = {lapse: 0, running: false}
6 handleRunClick = () => {
7 this.setState(state => {
8 if (state.running) {
9 clearInterval(this.timer)
10 } else {
11 const startTime = Date.now() - this.state.lapse
12 this.timer = setInterval(() => {
13 this.setState({lapse: Date.now() - startTime})
14 })
15 }
16 return {running: !state.running}
17 })
18 }
19 handleClearClick = () => {
20 clearInterval(this.timer)
21 this.setState({lapse: 0, running: false})
22 }
23 componentWillUnmount() {
24 clearInterval(this.timer)
25 }
26 render() {
27 const {lapse, running} = this.state
28 return (
29 <div>
30 <span>{lapse}ms</span>
31 <button onClick={this.handleRunClick}>
32 {running ? 'Stop' : 'Start'}
33 </button>
34 <button onClick={this.handleClearClick}>Clear</button>
35 </div>
36 )
37 }
38}
39
40const sleep = t => new Promise(resolve => setTimeout(resolve, t))
41

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…