Skip to content

Commit 6739874

Browse files
committed
Add explanations
1 parent 290e202 commit 6739874

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

tests/__tests__/simple-button.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import { render, cleanup, fireEvent } from '@testing-library/vue'
2-
import SimpleButton from './components/Button'
2+
import Button from './components/Button'
3+
import 'jest-dom/extend-expect'
34

45
afterEach(cleanup)
56

67
test('renders button with text', () => {
7-
const buttonText = "Click me; I'm sick"
8-
const { getByText } = render(SimpleButton, {
9-
props: { text: buttonText }
8+
const text = "Click me; I'm sick"
9+
10+
// Set the prop value by using the second argument of `render()`
11+
const { getByRole } = render(Button, {
12+
props: { text }
1013
})
1114

12-
getByText(buttonText)
15+
expect(getByRole('button')).toHaveTextContent(text)
1316
})
1417

15-
test('click event is emitted when button is clicked', () => {
18+
test('click event is emitted when button is clicked', async () => {
1619
const text = 'Click me'
17-
const { getByText, emitted } = render(SimpleButton, {
20+
21+
const { getByRole, emitted } = render(Button, {
1822
props: { text }
1923
})
20-
fireEvent.click(getByText(text))
24+
25+
// Send a click event to the element with a 'button' role
26+
await fireEvent.click(getByRole('button'))
27+
2128
expect(emitted().click).toHaveLength(1)
2229
})

tests/__tests__/stopwatch.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ test('unmounts a component', async () => {
1010
const { unmount, isUnmounted, getByText } = render(StopWatch)
1111
await fireEvent.click(getByText('Start'))
1212

13+
// Destroys a Vue component instance.
1314
unmount()
15+
1416
expect(isUnmounted()).toBe(true)
1517

1618
await wait()
19+
1720
expect(console.error).not.toHaveBeenCalled()
1821
})
1922

@@ -23,11 +26,21 @@ test('updates component state', async () => {
2326
const startButton = getByText('Start')
2427
const elapsedTime = getByTestId('elapsed')
2528

29+
// Assert initial state.
2630
expect(elapsedTime).toHaveTextContent('0ms')
31+
getByText('Start')
2732

2833
await fireEvent.click(startButton)
34+
35+
getByText('Stop')
36+
37+
// Wait for one tick of the event loop.
2938
await wait()
39+
40+
// Stop the timer.
3041
await fireEvent.click(startButton)
3142

43+
// We can't assert a specific amount of time. Instead, we assert that the
44+
// content has changed.
3245
expect(elapsedTime).not.toHaveTextContent('0ms')
3346
})

tests/__tests__/validate-plugin.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ import { render, fireEvent } from '@testing-library/vue'
55
import Validate from './components/Validate'
66

77
test('can validate using plugin', async () => {
8-
const { getByPlaceholderText, queryByTestId } = render(Validate, {}, vue =>
9-
vue.use(VeeValidate, { events: 'blur' })
8+
// The third argument of `render` is a callback function that receives the
9+
// Vue instance as a parameter. This way, we can register plugins such as
10+
// VeeValidate.
11+
const { getByPlaceholderText, queryByTestId, getByTestId } = render(
12+
Validate,
13+
{},
14+
vue => vue.use(VeeValidate, { events: 'blur' })
1015
)
1116

17+
// Assert error messages are not in the DOM when rendering the component.
18+
expect(queryByTestId('username-errors')).toBeNull()
19+
1220
const usernameInput = getByPlaceholderText('Username...')
1321
await fireEvent.touch(usernameInput)
1422

15-
expect(queryByTestId('username-errors')).toHaveTextContent(
23+
// After "touching" the input (focusing and blurring), validation error
24+
// should appear.
25+
expect(getByTestId('username-errors')).toHaveTextContent(
1626
'The username field is required.'
1727
)
1828
})

tests/__tests__/vue-router.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ const routes = [
1515
afterEach(cleanup)
1616

1717
test('full app rendering/navigating', async () => {
18+
// Notice how we pass a `routes` object to our render function.
1819
const { queryByTestId } = render(App, { routes })
1920

20-
// normally I'd use a data-testid, but just wanted to show this is also possible
2121
expect(queryByTestId('location-display')).toHaveTextContent('/')
22+
2223
await fireEvent.click(queryByTestId('about-link'))
2324

2425
expect(queryByTestId('location-display')).toHaveTextContent('/about')
2526
})
2627

2728
test('setting initial route', () => {
29+
// The callback function receives three parameters: the Vue instance where
30+
// the component is mounted, the store instance (if any) and the router
31+
// object.
2832
const { queryByTestId } = render(App, { routes }, (vue, store, router) => {
2933
router.push('/about')
3034
})

0 commit comments

Comments
 (0)