2019-01-12 03:33:27 +00:00
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import Tooltip from '..';
|
2020-03-07 11:45:13 +00:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2018-06-10 13:53:52 +00:00
|
|
|
|
|
|
|
describe('Tooltip', () => {
|
2020-03-07 11:45:13 +00:00
|
|
|
mountTest(Tooltip);
|
2018-06-10 13:53:52 +00:00
|
|
|
it('check `onVisibleChange` arguments', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onVisibleChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
props: ['title', 'visible'],
|
|
|
|
render() {
|
|
|
|
const props = {
|
2020-08-04 10:13:51 +00:00
|
|
|
title: this.title || '',
|
|
|
|
mouseEnterDelay: 0,
|
|
|
|
mouseLeaveDelay: 0,
|
|
|
|
onVisibleChange,
|
2021-03-16 07:39:44 +00:00
|
|
|
// TODO test/util 的bug 没有使用 v-model 同样会改变
|
|
|
|
'onUpdate:visible': () => {
|
|
|
|
props.visible = this.visible;
|
|
|
|
this.$forceUpdate();
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
if (this.visible !== undefined) {
|
2020-08-04 10:13:51 +00:00
|
|
|
props.visible = this.visible;
|
2019-01-12 03:33:27 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Tooltip ref="tooltip" {...props}>
|
|
|
|
<div id="hello">Hello world!</div>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
},
|
2018-06-10 13:53:52 +00:00
|
|
|
},
|
2020-08-04 10:13:51 +00:00
|
|
|
{ sync: false, attachTo: 'body' },
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-10 13:53:52 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let div = null;
|
|
|
|
let lastCount = null;
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
// `title` is empty.
|
2020-08-04 10:13:51 +00:00
|
|
|
div = wrapper.findAll('#hello')[0].element;
|
|
|
|
div.dispatchEvent(new MouseEvent('mouseenter'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange).not.toHaveBeenCalled();
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(false);
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-04 10:13:51 +00:00
|
|
|
div.dispatchEvent(new MouseEvent('mouseleave'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange).not.toHaveBeenCalled();
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(false);
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
// update `title` value.
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.setProps({ title: 'Have a nice day!' });
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-04 10:13:51 +00:00
|
|
|
wrapper.findAll('#hello')[0].element.dispatchEvent(new MouseEvent('mouseenter'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(true);
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(true);
|
|
|
|
}, 0);
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-04 10:13:51 +00:00
|
|
|
wrapper.findAll('#hello')[0].element.dispatchEvent(new MouseEvent('mouseleave'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(false);
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(false);
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
// add `visible` props.
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.setProps({ visible: false });
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-04 10:13:51 +00:00
|
|
|
wrapper.findAll('#hello')[0].element.dispatchEvent(new MouseEvent('mouseenter'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange).toHaveBeenLastCalledWith(true);
|
|
|
|
lastCount = onVisibleChange.mock.calls.length;
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(false);
|
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
// always trigger onVisibleChange
|
2020-08-04 10:13:51 +00:00
|
|
|
wrapper.findAll('#hello')[0].element.dispatchEvent(new MouseEvent('mouseleave'));
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-10 13:53:52 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onVisibleChange.mock.calls.length).toBe(lastCount); // no change with lastCount
|
|
|
|
expect(wrapper.vm.$refs.tooltip.$refs.tooltip.visible).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|