debian-mirror-gitlab/spec/frontend/admin/users/components/user_actions_spec.js

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

187 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { GlDropdownDivider } from '@gitlab/ui';
2021-09-30 23:02:18 +05:30
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
2021-06-08 01:23:25 +05:30
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
2021-03-11 19:13:27 +05:30
import Actions from '~/admin/users/components/actions';
import AdminUserActions from '~/admin/users/components/user_actions.vue';
import { I18N_USER_ACTIONS } from '~/admin/users/constants';
import { generateUserPaths } from '~/admin/users/utils';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
2021-09-30 23:02:18 +05:30
import { CONFIRMATION_ACTIONS, DELETE_ACTIONS, LDAP, EDIT } from '../constants';
2021-03-11 19:13:27 +05:30
import { users, paths } from '../mock_data';
describe('AdminUserActions component', () => {
let wrapper;
const user = users[0];
const userPaths = generateUserPaths(paths, user.username);
2021-06-08 01:23:25 +05:30
const findUserActions = (id) => wrapper.findByTestId(`user-actions-${id}`);
const findEditButton = (id = user.id) => findUserActions(id).find('[data-testid="edit"]');
const findActionsDropdown = (id = user.id) =>
findUserActions(id).find('[data-testid="dropdown-toggle"]');
const findDropdownDivider = () => wrapper.findComponent(GlDropdownDivider);
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
const initComponent = ({ actions = [], showButtonLabels } = {}) => {
2021-06-08 01:23:25 +05:30
wrapper = shallowMountExtended(AdminUserActions, {
2021-03-11 19:13:27 +05:30
propsData: {
user: {
...user,
actions,
},
paths,
2021-09-30 23:02:18 +05:30
showButtonLabels,
},
directives: {
GlTooltip: createMockDirective(),
2021-03-11 19:13:27 +05:30
},
});
};
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
describe('edit button', () => {
describe('when the user has an edit action attached', () => {
beforeEach(() => {
initComponent({ actions: [EDIT] });
});
it('renders the edit button linking to the user edit path', () => {
expect(findEditButton().exists()).toBe(true);
expect(findEditButton().attributes('href')).toBe(userPaths.edit);
});
});
describe('when there is no edit action attached to the user', () => {
beforeEach(() => {
initComponent({ actions: [] });
});
it('does not render the edit button linking to the user edit path', () => {
expect(findEditButton().exists()).toBe(false);
});
});
});
describe('actions dropdown', () => {
describe('when there are actions', () => {
2021-09-30 23:02:18 +05:30
const actions = [EDIT, ...CONFIRMATION_ACTIONS];
2021-03-11 19:13:27 +05:30
beforeEach(() => {
initComponent({ actions });
});
it('renders the actions dropdown', () => {
expect(findActionsDropdown().exists()).toBe(true);
});
2022-05-07 20:08:51 +05:30
it('renders the tooltip', () => {
const tooltip = getBinding(findActionsDropdown().element, 'gl-tooltip');
expect(tooltip.value).toBe(I18N_USER_ACTIONS.userAdministration);
});
2021-03-11 19:13:27 +05:30
describe('when there are actions that require confirmation', () => {
beforeEach(() => {
initComponent({ actions: CONFIRMATION_ACTIONS });
});
it.each(CONFIRMATION_ACTIONS)('renders an action component item for "%s"', (action) => {
const component = wrapper.find(Actions[capitalizeFirstCharacter(action)]);
expect(component.props('username')).toBe(user.name);
expect(component.props('path')).toBe(userPaths[action]);
expect(component.text()).toBe(I18N_USER_ACTIONS[action]);
});
});
describe('when there is a LDAP action', () => {
beforeEach(() => {
initComponent({ actions: [LDAP] });
});
it('renders the LDAP dropdown item without a link', () => {
const dropdownAction = wrapper.find(`[data-testid="${LDAP}"]`);
expect(dropdownAction.exists()).toBe(true);
expect(dropdownAction.attributes('href')).toBe(undefined);
expect(dropdownAction.text()).toBe(I18N_USER_ACTIONS[LDAP]);
});
});
describe('when there is a delete action', () => {
beforeEach(() => {
initComponent({ actions: [LDAP, ...DELETE_ACTIONS] });
});
it('renders a dropdown divider', () => {
expect(findDropdownDivider().exists()).toBe(true);
});
it('only renders delete dropdown items for actions containing the word "delete"', () => {
const { length } = wrapper.findAll(`[data-testid*="delete-"]`);
expect(length).toBe(DELETE_ACTIONS.length);
});
it.each(DELETE_ACTIONS)('renders a delete action component item for "%s"', (action) => {
const component = wrapper.find(Actions[capitalizeFirstCharacter(action)]);
expect(component.props('username')).toBe(user.name);
expect(component.props('paths')).toEqual(userPaths);
expect(component.text()).toBe(I18N_USER_ACTIONS[action]);
});
});
describe('when there are no delete actions', () => {
it('does not render a dropdown divider', () => {
expect(findDropdownDivider().exists()).toBe(false);
});
it('does not render a delete dropdown item', () => {
const anyDeleteAction = wrapper.find(`[data-testid*="delete-"]`);
expect(anyDeleteAction.exists()).toBe(false);
});
});
});
describe('when there are no actions', () => {
beforeEach(() => {
initComponent({ actions: [] });
});
it('does not render the actions dropdown', () => {
expect(findActionsDropdown().exists()).toBe(false);
});
});
});
2021-09-30 23:02:18 +05:30
describe('when `showButtonLabels` prop is `false`', () => {
beforeEach(() => {
2022-05-07 20:08:51 +05:30
initComponent({ actions: [EDIT] });
2021-09-30 23:02:18 +05:30
});
it('does not render "Edit" button label', () => {
const tooltip = getBinding(findEditButton().element, 'gl-tooltip');
expect(findEditButton().text()).toBe('');
expect(findEditButton().attributes('aria-label')).toBe(I18N_USER_ACTIONS.edit);
expect(tooltip).toBeDefined();
expect(tooltip.value).toBe(I18N_USER_ACTIONS.edit);
});
});
describe('when `showButtonLabels` prop is `true`', () => {
beforeEach(() => {
2022-05-07 20:08:51 +05:30
initComponent({ actions: [EDIT], showButtonLabels: true });
2021-09-30 23:02:18 +05:30
});
it('renders "Edit" button label', () => {
const tooltip = getBinding(findEditButton().element, 'gl-tooltip');
expect(findEditButton().text()).toBe(I18N_USER_ACTIONS.edit);
expect(tooltip).not.toBeDefined();
});
});
2021-03-11 19:13:27 +05:30
});