Skip to content

Commit

Permalink
feat: support lazy modifier with setValue (#1467)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lmiller1990 committed Mar 13, 2020
1 parent ee3a93f commit afd7a82
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/test-utils/src/wrapper.js
Expand Up @@ -569,10 +569,24 @@ export default class Wrapper implements BaseWrapper {
tagName === 'TEXTAREA' ||
tagName === 'SELECT'
) {
const event = tagName === 'SELECT' ? 'change' : 'input'
// $FlowIgnore
this.element.value = value
this.trigger(event)

if (tagName === 'SELECT') {
this.trigger('change')
} else {
this.trigger('input')
}

// for v-model.lazy, we need to trigger a change event, too.
// $FlowIgnore
if (
(tagName === 'INPUT' || tagName === 'TEXTAREA') &&
this.element._vModifiers &&
this.element._vModifiers.lazy
) {
this.trigger('change')
}
} else {
throwError(`wrapper.setValue() cannot be called on this element`)
}
Expand Down
3 changes: 3 additions & 0 deletions test/resources/components/component-with-input.vue
Expand Up @@ -36,6 +36,8 @@
{{ textVal }}
{{ selectVal }}
{{ radioVal }}
<input id="lazy" type="text" v-model.lazy="lazy" />
{{ lazy }}
</div>
</template>

Expand All @@ -44,6 +46,7 @@ export default {
name: 'component-with-input',
data() {
return {
lazy: '',
checkboxVal: undefined,
textVal: undefined,
textareaVal: undefined,
Expand Down
15 changes: 15 additions & 0 deletions test/specs/wrapper/setValue.spec.js
@@ -1,5 +1,7 @@
import ComponentWithInput from '~resources/components/component-with-input.vue'
import { describeWithShallowAndMount } from '~resources/utils'
import { itDoNotRunIf } from 'conditional-specs'
import { vueVersion } from '~resources/utils'
import Vue from 'vue'

describeWithShallowAndMount('setValue', mountingMethod => {
Expand Down Expand Up @@ -28,6 +30,19 @@ describeWithShallowAndMount('setValue', mountingMethod => {
expect(wrapper.text()).to.contain('input text awesome binding')
})

itDoNotRunIf(
vueVersion < 2.1,
'updates dom with input v-model.lazy',
async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input#lazy')
input.setValue('lazy')
await Vue.nextTick()

expect(wrapper.text()).to.contain('lazy')
}
)

it('sets element of select value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select')
Expand Down

0 comments on commit afd7a82

Please sign in to comment.