summaryrefslogtreecommitdiff
path: root/spec/frontend/admin/users/components/actions/actions_spec.js
blob: fa485e739994172e4924cdef7fa5f1d68f2fea0a (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
import { GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import { kebabCase } from 'lodash';
import Actions from '~/admin/users/components/actions';
import SharedDeleteAction from '~/admin/users/components/actions/shared/shared_delete_action.vue';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
import { OBSTACLE_TYPES } from '~/vue_shared/components/user_deletion_obstacles/constants';
import { CONFIRMATION_ACTIONS, DELETE_ACTIONS } from '../../constants';
import { paths } from '../../mock_data';

describe('Action components', () => {
  let wrapper;

  const findDropdownItem = () => wrapper.find(GlDropdownItem);

  const initComponent = ({ component, props, stubs = {} } = {}) => {
    wrapper = shallowMount(component, {
      propsData: {
        ...props,
      },
      stubs,
    });
  };

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('CONFIRMATION_ACTIONS', () => {
    it.each(CONFIRMATION_ACTIONS)('renders a dropdown item for "%s"', async (action) => {
      initComponent({
        component: Actions[capitalizeFirstCharacter(action)],
        props: {
          username: 'John Doe',
          path: '/test',
        },
      });

      await nextTick();
      expect(findDropdownItem().exists()).toBe(true);
    });
  });

  describe('DELETE_ACTION_COMPONENTS', () => {
    const userDeletionObstacles = [
      { name: 'schedule1', type: OBSTACLE_TYPES.oncallSchedules },
      { name: 'policy1', type: OBSTACLE_TYPES.escalationPolicies },
    ];

    it.each(DELETE_ACTIONS.map((action) => [action, paths[action]]))(
      'renders a dropdown item for "%s"',
      async (action, expectedPath) => {
        initComponent({
          component: Actions[capitalizeFirstCharacter(action)],
          props: {
            username: 'John Doe',
            paths,
            userDeletionObstacles,
          },
          stubs: { SharedDeleteAction },
        });

        await nextTick();
        const sharedAction = wrapper.find(SharedDeleteAction);

        expect(sharedAction.attributes('data-block-user-url')).toBe(paths.block);
        expect(sharedAction.attributes('data-delete-user-url')).toBe(expectedPath);
        expect(sharedAction.attributes('data-gl-modal-action')).toBe(kebabCase(action));
        expect(sharedAction.attributes('data-username')).toBe('John Doe');
        expect(sharedAction.attributes('data-user-deletion-obstacles')).toBe(
          JSON.stringify(userDeletionObstacles),
        );

        expect(findDropdownItem().exists()).toBe(true);
      },
    );
  });
});