-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathstopwatch.js
44 lines (30 loc) · 1.16 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
import '@testing-library/jest-dom'
import {render, wait, fireEvent} from '@testing-library/vue'
import StopWatch from './components/StopWatch.vue'
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 wait(() => {})
// Stop the timer.
await fireEvent.click(startButton)
// We can't assert a specific amount of time. Instead, we assert that the
// content has changed.
expect(elapsedTime).not.toHaveTextContent('0ms')
})
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)
await wait(() => {})
expect(console.error).not.toHaveBeenCalled()
})