2022-01-25 10:12:32 -05:00
|
|
|
import { nextTick } from 'vue';
|
2020-10-27 11:08:39 -04:00
|
|
|
import { initPopovers, dispose, destroy } from '~/popovers';
|
|
|
|
|
|
|
|
describe('popovers/index.js', () => {
|
|
|
|
const createPopoverTarget = (trigger = 'hover') => {
|
|
|
|
const target = document.createElement('button');
|
|
|
|
const dataset = {
|
|
|
|
title: 'default title',
|
|
|
|
content: 'some content',
|
|
|
|
toggle: 'popover',
|
|
|
|
trigger,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.entries(dataset).forEach(([key, value]) => {
|
|
|
|
target.dataset[key] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(target);
|
|
|
|
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildPopoversApp = () => {
|
2022-01-25 10:12:32 -05:00
|
|
|
initPopovers('[data-toggle="popover"]');
|
2020-10-27 11:08:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const triggerEvent = (target, eventName = 'mouseenter') => {
|
|
|
|
const event = new Event(eventName);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initPopover', () => {
|
|
|
|
it('attaches a GlPopover for the elements specified in the selector', async () => {
|
|
|
|
const target = createPopoverTarget();
|
|
|
|
|
|
|
|
buildPopoversApp();
|
|
|
|
|
|
|
|
triggerEvent(target);
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
const html = document.querySelector('.gl-popover').innerHTML;
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-popover')).not.toBe(null);
|
|
|
|
expect(html).toContain('default title');
|
|
|
|
expect(html).toContain('some content');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports triggering a popover via custom events', async () => {
|
|
|
|
const trigger = 'click';
|
|
|
|
const target = createPopoverTarget(trigger);
|
|
|
|
|
|
|
|
buildPopoversApp();
|
|
|
|
triggerEvent(target, trigger);
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
expect(document.querySelector('.gl-popover')).not.toBe(null);
|
|
|
|
expect(document.querySelector('.gl-popover').innerHTML).toContain('default title');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inits popovers on targets added after content load', async () => {
|
|
|
|
buildPopoversApp();
|
|
|
|
|
|
|
|
expect(document.querySelector('.gl-popover')).toBe(null);
|
|
|
|
|
|
|
|
const trigger = 'click';
|
|
|
|
const target = createPopoverTarget(trigger);
|
|
|
|
triggerEvent(target, trigger);
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
expect(document.querySelector('.gl-popover')).not.toBe(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dispose', () => {
|
|
|
|
it('removes popovers that target the elements specified', async () => {
|
|
|
|
const fakeTarget = createPopoverTarget();
|
|
|
|
const target = createPopoverTarget();
|
|
|
|
buildPopoversApp();
|
|
|
|
triggerEvent(target);
|
|
|
|
triggerEvent(createPopoverTarget());
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
expect(document.querySelectorAll('.gl-popover')).toHaveLength(2);
|
|
|
|
|
|
|
|
dispose([fakeTarget]);
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
expect(document.querySelectorAll('.gl-popover')).toHaveLength(2);
|
|
|
|
|
|
|
|
dispose([target]);
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
2020-10-27 11:08:39 -04:00
|
|
|
|
|
|
|
expect(document.querySelectorAll('.gl-popover')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|