Skip to content

Commit 3e652f9

Browse files
committed
Add form use cases
1 parent 2f45449 commit 3e652f9

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

tests/__tests__/components/Signup.vue

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
data-testid="username-input"
99
placeholder="Username..."
1010
name="username" >
11+
1112
<label id="password-label">Password</label>
1213
<input
1314
v-model="password"
@@ -16,6 +17,29 @@
1617
name="password"
1718
aria-labelledby="password-label"
1819
>
20+
21+
<label id="about-me">About Me</label>
22+
<textarea
23+
v-model="about"
24+
name="about-me"
25+
aria-labelledby="about-me"
26+
/>
27+
28+
<label for="preference-select">I prefer...</label>
29+
<select
30+
id="preference-select"
31+
v-model="selected"
32+
name="preference-select"
33+
>
34+
<option
35+
disabled
36+
value=""
37+
/>
38+
<option>Dogs</option>
39+
<option>Cats</option>
40+
<option>None of the above</option>
41+
</select>
42+
1943
<label id="remember-me-label">Remember Me</label>
2044
<input
2145
id="remember-me"
@@ -24,31 +48,42 @@
2448
name="remember-me"
2549
aria-labelledby="remember-me-label"
2650
>
27-
<button type="submit">Submit</button>
51+
52+
<button
53+
:disabled="submitDisabled"
54+
type="submit"
55+
>
56+
Submit
57+
</button>
2858
</form>
2959
</div>
3060
</template>
3161

3262
<script>
3363
export default {
34-
props: {
35-
onSubmit: {
36-
type: Function,
37-
required: true
38-
}
39-
},
4064
data () {
4165
return {
4266
username: '',
4367
password: '',
68+
about: '',
69+
selected: '',
4470
rememberMe: false
4571
}
4672
},
73+
computed: {
74+
submitDisabled () {
75+
return !this.username || !this.password || !this.about || !this.selected
76+
}
77+
},
4778
methods: {
4879
submit () {
49-
this.onSubmit({
80+
if (this.submitDisabled) return
81+
82+
this.$emit('submit', {
5083
username: this.username,
5184
password: this.password,
85+
about: this.about,
86+
selected: this.selected,
5287
rememberMe: this.rememberMe
5388
})
5489
}

tests/__tests__/signup.js

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

4-
test('login form submits', async () => {
5-
const fakeUser = { username: 'jackiechan', password: 'hiya! 🥋', rememberMe: true }
6-
const handleSubmit = jest.fn()
7-
const { getByLabelText, getByText } = render(
8-
Login, { props: { onSubmit: handleSubmit } }
9-
)
5+
test('signup form submits', async () => {
6+
const fakeUser = {
7+
username: 'jackiechan',
8+
password: 'hiya! 🥋',
9+
about: 'Lorem ipsum dolor sit amet',
10+
selected: 'None of the above',
11+
rememberMe: true
12+
}
1013

11-
const submitButtonNode = getByText('Submit')
14+
const { getByLabelText, getByText, emitted } = render(Signup)
15+
16+
const submitButton = getByText('Submit')
17+
18+
// Initially the form should be disabled
19+
expect(submitButton).toBeDisabled()
1220

1321
const userNameInput = getByLabelText('Username')
1422
await fireEvent.update(userNameInput, fakeUser.username)
1523

1624
const passwordInput = getByLabelText('Password')
1725
await fireEvent.update(passwordInput, fakeUser.password)
1826

27+
const aboutMeTextarea = getByLabelText('About Me')
28+
await fireEvent.update(aboutMeTextarea, fakeUser.about)
29+
1930
const rememberMeInput = getByLabelText('Remember Me')
20-
await fireEvent.update(rememberMeInput, true)
31+
await fireEvent.update(rememberMeInput, fakeUser.rememberMe)
32+
33+
const preferenceSelect = getByLabelText('I prefer...')
34+
await fireEvent.update(preferenceSelect, fakeUser.selected)
2135

2236
// NOTE: in jsdom, it's not possible to trigger a form submission
2337
// by clicking on the submit button. This is really unfortunate.
2438
// So the next best thing is to fireEvent a submit on the form itself
2539
// then ensure that there's a submit button.
26-
await fireEvent.click(submitButtonNode)
40+
expect(submitButton).toBeEnabled()
41+
expect(submitButton).toHaveAttribute('type', 'submit')
42+
await fireEvent.click(submitButton)
2743

28-
// Assert
29-
expect(handleSubmit).toHaveBeenCalledTimes(1)
30-
expect(handleSubmit).toHaveBeenCalledWith(fakeUser)
31-
// make sure the form is submittable
32-
expect(submitButtonNode.type).toBe('submit')
44+
// Assert event has been emitted.
45+
expect(emitted().submit).toHaveLength(1)
46+
expect(emitted().submit[0]).toEqual([ fakeUser ])
3347
})

0 commit comments

Comments
 (0)