Skip to content

Commit 3479a9e

Browse files
committed
Showcase several selectors
1 parent 3e652f9 commit 3479a9e

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

tests/__tests__/components/Signup.vue

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55
<input
66
id="username-input"
77
v-model="username"
8-
data-testid="username-input"
9-
placeholder="Username..."
108
name="username" >
119

12-
<label id="password-label">Password</label>
13-
<input
14-
v-model="password"
15-
placeholder="Password..."
16-
type="password"
17-
name="password"
18-
aria-labelledby="password-label"
19-
>
20-
2110
<label id="about-me">About Me</label>
2211
<textarea
2312
v-model="about"
2413
name="about-me"
14+
placeholder="I was born in..."
2515
aria-labelledby="about-me"
2616
/>
2717

@@ -46,7 +36,7 @@
4636
v-model="rememberMe"
4737
type="checkbox"
4838
name="remember-me"
49-
aria-labelledby="remember-me-label"
39+
data-testid="remember-input"
5040
>
5141

5242
<button
@@ -64,15 +54,14 @@ export default {
6454
data () {
6555
return {
6656
username: '',
67-
password: '',
6857
about: '',
69-
selected: '',
58+
selected: 'Dogs',
7059
rememberMe: false
7160
}
7261
},
7362
computed: {
7463
submitDisabled () {
75-
return !this.username || !this.password || !this.about || !this.selected
64+
return !this.username || !this.about || !this.selected
7665
}
7766
},
7867
methods: {
@@ -81,7 +70,6 @@ export default {
8170
8271
this.$emit('submit', {
8372
username: this.username,
84-
password: this.password,
8573
about: this.about,
8674
selected: this.selected,
8775
rememberMe: this.rememberMe

tests/__tests__/signup.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,42 @@ import Signup from './components/Signup'
55
test('signup form submits', async () => {
66
const fakeUser = {
77
username: 'jackiechan',
8-
password: 'hiya! 🥋',
98
about: 'Lorem ipsum dolor sit amet',
109
selected: 'None of the above',
1110
rememberMe: true
1211
}
1312

14-
const { getByLabelText, getByText, emitted } = render(Signup)
13+
const {
14+
getByLabelText,
15+
getByText,
16+
getByTestId,
17+
getByDisplayValue,
18+
getByPlaceholderText,
19+
emitted
20+
} = render(Signup)
1521

1622
const submitButton = getByText('Submit')
1723

1824
// Initially the form should be disabled
1925
expect(submitButton).toBeDisabled()
2026

21-
const userNameInput = getByLabelText('Username')
27+
// We are gonna showcase several ways of targetting DOM elements.
28+
// However, `getByLabelText` should be your top preference when handling
29+
// form elements.
30+
//
31+
// Read 'What queries should I use?' for additional context:
32+
// https://testing-library.com/docs/guide-which-query
33+
const userNameInput = getByLabelText(/username/i)
2234
await fireEvent.update(userNameInput, fakeUser.username)
2335

24-
const passwordInput = getByLabelText('Password')
25-
await fireEvent.update(passwordInput, fakeUser.password)
26-
27-
const aboutMeTextarea = getByLabelText('About Me')
36+
const aboutMeTextarea = getByPlaceholderText('I was born in...')
2837
await fireEvent.update(aboutMeTextarea, fakeUser.about)
2938

30-
const rememberMeInput = getByLabelText('Remember Me')
39+
const rememberMeInput = getByTestId('remember-input')
3140
await fireEvent.update(rememberMeInput, fakeUser.rememberMe)
3241

33-
const preferenceSelect = getByLabelText('I prefer...')
42+
// Get the Select element by using the initially displayed value.
43+
const preferenceSelect = getByDisplayValue('Dogs')
3444
await fireEvent.update(preferenceSelect, fakeUser.selected)
3545

3646
// NOTE: in jsdom, it's not possible to trigger a form submission
@@ -39,6 +49,7 @@ test('signup form submits', async () => {
3949
// then ensure that there's a submit button.
4050
expect(submitButton).toBeEnabled()
4151
expect(submitButton).toHaveAttribute('type', 'submit')
52+
4253
await fireEvent.click(submitButton)
4354

4455
// Assert event has been emitted.

0 commit comments

Comments
 (0)