2022-04-04 11:22:00 +05:30
|
|
|
import { nextTick } from 'vue';
|
2021-01-29 00:20:46 +05:30
|
|
|
import {
|
|
|
|
add,
|
|
|
|
initTooltips,
|
|
|
|
dispose,
|
|
|
|
destroy,
|
|
|
|
hide,
|
|
|
|
show,
|
|
|
|
enable,
|
|
|
|
disable,
|
|
|
|
fixTitle,
|
|
|
|
} from '~/tooltips';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
describe('tooltips/index.js', () => {
|
|
|
|
let tooltipsApp;
|
|
|
|
|
|
|
|
const createTooltipTarget = () => {
|
|
|
|
const target = document.createElement('button');
|
|
|
|
const attributes = {
|
|
|
|
title: 'default title',
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
Object.keys(attributes).forEach((name) => {
|
2020-11-24 15:15:51 +05:30
|
|
|
target.setAttribute(name, attributes[name]);
|
|
|
|
});
|
|
|
|
|
|
|
|
target.classList.add('has-tooltip');
|
|
|
|
|
|
|
|
document.body.appendChild(target);
|
|
|
|
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildTooltipsApp = () => {
|
|
|
|
tooltipsApp = initTooltips({ selector: '.has-tooltip' });
|
|
|
|
};
|
|
|
|
|
|
|
|
const triggerEvent = (target, eventName = 'mouseenter') => {
|
|
|
|
const event = new Event(eventName);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-03-08 18:12:59 +05:30
|
|
|
window.gon.features = { glTooltips: true };
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2021-03-08 18:12:59 +05:30
|
|
|
document.body.childNodes.forEach((node) => node.remove());
|
2020-11-24 15:15:51 +05:30
|
|
|
destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initTooltip', () => {
|
|
|
|
it('attaches a GlTooltip for the elements specified in the selector', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
|
|
|
|
triggerEvent(target);
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-tooltip').innerHTML).toContain('default title');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports triggering a tooltip in custom events', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
triggerEvent(target, 'click');
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-tooltip').innerHTML).toContain('default title');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
describe('add', () => {
|
|
|
|
it('adds a GlTooltip for the specified elements', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
add([target], { title: 'custom title' });
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-tooltip').innerHTML).toContain('custom title');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe('dispose', () => {
|
|
|
|
it('removes tooltips that target the elements specified', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
triggerEvent(target);
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
|
|
|
|
dispose([target]);
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
methodName | method | event
|
|
|
|
${'enable'} | ${enable} | ${'enable'}
|
|
|
|
${'disable'} | ${disable} | ${'disable'}
|
|
|
|
${'hide'} | ${hide} | ${'close'}
|
|
|
|
${'show'} | ${show} | ${'open'}
|
|
|
|
`(
|
|
|
|
'$methodName calls triggerEvent in tooltip app with $event event',
|
|
|
|
async ({ method, event }) => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
jest.spyOn(tooltipsApp, 'triggerEvent');
|
|
|
|
|
|
|
|
method([target]);
|
|
|
|
|
|
|
|
expect(tooltipsApp.triggerEvent).toHaveBeenCalledWith(target, event);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('fixTitle calls fixTitle in tooltip app with the target specified', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
jest.spyOn(tooltipsApp, 'fixTitle');
|
|
|
|
|
|
|
|
fixTitle([target]);
|
|
|
|
|
|
|
|
expect(tooltipsApp.fixTitle).toHaveBeenCalledWith(target);
|
|
|
|
});
|
|
|
|
});
|