debian-mirror-gitlab/spec/frontend/popovers/index_spec.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2021-01-29 00:20:46 +05:30
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-04-04 11:22:00 +05:30
initPopovers('[data-toggle="popover"]');
2021-01-29 00:20:46 +05:30
};
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-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
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-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
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-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
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-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
expect(document.querySelectorAll('.gl-popover')).toHaveLength(2);
dispose([fakeTarget]);
2022-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
expect(document.querySelectorAll('.gl-popover')).toHaveLength(2);
dispose([target]);
2022-04-04 11:22:00 +05:30
await nextTick();
2021-01-29 00:20:46 +05:30
expect(document.querySelectorAll('.gl-popover')).toHaveLength(1);
});
});
});