2020-10-21 21:09:00 +00:00
|
|
|
import {
|
|
|
|
add,
|
|
|
|
initTooltips,
|
|
|
|
dispose,
|
|
|
|
destroy,
|
|
|
|
hide,
|
|
|
|
show,
|
|
|
|
enable,
|
|
|
|
disable,
|
|
|
|
fixTitle,
|
|
|
|
} from '~/tooltips';
|
2020-08-26 15:10:29 +00:00
|
|
|
|
|
|
|
describe('tooltips/index.js', () => {
|
2020-08-31 03:10:13 +00:00
|
|
|
let tooltipsApp;
|
|
|
|
|
2020-08-26 15:10:29 +00:00
|
|
|
const createTooltipTarget = () => {
|
|
|
|
const target = document.createElement('button');
|
|
|
|
const attributes = {
|
|
|
|
title: 'default title',
|
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
Object.keys(attributes).forEach((name) => {
|
2020-08-26 15:10:29 +00:00
|
|
|
target.setAttribute(name, attributes[name]);
|
|
|
|
});
|
|
|
|
|
|
|
|
target.classList.add('has-tooltip');
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
document.body.appendChild(target);
|
|
|
|
|
2020-08-26 15:10:29 +00:00
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
const buildTooltipsApp = () => {
|
2020-09-17 00:09:34 +00:00
|
|
|
tooltipsApp = initTooltips({ selector: '.has-tooltip' });
|
2020-08-31 03:10:13 +00:00
|
|
|
};
|
|
|
|
|
2020-08-26 15:10:29 +00:00
|
|
|
const triggerEvent = (target, eventName = 'mouseenter') => {
|
|
|
|
const event = new Event(eventName);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
2020-09-17 00:09:34 +00:00
|
|
|
beforeEach(() => {
|
2020-12-23 15:09:54 +00:00
|
|
|
window.gon.features = { glTooltips: true };
|
2020-09-17 00:09:34 +00:00
|
|
|
});
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
afterEach(() => {
|
2020-12-23 21:10:24 +00:00
|
|
|
document.body.childNodes.forEach((node) => node.remove());
|
2020-08-31 03:10:13 +00:00
|
|
|
destroy();
|
|
|
|
});
|
|
|
|
|
2020-08-26 15:10:29 +00:00
|
|
|
describe('initTooltip', () => {
|
|
|
|
it('attaches a GlTooltip for the elements specified in the selector', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
buildTooltipsApp();
|
2020-08-26 15:10:29 +00:00
|
|
|
|
|
|
|
triggerEvent(target);
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
buildTooltipsApp();
|
2020-08-26 15:10:29 +00:00
|
|
|
triggerEvent(target, 'click');
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-tooltip').innerHTML).toContain('default title');
|
|
|
|
});
|
|
|
|
});
|
2020-08-31 03:10:13 +00:00
|
|
|
|
2020-10-21 21:09:00 +00:00
|
|
|
describe('add', () => {
|
|
|
|
it('adds a GlTooltip for the specified elements', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
add([target], { title: 'custom title' });
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-tooltip').innerHTML).toContain('custom title');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-31 03:10:13 +00:00
|
|
|
describe('dispose', () => {
|
|
|
|
it('removes tooltips that target the elements specified', async () => {
|
|
|
|
const target = createTooltipTarget();
|
|
|
|
|
|
|
|
buildTooltipsApp();
|
|
|
|
triggerEvent(target);
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).not.toBe(null);
|
|
|
|
|
|
|
|
dispose([target]);
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-tooltip')).toBe(null);
|
|
|
|
});
|
|
|
|
});
|
2020-09-11 09:08:44 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
await tooltipsApp.$nextTick();
|
|
|
|
|
|
|
|
jest.spyOn(tooltipsApp, 'fixTitle');
|
|
|
|
|
|
|
|
fixTitle([target]);
|
|
|
|
|
|
|
|
expect(tooltipsApp.fixTitle).toHaveBeenCalledWith(target);
|
|
|
|
});
|
2020-08-26 15:10:29 +00:00
|
|
|
});
|