使用Vue-test-utils测试Vuetify – 无法将值设置到v-text-field

huangapple go评论49阅读模式
英文:

Testing Vuetify with Vue-test-utils - Cannot set value to v-text-field

问题

我想测试一个简单的登录表单,其中包含两个v-text-field和一个v-btn。我正在使用Jest和Vue测试工具进行测试。

代码看起来像这样:

<v-text-field
    v-model="form.email"
    test-id="input-email">
</v-text-field>

我的测试看起来像这样:

import Login from './login.vue'
import { shallowMount } from '@vue/test-utils'

describe('login', () => {
  it('set value', () => {
    const wrapper = shallowMount(Login)
    const emailInput = wrapper.findComponent('[test-id="input-email"]')
    emailInput.setValue('john@example.com')
  })
})

我得到了错误消息:

[vue-test-utils]: wrapper.setValue() 不能在此元素上调用

并指向这里:

emailInput.setValue('john@example.com')

如何为v-text-field设置值?

英文:

I want to test simple login form with two v-text-fields and one v-btn. I'm testing with Jest and Vue test utils.

Code looks like that:

&lt;v-text-field
    v-model=&quot;form.email&quot;
    test-id=&quot;input-email&quot;&gt;
&lt;/v-text-field&gt;

My test looks like that:

import Login from &#39;./login.vue&#39;
import { shallowMount } from &#39;@vue/test-utils&#39;

describe(&#39;login&#39;, () =&gt; {
  it(&#39;set value&#39;, () =&gt; {
    const wrapper = shallowMount(Login)
    const emailInput = wrapper.findComponent(&#39;[test-id=&quot;input-email&quot;]&#39;)
    emailInput.setValue(&#39;john@example.com&#39;)
  })
})

And I'm getting error:

>[vue-test-utils]: wrapper.setValue() cannot be called on this element

and points here:
emailInput.setValue(&#39;john@example.com&#39;)

How can I set value to v-text-field?

答案1

得分: 1

I tried to repeat your example and it works

Component:

<template>
  <v-text-field
    v-model="form.email"
    test-id="input-email"
  />
</template>

<script>
export default {
  data () {
    return {
      form: { email: '' },
    }
  },
}
</script>

Test:

import Input from './input.vue';
import { mount } from '@vue/test-utils';

describe('input', () => {
  it('should set v-model', () => {
    const wrapper = mount(Input);
    const input = wrapper.findComponent('[test-id="input-email"]');
    input.setValue('test');
  });
})
英文:

I tried to repeat your example and it works

Component:

&lt;template&gt;
  &lt;v-text-field
    v-model=&quot;form.email&quot;
    test-id=&quot;input-email&quot;
  /&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data () {
    return {
      form: { email: &#39;&#39; },
    }
  },
}
&lt;/script&gt;

Test:

import Input from &#39;./input.vue&#39;
import { mount } from &#39;@vue/test-utils&#39;

describe(&#39;input&#39;, () =&gt; {
  it(&#39;should set v-model&#39;, () =&gt; {
    const wrapper = mount(Input)
    const input = wrapper.findComponent(&#39;[test-id=&quot;input-email&quot;]&#39;)
    input.setValue(&#39;test&#39;)
  })
})

huangapple
  • 本文由 发表于 2023年2月20日 00:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定