2018-12-08 03:12:23 +00:00
|
|
|
import UsersCache from '~/lib/utils/users_cache';
|
2021-02-14 18:09:20 +00:00
|
|
|
import initUserPopovers from '~/user_popovers';
|
2018-12-08 03:12:23 +00:00
|
|
|
|
|
|
|
describe('User Popovers', () => {
|
2020-02-17 21:09:20 +00:00
|
|
|
const fixtureTemplate = 'merge_requests/merge_request_with_mentions.html';
|
2018-12-12 21:49:10 +00:00
|
|
|
|
2020-02-17 21:09:20 +00:00
|
|
|
const selector = '.js-user-link, .gfm-project_member';
|
2021-02-18 12:09:34 +00:00
|
|
|
const findFixtureLinks = () => {
|
|
|
|
return Array.from(document.querySelectorAll(selector)).filter(
|
|
|
|
({ dataset }) => dataset.user || dataset.userId,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
const createUserLink = () => {
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
|
|
|
link.classList.add('js-user-link');
|
|
|
|
link.setAttribute('data-user', '1');
|
|
|
|
|
|
|
|
return link;
|
|
|
|
};
|
2018-12-08 03:12:23 +00:00
|
|
|
|
|
|
|
const dummyUser = { name: 'root' };
|
|
|
|
const dummyUserStatus = { message: 'active' };
|
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
let popovers;
|
|
|
|
|
2018-12-08 03:12:23 +00:00
|
|
|
const triggerEvent = (eventName, el) => {
|
2020-01-30 21:08:47 +00:00
|
|
|
const event = new MouseEvent(eventName, {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
view: window,
|
|
|
|
});
|
2018-12-08 03:12:23 +00:00
|
|
|
|
|
|
|
el.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-12-12 21:49:10 +00:00
|
|
|
loadFixtures(fixtureTemplate);
|
2018-12-08 03:12:23 +00:00
|
|
|
|
|
|
|
const usersCacheSpy = () => Promise.resolve(dummyUser);
|
2020-12-23 21:10:24 +00:00
|
|
|
jest.spyOn(UsersCache, 'retrieveById').mockImplementation((userId) => usersCacheSpy(userId));
|
2018-12-08 03:12:23 +00:00
|
|
|
|
|
|
|
const userStatusCacheSpy = () => Promise.resolve(dummyUserStatus);
|
2020-06-08 09:08:23 +00:00
|
|
|
jest
|
|
|
|
.spyOn(UsersCache, 'retrieveStatusById')
|
2020-12-23 21:10:24 +00:00
|
|
|
.mockImplementation((userId) => userStatusCacheSpy(userId));
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
popovers = initUserPopovers(document.querySelectorAll(selector));
|
2018-12-08 03:12:23 +00:00
|
|
|
});
|
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
it('initializes a popover for each user link with a user id', () => {
|
|
|
|
const linksWithUsers = findFixtureLinks();
|
2020-02-17 21:09:20 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
expect(linksWithUsers.length).toBe(popovers.length);
|
|
|
|
});
|
2021-02-18 12:09:34 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
it('adds popovers to user links added to the DOM tree after the initial call', async () => {
|
|
|
|
document.body.appendChild(createUserLink());
|
|
|
|
document.body.appendChild(createUserLink());
|
2021-02-18 12:09:34 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
const linksWithUsers = findFixtureLinks();
|
2022-03-29 15:09:53 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
expect(linksWithUsers.length).toBe(popovers.length + 2);
|
2021-02-18 12:09:34 +00:00
|
|
|
});
|
|
|
|
|
2020-02-06 12:10:29 +00:00
|
|
|
it('does not initialize the user popovers twice for the same element', () => {
|
2022-04-13 21:09:57 +00:00
|
|
|
const newPopovers = initUserPopovers(document.querySelectorAll(selector));
|
|
|
|
const samePopovers = popovers.every((popover, index) => newPopovers[index] === popover);
|
2020-02-06 12:10:29 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
expect(samePopovers).toBe(true);
|
2020-02-06 12:10:29 +00:00
|
|
|
});
|
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
describe('when user link emits mouseenter event', () => {
|
|
|
|
let userLink;
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
beforeEach(() => {
|
2020-06-08 09:08:23 +00:00
|
|
|
UsersCache.retrieveById.mockReset();
|
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
userLink = document.querySelector(selector);
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
triggerEvent('mouseenter', userLink);
|
|
|
|
});
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
it('removes title attribute from user links', () => {
|
|
|
|
expect(userLink.getAttribute('title')).toBeFalsy();
|
|
|
|
expect(userLink.dataset.originalTitle).toBeFalsy();
|
|
|
|
});
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
it('populates popovers with preloaded user data', () => {
|
2020-01-30 21:08:47 +00:00
|
|
|
const { name, userId, username } = userLink.dataset;
|
2022-04-13 21:09:57 +00:00
|
|
|
const [firstPopover] = popovers;
|
2020-01-30 21:08:47 +00:00
|
|
|
|
2022-04-13 21:09:57 +00:00
|
|
|
expect(firstPopover.$props.user).toEqual(
|
2020-06-08 09:08:23 +00:00
|
|
|
expect.objectContaining({
|
2020-01-30 21:08:47 +00:00
|
|
|
name,
|
|
|
|
userId,
|
|
|
|
username,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
it('fetches user info and status from the user cache', () => {
|
|
|
|
const { userId } = userLink.dataset;
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
expect(UsersCache.retrieveById).toHaveBeenCalledWith(userId);
|
|
|
|
expect(UsersCache.retrieveStatusById).toHaveBeenCalledWith(userId);
|
|
|
|
});
|
|
|
|
});
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
it('removes aria-describedby attribute from the user link on mouseleave', () => {
|
|
|
|
const userLink = document.querySelector(selector);
|
2018-12-08 03:12:23 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
userLink.setAttribute('aria-describedby', 'popover');
|
|
|
|
triggerEvent('mouseleave', userLink);
|
|
|
|
|
|
|
|
expect(userLink.getAttribute('aria-describedby')).toBe(null);
|
2018-12-08 03:12:23 +00:00
|
|
|
});
|
|
|
|
});
|