2018-12-07 22:12:23 -05:00
|
|
|
import initUserPopovers from '~/user_popovers';
|
|
|
|
import UsersCache from '~/lib/utils/users_cache';
|
|
|
|
|
|
|
|
describe('User Popovers', () => {
|
2019-03-26 12:03:28 -04:00
|
|
|
const fixtureTemplate = 'merge_requests/diff_comment.html';
|
2018-12-12 16:49:10 -05:00
|
|
|
preloadFixtures(fixtureTemplate);
|
|
|
|
|
2018-12-07 22:12:23 -05:00
|
|
|
const selector = '.js-user-link';
|
|
|
|
|
|
|
|
const dummyUser = { name: 'root' };
|
|
|
|
const dummyUserStatus = { message: 'active' };
|
|
|
|
|
|
|
|
const triggerEvent = (eventName, el) => {
|
|
|
|
const event = document.createEvent('MouseEvents');
|
|
|
|
event.initMouseEvent(eventName, true, true, window);
|
|
|
|
|
|
|
|
el.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-12-12 16:49:10 -05:00
|
|
|
loadFixtures(fixtureTemplate);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
const usersCacheSpy = () => Promise.resolve(dummyUser);
|
|
|
|
spyOn(UsersCache, 'retrieveById').and.callFake(userId => usersCacheSpy(userId));
|
|
|
|
|
|
|
|
const userStatusCacheSpy = () => Promise.resolve(dummyUserStatus);
|
|
|
|
spyOn(UsersCache, 'retrieveStatusById').and.callFake(userId => userStatusCacheSpy(userId));
|
|
|
|
|
|
|
|
initUserPopovers(document.querySelectorAll('.js-user-link'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should Show+Hide Popover on mouseenter and mouseleave', done => {
|
2019-01-04 04:55:31 -05:00
|
|
|
const targetLink = document.querySelector(selector);
|
|
|
|
const { userId } = targetLink.dataset;
|
|
|
|
triggerEvent('mouseenter', targetLink);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
const shownPopover = document.querySelector('.popover');
|
|
|
|
|
|
|
|
expect(shownPopover).not.toBeNull();
|
2019-11-29 04:06:31 -05:00
|
|
|
expect(targetLink.getAttribute('aria-describedby')).not.toBeNull();
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
expect(shownPopover.innerHTML).toContain(dummyUser.name);
|
2019-01-04 04:55:31 -05:00
|
|
|
expect(UsersCache.retrieveById).toHaveBeenCalledWith(userId.toString());
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2019-01-04 04:55:31 -05:00
|
|
|
triggerEvent('mouseleave', targetLink);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
// After Mouse leave it should be hidden now
|
|
|
|
expect(document.querySelector('.popover')).toBeNull();
|
2019-11-29 04:06:31 -05:00
|
|
|
expect(targetLink.getAttribute('aria-describedby')).toBeNull();
|
2018-12-07 22:12:23 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, 210); // We need to wait until the 200ms mouseover delay is over, only then the popover will be visible
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should Not show a popover on short mouse over', done => {
|
2019-01-04 04:55:31 -05:00
|
|
|
const targetLink = document.querySelector(selector);
|
|
|
|
const { userId } = targetLink.dataset;
|
|
|
|
triggerEvent('mouseenter', targetLink);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(document.querySelector('.popover')).toBeNull();
|
2019-01-04 04:55:31 -05:00
|
|
|
expect(UsersCache.retrieveById).not.toHaveBeenCalledWith(userId.toString());
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2019-01-04 04:55:31 -05:00
|
|
|
triggerEvent('mouseleave', targetLink);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|