嗨,我是玩笑和单元测试的新手。我想问如何使用 vue test utils 设置值文本输入。
很快我就有了用于文本输入的自定义组件,这是我的代码
<input
v-model="local_value"
@keyup.enter="submitToParent"
:class="input_class"
:id="id"
:disabled="is_disabled"
:maxlength="max_length"
:placeholder="placeholder"
:autocomplete="(is_autocomplete) ? 'on' : 'off'"
:name="id"
:ref="id"
/>
这是我的测试
it("type something on field", async () => {
const wrapper = shallowMount(TextInput, {
propsData: {
id: "my_input",
}
})
// find component (its work properly) and I want to try inserting some text
const input = wrapper.findComponent({ref: "my_input"})
input.element.value = "sample text"
input.setValue("sample text")
// the value still empty string (""), idk what happens with my code
console.log(wrapper.vm.local_value)
expect(wrapper.vm.local_value).toBe("sample text")
请告诉我您是否知道此问题的解决方案,谢谢您的宝贵时间
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
据我所知,
setValue是异步的,所以你可能需要设置它await input.setValue('示例文本')