Skip to content

Commit c8e2594

Browse files
author
Jesse Knowles
committed
Make fireEvent.update work with lazy modifier
- switched to use change event if lazy modifier is found on element - updated form unit test to test lazy input and textarea
1 parent 4376e64 commit c8e2594

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/__tests__/components/Form.vue

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<label for="movie-input">Title of the movie</label>
66
<input id="movie-input" v-model="title" name="title" />
77

8+
<label for="director-input">Director of the movie</label>
9+
<input id="director-input" v-model.lazy="director" name="director" />
10+
811
<label id="review-textarea">Your review</label>
912
<textarea
1013
v-model="review"
@@ -13,6 +16,14 @@
1316
aria-labelledby="review-textarea"
1417
/>
1518

19+
<label id="notes-textarea">Add some notes</label>
20+
<textarea
21+
v-model.lazy="notes"
22+
name="notes-textarea"
23+
placeholder="Add some notes"
24+
aria-labelledby="notes-textarea"
25+
/>
26+
1627
<label>
1728
<input v-model="rating" type="radio" value="3" />
1829
Wonderful
@@ -46,7 +57,9 @@ export default {
4657
data() {
4758
return {
4859
title: '',
60+
director: '',
4961
review: '',
62+
notes: '',
5063
rating: '1',
5164
recommend: false,
5265
}
@@ -63,7 +76,9 @@ export default {
6376
6477
this.$emit('submit', {
6578
title: this.title,
79+
director: this.director,
6680
review: this.review,
81+
notes: this.notes,
6782
rating: this.rating,
6883
recommend: this.recommend,
6984
})

src/__tests__/form.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {render, fireEvent} from '@testing-library/vue'
1+
import {render, fireEvent, waitFor} from '@testing-library/vue'
22
import '@testing-library/jest-dom'
33
import Form from './components/Form'
44

@@ -11,7 +11,9 @@ test('Review form submits', async () => {
1111
jest.spyOn(console, 'warn').mockImplementation(() => {})
1212
const fakeReview = {
1313
title: 'An Awesome Movie',
14+
director: 'Stephen Spielberg',
1415
review: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
16+
notes: 'Add something',
1517
rating: '3',
1618
}
1719

@@ -31,9 +33,15 @@ test('Review form submits', async () => {
3133
const titleInput = getByLabelText(/title of the movie/i)
3234
await fireEvent.update(titleInput, fakeReview.title)
3335

36+
const directorInput = getByLabelText(/director of the movie/i)
37+
await fireEvent.update(directorInput, fakeReview.director)
38+
3439
const reviewTextarea = getByPlaceholderText('Write an awesome review')
3540
await fireEvent.update(reviewTextarea, fakeReview.review)
3641

42+
const notesTextarea = getByPlaceholderText('Add some notes')
43+
await fireEvent.update(notesTextarea, fakeReview.notes)
44+
3745
// Rating Radio buttons.
3846
const initiallySelectedInput = getByLabelText('Awful')
3947
const ratingSelect = getByLabelText('Wonderful')

src/vue-testing-library.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable testing-library/no-wait-for-empty-callback */
22
import {createLocalVue, mount} from '@vue/test-utils'
3+
import Vue from 'vue'
34

45
import {
56
getQueriesForElement,
@@ -138,7 +139,7 @@ fireEvent.touch = async elem => {
138139
// Small utility to provide a better experience when working with v-model.
139140
// Related upstream issue: https://github.com/vuejs/vue-test-utils/issues/345#issuecomment-380588199
140141
// Examples: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/form.js
141-
fireEvent.update = (elem, value) => {
142+
fireEvent.update = async (elem, value, isLazy = false) => {
142143
const tagName = elem.tagName
143144
const type = elem.type
144145

@@ -160,13 +161,13 @@ fireEvent.update = (elem, value) => {
160161
return fireEvent.change(elem)
161162
} else {
162163
elem.value = value
163-
return fireEvent.input(elem)
164+
return elem._vModifiers.lazy ? fireEvent.change(elem) : fireEvent.input(elem)
164165
}
165166
}
166167

167168
case 'TEXTAREA': {
168169
elem.value = value
169-
return fireEvent.input(elem)
170+
return elem._vModifiers.lazy ? fireEvent.change(elem) : fireEvent.input(elem)
170171
}
171172

172173
case 'SELECT': {

0 commit comments

Comments
 (0)