Vue.js 如何使用Vue测试UTIL测试输入是否集中?
我有一个关于输入的指令。我想测试一下这个指令。唯一的问题是我找不到如何测试输入是否集中 这是我的简单模拟组件Vue.js 如何使用Vue测试UTIL测试输入是否集中?,vue.js,jestjs,vue-test-utils,Vue.js,Jestjs,Vue Test Utils,我有一个关于输入的指令。我想测试一下这个指令。唯一的问题是我找不到如何测试输入是否集中 这是我的简单模拟组件 <template> <div v-directive> <input/> </div> </template> 这是我的测试: import Vue from 'vue' import { mount , createLocalVue} from '@vue/test-utils' import Focu
<template>
<div v-directive>
<input/>
</div>
</template>
这是我的测试:
import Vue from 'vue'
import { mount , createLocalVue} from '@vue/test-utils'
import FocusInputMock from './mockComponents/FocusInputMock.vue'
import {focusInput} from '@/directives/focusInput';
const localVue = createLocalVue()
localVue.directive('directive', focusInput())
test('update content after changing state', () => {
const wrapper = mount(FocusInputMock, {
localVue
})
Vue.nextTick(function () {
expect(wrapper.find('input')) // I have to expect for this input to be focused
});
})
有人有什么想法吗?我一直在读Jest和Vue utils文档,但没有成功 安装模拟组件时,传递
{attachToDocument:true}
试试这个:
let input = wrapper.find('input').element
expect(input).toBe(document.activeElement);
附件文档
已被弃用。vue test utils文档建议改为使用attachTo
:谢谢,这是如何附加到文档正文attachTo:document.body
let input = wrapper.find('input').element
expect(input).toBe(document.activeElement);