-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathstopwatch.js
39 lines (29 loc) · 1.03 KB
/
stopwatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import '@testing-library/jest-dom'
import {render, waitFor, fireEvent} from '..'
import StopWatch from './components/StopWatch.vue'
const sleep = ms =>
new Promise(resolve => {
setTimeout(resolve, ms)
})
test('updates component state', async () => {
const {getByTestId, getByText} = render(StopWatch)
const startButton = getByText('Start')
const elapsedTime = getByTestId('elapsed')
// Assert initial state.
expect(elapsedTime).toHaveTextContent('0ms')
expect(getByText('Start')).toBeInTheDocument()
await fireEvent.click(startButton)
expect(getByText('Stop')).toBeInTheDocument()
// Wait for one tick of the event loop.
await waitFor(() => {
expect(elapsedTime).not.toHaveTextContent('0ms')
})
const timeBeforeStop = elapsedTime.textContent
// Stop the timer.
await fireEvent.click(startButton)
// Wait for a few milliseconds
await sleep(5)
// We can't assert a specific amount of time. Instead, we assert that the
// content has changed.
expect(elapsedTime).toHaveTextContent(timeBeforeStop)
})