import { asyncExpect } from '../../../tests/utils';
import { mount } from '@vue/test-utils';
import Tooltip from '..';
import mountTest from '../../../tests/shared/mountTest';
describe('Tooltip', () => {
mountTest(Tooltip);
it('check `onOpenChange` arguments', async () => {
const onOpenChange = jest.fn();
const wrapper = mount(
{
props: ['title', 'open'],
render() {
const props = {
title: this.title || '',
mouseEnterDelay: 0,
mouseLeaveDelay: 0,
onOpenChange,
};
if (this.open !== undefined) {
props.open = this.open;
}
return (
<Tooltip ref="tooltip" {...props}>
<div id="hello">Hello world!</div>
</Tooltip>
);
},
{ sync: false, attachTo: 'body' },
let div = null;
let lastCount = null;
await asyncExpect(() => {
// `title` is empty.
div = document.getElementById('hello');
div.dispatchEvent(new MouseEvent('mouseenter'));
});
expect(onOpenChange).not.toHaveBeenCalled();
expect(wrapper.vm.$refs.tooltip.open).toBe(false);
div.dispatchEvent(new MouseEvent('mouseleave'));
// update `title` value.
wrapper.setProps({ title: 'Have a nice day!' });
document.getElementById('hello').dispatchEvent(new MouseEvent('mouseenter'));
expect(onOpenChange).toHaveBeenLastCalledWith(true);
expect(wrapper.vm.$refs.tooltip.open).toBe(true);
}, 0);
document.getElementById('hello').dispatchEvent(new MouseEvent('mouseleave'));
expect(onOpenChange).toHaveBeenLastCalledWith(false);
// add `open` props.
wrapper.setProps({ open: false });
// always trigger onOpenChange
lastCount = onOpenChange.mock.calls.length;
expect(onOpenChange.mock.calls.length).toBe(lastCount); // no change with lastCount