summaryrefslogtreecommitdiff
path: root/spec/frontend/admin/users/components/actions/actions_spec.js
blob: 5e232f3431187e849e7333033b978d9748651f8e (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
import { GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { kebabCase } from 'lodash';
import { nextTick } from 'vue';
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 { CONFIRMATION_ACTIONS, DELETE_ACTIONS } from '../../constants';

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

      const div = wrapper.find('div');
      expect(div.attributes('data-path')).toBe('/test');
      expect(div.attributes('data-modal-attributes')).toContain('John Doe');
      expect(findDropdownItem().exists()).toBe(true);
    });
  });

  describe('LINK_ACTIONS', () => {
    it.each`
      action       | method
      ${'Approve'} | ${'put'}
      ${'Reject'}  | ${'delete'}
    `(
      'renders a dropdown item link with method "$method" for "$action"',
      async ({ action, method }) => {
        initComponent({
          component: Actions[action],
          props: {
            path: '/test',
          },
        });

        await nextTick();

        const item = wrapper.find(GlDropdownItem);
        expect(item.attributes('href')).toBe('/test');
        expect(item.attributes('data-method')).toContain(method);
      },
    );
  });

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

      await nextTick();

      const sharedAction = wrapper.find(SharedDeleteAction);

      expect(sharedAction.attributes('data-block-user-url')).toBe('/block');
      expect(sharedAction.attributes('data-delete-user-url')).toBe('/delete');
      expect(sharedAction.attributes('data-gl-modal-action')).toBe(kebabCase(action));
      expect(sharedAction.attributes('data-username')).toBe('John Doe');
      expect(findDropdownItem().exists()).toBe(true);
    });
  });
});