【发布时间】:2020-01-19 02:25:34
【问题描述】:
我尝试测试来自bootstrap-vue 的b-tooltip.hover 指令在悬停时被调用。
似乎即使没有触发悬停事件也调用了该指令,并且buttonWrapper.trigger('mouseover'); 没有显示任何效果。
如何正确触发悬停事件?
我尝试测试的简化的Vue组件ButtonWithTooltip.vue:
<template>
<b-btn v-b-tooltip.hover="{ title: 'someText'}">Button with Tooltip</b-btn>
</template>
虽然注释了触发悬停事件的行但成功通过的单元测试:
import { createLocalVue, mount } from '@vue/test-utils';
import BootstrapVue from 'bootstrap-vue';
import ButtonWithTooltip from '@/components/ButtonWithTooltip.vue';
const localVue = createLocalVue();
localVue.use(BootstrapVue);
localVue.use(ButtonWithTooltip);
describe('ButtonWithTooltip.vue', () => {
it('shows tooltip on hover over the button', () => {
const BTooltipDirective = jest.fn();
const wrapper = mount(ButtonWithTooltip, {
localVue,
directives: { 'b-tooltip': BTooltipDirective }
});
const buttonWrapper = wrapper.find('button');
expect(buttonWrapper.exists()).toBe(true);
// buttonWrapper.trigger('mouseover'); <--- test is passed successfully even without this line
expect(BTooltipDirective).toHaveBeenCalled();
expect(BTooltipDirective.mock.calls[0][1].value).toEqual({
title: 'someText'
});
});
});
【问题讨论】:
-
这应该是和already is tested by bootstrap-vue。相反,您应该专注于测试您自己的组件的行为。
标签: vue.js vuejs2 jestjs bootstrap-vue vue-test-utils