Skip to content

Commit afd7a82

Browse files
authored
feat: support lazy modifier with setValue (#1467)
* feat: support lazy modifier with setValue * fix: check lazy modifier * fix: add check for vModifiers * fix: include textarea in lazy check * fix: skip lazy modifier spec for Vue 2.0
1 parent ee3a93f commit afd7a82

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,24 @@ export default class Wrapper implements BaseWrapper {
569569
tagName === 'TEXTAREA' ||
570570
tagName === 'SELECT'
571571
) {
572-
const event = tagName === 'SELECT' ? 'change' : 'input'
573572
// $FlowIgnore
574573
this.element.value = value
575-
this.trigger(event)
574+
575+
if (tagName === 'SELECT') {
576+
this.trigger('change')
577+
} else {
578+
this.trigger('input')
579+
}
580+
581+
// for v-model.lazy, we need to trigger a change event, too.
582+
// $FlowIgnore
583+
if (
584+
(tagName === 'INPUT' || tagName === 'TEXTAREA') &&
585+
this.element._vModifiers &&
586+
this.element._vModifiers.lazy
587+
) {
588+
this.trigger('change')
589+
}
576590
} else {
577591
throwError(`wrapper.setValue() cannot be called on this element`)
578592
}

test/resources/components/component-with-input.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
{{ textVal }}
3737
{{ selectVal }}
3838
{{ radioVal }}
39+
<input id="lazy" type="text" v-model.lazy="lazy" />
40+
{{ lazy }}
3941
</div>
4042
</template>
4143

@@ -44,6 +46,7 @@ export default {
4446
name: 'component-with-input',
4547
data() {
4648
return {
49+
lazy: '',
4750
checkboxVal: undefined,
4851
textVal: undefined,
4952
textareaVal: undefined,

test/specs/wrapper/setValue.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import ComponentWithInput from '~resources/components/component-with-input.vue'
22
import { describeWithShallowAndMount } from '~resources/utils'
3+
import { itDoNotRunIf } from 'conditional-specs'
4+
import { vueVersion } from '~resources/utils'
35
import Vue from 'vue'
46

57
describeWithShallowAndMount('setValue', mountingMethod => {
@@ -28,6 +30,19 @@ describeWithShallowAndMount('setValue', mountingMethod => {
2830
expect(wrapper.text()).to.contain('input text awesome binding')
2931
})
3032

33+
itDoNotRunIf(
34+
vueVersion < 2.1,
35+
'updates dom with input v-model.lazy',
36+
async () => {
37+
const wrapper = mountingMethod(ComponentWithInput)
38+
const input = wrapper.find('input#lazy')
39+
input.setValue('lazy')
40+
await Vue.nextTick()
41+
42+
expect(wrapper.text()).to.contain('lazy')
43+
}
44+
)
45+
3146
it('sets element of select value', () => {
3247
const wrapper = mountingMethod(ComponentWithInput)
3348
const select = wrapper.find('select')

0 commit comments

Comments
 (0)