debian-mirror-gitlab/spec/frontend/user_popovers_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

237 lines
7 KiB
JavaScript
Raw Normal View History

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