【发布时间】:2020-05-03 14:36:57
【问题描述】:
对于我的测试,我希望它以长度 39 的值(最大长度:40)开始,当我按下一个键(即:'a')时,长度应该是 40。但是,任何我尝试或查找似乎不允许我触发 keypress/keydown/keyup 事件。
我的组件:
<q-input
v-model="name"
class="my-class"
maxlength="40"
\>
我的测试:
it('Input should change', async() => {
let wrapper = mount(Component, { name: 'M'.repeat(39) })
let name = wrapper.find('.my-class')
console.log(name.vm.$options.propsData.value) // prints 39 M's as expected
name.trigger('click') // I try clicking the input. I've also tried triggering 'focus'
await wrapper.vm.$nextTick() // I've also tried with and without this
// I've also tried await name.vm.$nextTick()
name.trigger('keydown', { key: 'a' }) // According to their testing docs, this should press 'a'
// I've also tried name.trigger('keyup', { key: 'a' })
// and name.trigger('keypress', { key: 'a' })
await wrapper.vm.$nextTick()
console.log(name.vm.$options.propsData.value) // prints 39 M's without the additional 'a'
expect(name.vm.$options.propsData.value.length).toBe(40)
})
【问题讨论】:
-
如果 q-input 根标签不是输入标签,那么您应该尝试在输入而不是 q-input 根标签上触发点击并按下键
标签: vue.js jestjs vue-test-utils