Skip to content

Commit 5f9c49b

Browse files
committed
Add select-specific test file
1 parent aa03861 commit 5f9c49b

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

tests/__tests__/components/Form.vue

-10
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626
Awful
2727
</label>
2828

29-
<label for="genre-select">Movie genre</label>
30-
<select id="genre-select" v-model="genre">
31-
<option>Comedy</option>
32-
<option>Action</option>
33-
<option>Romance</option>
34-
<option>None of the above</option>
35-
</select>
36-
3729
<label id="recommend-label">Would you recommend this movie?</label>
3830
<input
3931
id="recommend"
@@ -56,7 +48,6 @@ export default {
5648
title: '',
5749
review: '',
5850
rating: '1',
59-
genre: 'Comedy',
6051
recommend: false
6152
}
6253
},
@@ -73,7 +64,6 @@ export default {
7364
title: this.title,
7465
review: this.review,
7566
rating: this.rating,
76-
genre: this.genre,
7767
recommend: this.recommend
7868
})
7969
}

tests/__tests__/components/Select.vue

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div>
3+
<label for="dino-select">Choose a dinosaur:</label>
4+
<select id="dino-select" v-model="selectedDino">
5+
<option value="dino1">Tyrannosaurus</option>
6+
<option value="dino2">Velociraptor</option>
7+
<option value="dino3">Deinonychus</option>
8+
<optgroup label="Sauropods">
9+
<option value="dino4">Diplodocus</option>
10+
<option value="dino5">Saltasaurus</option>
11+
<option value="dino6">Apatosaurus</option>
12+
</optgroup>
13+
</select>
14+
</div>
15+
</template>
16+
17+
<script>
18+
export default {
19+
data() {
20+
return {
21+
selectedDino: 'dino1'
22+
}
23+
}
24+
}
25+
</script>

tests/__tests__/form.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ import { render, fireEvent } from '@testing-library/vue'
22
import '@testing-library/jest-dom/extend-expect'
33
import Form from './components/Form'
44

5+
// In this test we showcase several ways of targetting DOM elements.
6+
// However, `getByLabelText` should be your top preference when handling
7+
// form elements.
8+
// Read 'What queries should I use?' for additional context:
9+
// https://testing-library.com/docs/guide-which-query
510
test('Review form submits', async () => {
611
const fakeReview = {
712
title: 'An Awesome Movie',
813
review: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
914
rating: '3',
10-
genre: 'Action',
1115
recommend: true
1216
}
1317

1418
const {
1519
getByLabelText,
1620
getByText,
1721
getByRole,
18-
getByDisplayValue,
1922
getByPlaceholderText,
2023
emitted
2124
} = render(Form)
@@ -25,12 +28,6 @@ test('Review form submits', async () => {
2528
// Initially the submit button should be disabled
2629
expect(submitButton).toBeDisabled()
2730

28-
// In this test we showcase several ways of targetting DOM elements.
29-
// However, `getByLabelText` should be your top preference when handling
30-
// form elements.
31-
// Read 'What queries should I use?' for additional context:
32-
// https://testing-library.com/docs/guide-which-query
33-
3431
const titleInput = getByLabelText(/title of the movie/i)
3532
await fireEvent.update(titleInput, fakeReview.title)
3633

@@ -49,10 +46,6 @@ test('Review form submits', async () => {
4946
expect(ratingSelect.checked).toBe(true)
5047
expect(initiallySelectedInput.checked).toBe(false)
5148

52-
// Get the Select element by using the initially displayed value.
53-
const genreSelect = getByDisplayValue('Comedy')
54-
await fireEvent.update(genreSelect, fakeReview.genre)
55-
5649
// Get the Input element by its implicit ARIA role.
5750
const recommendInput = getByRole('checkbox')
5851

tests/__tests__/select.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { render, fireEvent } from '@testing-library/vue'
2+
import 'jest-dom/extend-expect'
3+
import Select from './components/Select'
4+
5+
// There are several ways to interact with a Select component.
6+
test('Select component', async () => {
7+
const { getByDisplayValue, getByText } = render(Select)
8+
9+
// Get the Select element by using the initially displayed value.
10+
const select = getByDisplayValue('Tyrannosaurus')
11+
12+
// Assert initial value
13+
expect(select.value).toBe('dino1')
14+
15+
// Update it by manually sending an option value
16+
await fireEvent.update(select, 'dino2')
17+
expect(select.value).toBe('dino2')
18+
19+
// We can also get the option value from the element itself
20+
await fireEvent.update(select, getByText('Tyrannosaurus').value)
21+
expect(select.value).toBe('dino1')
22+
23+
// We can trigger an update event by directly getting the <option> element
24+
await fireEvent.update(getByText('Deinonychus'))
25+
expect(select.value).toBe('dino3')
26+
27+
// ...even if option is within an <optgroup>
28+
await fireEvent.update(getByText('Diplodocus'))
29+
expect(select.value).toBe('dino4')
30+
})

0 commit comments

Comments
 (0)