2022-05-04 05:09:02 -04:00
|
|
|
import { within } from '@testing-library/dom';
|
2022-05-13 08:08:49 -04:00
|
|
|
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2018-12-07 22:12:23 -05:00
|
|
|
import UsersCache from '~/lib/utils/users_cache';
|
2021-02-14 13:09:20 -05:00
|
|
|
import initUserPopovers from '~/user_popovers';
|
2022-05-04 05:09:02 -04:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
|
|
|
|
jest.mock('~/api/user_api', () => ({
|
|
|
|
followUser: jest.fn().mockResolvedValue({}),
|
|
|
|
unfollowUser: jest.fn().mockResolvedValue({}),
|
|
|
|
}));
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
describe('User Popovers', () => {
|
2022-08-01 05:09:08 -04:00
|
|
|
let origGon;
|
|
|
|
|
2020-02-17 16:09:20 -05:00
|
|
|
const fixtureTemplate = 'merge_requests/merge_request_with_mentions.html';
|
2018-12-12 16:49:10 -05:00
|
|
|
|
2022-06-22 05:09:36 -04:00
|
|
|
const selector = '.js-user-link[data-user], .js-user-link[data-user-id]';
|
|
|
|
const findFixtureLinks = () => Array.from(document.querySelectorAll(selector));
|
2021-02-18 07:09:34 -05:00
|
|
|
const createUserLink = () => {
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
|
|
|
link.classList.add('js-user-link');
|
2022-06-17 11:08:29 -04:00
|
|
|
link.dataset.user = '1';
|
2021-02-18 07:09:34 -05:00
|
|
|
|
|
|
|
return link;
|
|
|
|
};
|
2022-05-25 14:08:15 -04:00
|
|
|
const findPopovers = () => {
|
|
|
|
return Array.from(document.querySelectorAll('[data-testid="user-popover"]'));
|
|
|
|
};
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-05-04 05:09:02 -04:00
|
|
|
const dummyUser = { name: 'root', username: 'root', is_followed: false };
|
2018-12-07 22:12:23 -05:00
|
|
|
const dummyUserStatus = { message: 'active' };
|
|
|
|
|
|
|
|
const triggerEvent = (eventName, el) => {
|
2020-01-30 16:08:47 -05:00
|
|
|
const event = new MouseEvent(eventName, {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
view: window,
|
|
|
|
});
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
el.dispatchEvent(event);
|
|
|
|
};
|
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
const setupTestSubject = () => {
|
2022-05-13 08:08:49 -04:00
|
|
|
loadHTMLFixture(fixtureTemplate);
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
const usersCacheSpy = () => Promise.resolve(dummyUser);
|
2020-12-23 16:10:24 -05:00
|
|
|
jest.spyOn(UsersCache, 'retrieveById').mockImplementation((userId) => usersCacheSpy(userId));
|
2018-12-07 22:12:23 -05:00
|
|
|
|
|
|
|
const userStatusCacheSpy = () => Promise.resolve(dummyUserStatus);
|
2020-06-08 05:08:23 -04:00
|
|
|
jest
|
|
|
|
.spyOn(UsersCache, 'retrieveStatusById')
|
2020-12-23 16:10:24 -05:00
|
|
|
.mockImplementation((userId) => userStatusCacheSpy(userId));
|
2022-05-04 05:09:02 -04:00
|
|
|
jest.spyOn(UsersCache, 'updateById');
|
|
|
|
|
2022-05-25 14:08:15 -04:00
|
|
|
initUserPopovers((popoverInstance) => {
|
|
|
|
const mountingRoot = document.createElement('div');
|
|
|
|
document.body.appendChild(mountingRoot);
|
|
|
|
popoverInstance.$mount(mountingRoot);
|
|
|
|
});
|
2022-08-01 05:09:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
origGon = window.gon;
|
|
|
|
window.gon = {};
|
2018-12-07 22:12:23 -05:00
|
|
|
});
|
|
|
|
|
2022-05-13 08:08:49 -04:00
|
|
|
afterEach(() => {
|
2022-08-01 05:09:08 -04:00
|
|
|
window.gon = origGon;
|
2022-05-13 08:08:49 -04:00
|
|
|
});
|
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
describe('when signed out', () => {
|
2022-05-25 14:08:15 -04:00
|
|
|
beforeEach(() => {
|
2022-08-01 05:09:08 -04:00
|
|
|
setupTestSubject();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show a placeholder popover on hover', () => {
|
|
|
|
const linksWithUsers = findFixtureLinks();
|
2022-05-25 14:08:15 -04:00
|
|
|
linksWithUsers.forEach((el) => {
|
|
|
|
triggerEvent('mouseover', el);
|
|
|
|
});
|
2022-08-01 05:09:08 -04:00
|
|
|
|
|
|
|
expect(findPopovers().length).toBe(0);
|
2022-05-25 14:08:15 -04:00
|
|
|
});
|
2022-08-01 05:09:08 -04:00
|
|
|
});
|
2020-02-17 16:09:20 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
describe('when signed in', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
window.gon.current_user_id = 7;
|
|
|
|
|
|
|
|
setupTestSubject();
|
2022-05-25 14:08:15 -04:00
|
|
|
});
|
2021-02-18 07:09:34 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
2021-02-18 07:09:34 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
describe('shows a placeholder popover on hover', () => {
|
|
|
|
let linksWithUsers;
|
|
|
|
beforeEach(() => {
|
|
|
|
linksWithUsers = findFixtureLinks();
|
|
|
|
linksWithUsers.forEach((el) => {
|
|
|
|
triggerEvent('mouseover', el);
|
|
|
|
});
|
|
|
|
});
|
2022-03-29 11:09:53 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('for initial links', () => {
|
|
|
|
expect(findPopovers().length).toBe(linksWithUsers.length);
|
2022-05-25 14:08:15 -04:00
|
|
|
});
|
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('for elements added after initial load', async () => {
|
|
|
|
const addedLinks = [createUserLink(), createUserLink()];
|
|
|
|
addedLinks.forEach((link) => {
|
|
|
|
document.body.appendChild(link);
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
addedLinks.forEach((link) => {
|
|
|
|
triggerEvent('mouseover', link);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(findPopovers().length).toBe(linksWithUsers.length + addedLinks.length);
|
|
|
|
});
|
2022-05-25 14:08:15 -04:00
|
|
|
});
|
2021-02-18 07:09:34 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('does not initialize the popovers for group references', async () => {
|
|
|
|
const [groupLink] = Array.from(document.querySelectorAll('.js-user-link[data-group]'));
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('mouseover', groupLink);
|
|
|
|
jest.runOnlyPendingTimers();
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(findPopovers().length).toBe(0);
|
|
|
|
});
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('does not initialize the popovers for @all references', async () => {
|
|
|
|
const [projectLink] = Array.from(document.querySelectorAll('.js-user-link[data-project]'));
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('mouseover', projectLink);
|
|
|
|
jest.runOnlyPendingTimers();
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(findPopovers().length).toBe(0);
|
|
|
|
});
|
2022-06-22 05:09:36 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('does not initialize the user popovers twice for the same element', async () => {
|
|
|
|
const [firstUserLink] = findFixtureLinks();
|
|
|
|
triggerEvent('mouseover', firstUserLink);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
triggerEvent('mouseleave', firstUserLink);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
triggerEvent('mouseover', firstUserLink);
|
|
|
|
jest.runOnlyPendingTimers();
|
2020-02-06 07:10:29 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(findPopovers().length).toBe(1);
|
|
|
|
});
|
2020-02-06 07:10:29 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
describe('when user link emits mouseenter event with empty user cache', () => {
|
|
|
|
let userLink;
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
UsersCache.retrieveById.mockReset();
|
2020-06-08 05:08:23 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
[userLink] = findFixtureLinks();
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('mouseover', userLink);
|
|
|
|
});
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('populates popover with preloaded user data', () => {
|
|
|
|
const { name, userId, username } = userLink.dataset;
|
2020-01-30 16:08:47 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(userLink.user).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
name,
|
|
|
|
userId,
|
|
|
|
username,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2020-01-30 16:08:47 -05:00
|
|
|
});
|
2022-05-25 14:08:15 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
describe('when user link emits mouseenter event', () => {
|
|
|
|
let userLink;
|
2022-05-25 14:08:15 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
[userLink] = findFixtureLinks();
|
2022-05-25 14:08:15 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('mouseover', userLink);
|
|
|
|
});
|
2022-05-25 14:08:15 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('removes title attribute from user links', () => {
|
2022-09-21 17:13:33 -04:00
|
|
|
expect(userLink.getAttribute('title')).toBe('');
|
|
|
|
expect(userLink.dataset.originalTitle).toBe('');
|
2022-08-01 05:09:08 -04:00
|
|
|
});
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('fetches user info and status from the user cache', () => {
|
|
|
|
const { userId } = userLink.dataset;
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(UsersCache.retrieveById).toHaveBeenCalledWith(userId);
|
|
|
|
expect(UsersCache.retrieveStatusById).toHaveBeenCalledWith(userId);
|
|
|
|
});
|
2018-12-07 22:12:23 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('removes aria-describedby attribute from the user link on mouseleave', () => {
|
|
|
|
userLink.setAttribute('aria-describedby', 'popover');
|
|
|
|
triggerEvent('mouseleave', userLink);
|
2020-01-30 16:08:47 -05:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(userLink.getAttribute('aria-describedby')).toBe(null);
|
|
|
|
});
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
it('updates toggle follow button and `UsersCache` when toggle follow button is clicked', async () => {
|
|
|
|
const [firstPopover] = findPopovers();
|
|
|
|
const withinFirstPopover = within(firstPopover);
|
|
|
|
const findFollowButton = () => withinFirstPopover.queryByRole('button', { name: 'Follow' });
|
|
|
|
const findUnfollowButton = () =>
|
|
|
|
withinFirstPopover.queryByRole('button', { name: 'Unfollow' });
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
jest.runOnlyPendingTimers();
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
const { userId } = document.querySelector(selector).dataset;
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('click', findFollowButton());
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
await waitForPromises();
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(findUnfollowButton()).not.toBe(null);
|
|
|
|
expect(UsersCache.updateById).toHaveBeenCalledWith(userId, { is_followed: true });
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
triggerEvent('click', findUnfollowButton());
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
await waitForPromises();
|
2022-05-04 05:09:02 -04:00
|
|
|
|
2022-08-01 05:09:08 -04:00
|
|
|
expect(findFollowButton()).not.toBe(null);
|
|
|
|
expect(UsersCache.updateById).toHaveBeenCalledWith(userId, { is_followed: false });
|
|
|
|
});
|
2022-05-25 14:08:15 -04:00
|
|
|
});
|
2022-05-04 05:09:02 -04:00
|
|
|
});
|
2018-12-07 22:12:23 -05:00
|
|
|
});
|