-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathstopwatch.js
53 lines (37 loc) · 1.38 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import '@testing-library/jest-dom'
import {render, waitFor, fireEvent} from '@testing-library/vue'
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')
getByText('Start')
await fireEvent.click(startButton)
getByText('Stop')
// 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)
})
test('unmounts a component', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
const {unmount, isUnmounted, getByText} = render(StopWatch)
await fireEvent.click(getByText('Start'))
// Destroys a Vue component instance.
unmount()
expect(isUnmounted()).toBe(true)
// Wait for a few milliseconds
await sleep(5)
expect(console.error).not.toHaveBeenCalled()
})