Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_actions_spec.js « components « users « admin « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffc05e744c852c41fe3ddcf1e38e0ac4fcc512e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { GlDropdownDivider } from '@gitlab/ui';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
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';

import { CONFIRMATION_ACTIONS, DELETE_ACTIONS, LDAP, EDIT } from '../constants';
import { users, paths } from '../mock_data';

describe('AdminUserActions component', () => {
  let wrapper;
  const user = users[0];
  const userPaths = generateUserPaths(paths, user.username);

  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);

  const initComponent = ({ actions = [], showButtonLabels } = {}) => {
    wrapper = shallowMountExtended(AdminUserActions, {
      propsData: {
        user: {
          ...user,
          actions,
        },
        paths,
        showButtonLabels,
      },
      directives: {
        GlTooltip: createMockDirective(),
      },
    });
  };

  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', () => {
      const actions = [EDIT, ...CONFIRMATION_ACTIONS];

      beforeEach(() => {
        initComponent({ actions });
      });

      it('renders the actions dropdown', () => {
        expect(findActionsDropdown().exists()).toBe(true);
      });

      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.findComponent(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.findComponent(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);
      });
    });
  });

  describe('when `showButtonLabels` prop is `false`', () => {
    beforeEach(() => {
      initComponent({ actions: [EDIT] });
    });

    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(() => {
      initComponent({ actions: [EDIT], showButtonLabels: true });
    });

    it('renders "Edit" button label', () => {
      const tooltip = getBinding(findEditButton().element, 'gl-tooltip');

      expect(findEditButton().text()).toBe(I18N_USER_ACTIONS.edit);
      expect(tooltip).not.toBeDefined();
    });
  });
});