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

groups_list_item_spec.js « add_namespace_modal « components « subscriptions « jira_connect « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5df54abfc05b2288464463007b095dd8f07f1085 (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
import { GlButton } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';

import * as JiraConnectApi from '~/jira_connect/subscriptions/api';
import GroupItemName from '~/jira_connect/subscriptions/components/group_item_name.vue';
import GroupsListItem from '~/jira_connect/subscriptions/components/add_namespace_modal/groups_list_item.vue';
import { persistAlert, reloadPage } from '~/jira_connect/subscriptions/utils';
import {
  I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE,
  I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE,
  INTEGRATIONS_DOC_LINK,
} from '~/jira_connect/subscriptions/constants';
import createStore from '~/jira_connect/subscriptions/store';
import { mockGroup1 } from '../../mock_data';

jest.mock('~/jira_connect/subscriptions/utils');

describe('GroupsListItem', () => {
  let wrapper;
  let store;

  const mockAddSubscriptionsPath = '/addSubscriptionsPath';

  const createComponent = ({ mountFn = shallowMount, provide } = {}) => {
    store = createStore();

    jest.spyOn(store, 'dispatch').mockImplementation();

    wrapper = mountFn(GroupsListItem, {
      store,
      propsData: {
        group: mockGroup1,
      },
      provide: {
        addSubscriptionsPath: mockAddSubscriptionsPath,
        ...provide,
      },
    });
  };

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

  const findGroupItemName = () => wrapper.findComponent(GroupItemName);
  const findLinkButton = () => wrapper.findComponent(GlButton);
  const clickLinkButton = () => findLinkButton().trigger('click');

  describe('template', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders GroupItemName', () => {
      expect(findGroupItemName().exists()).toBe(true);
      expect(findGroupItemName().props('group')).toBe(mockGroup1);
    });

    it('renders Link button', () => {
      expect(findLinkButton().exists()).toBe(true);
      expect(findLinkButton().text()).toBe('Link');
    });
  });

  describe('on Link button click', () => {
    describe('when jiraConnectOauth feature flag is disabled', () => {
      let addSubscriptionSpy;

      beforeEach(() => {
        createComponent({ mountFn: mount });

        addSubscriptionSpy = jest.spyOn(JiraConnectApi, 'addSubscription').mockResolvedValue();
      });

      it('sets button to loading and sends request', async () => {
        expect(findLinkButton().props('loading')).toBe(false);

        clickLinkButton();
        await nextTick();

        expect(findLinkButton().props('loading')).toBe(true);
        await waitForPromises();

        expect(addSubscriptionSpy).toHaveBeenCalledWith(
          mockAddSubscriptionsPath,
          mockGroup1.full_path,
        );
        expect(persistAlert).toHaveBeenCalledWith({
          linkUrl: INTEGRATIONS_DOC_LINK,
          message: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE,
          title: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE,
          variant: 'success',
        });
      });

      describe('when request is successful', () => {
        it('reloads the page', async () => {
          clickLinkButton();

          await waitForPromises();

          expect(reloadPage).toHaveBeenCalled();
        });
      });

      describe('when request has errors', () => {
        const mockErrorMessage = 'error message';
        const mockError = { response: { data: { error: mockErrorMessage } } };

        beforeEach(() => {
          addSubscriptionSpy = jest
            .spyOn(JiraConnectApi, 'addSubscription')
            .mockRejectedValue(mockError);
        });

        it('emits `error` event', async () => {
          clickLinkButton();

          await waitForPromises();

          expect(reloadPage).not.toHaveBeenCalled();
          expect(wrapper.emitted('error')[0][0]).toBe(mockErrorMessage);
        });
      });
    });

    describe('when jiraConnectOauth feature flag is enabled', () => {
      const mockSubscriptionsPath = '/subscriptions';

      beforeEach(() => {
        createComponent({
          mountFn: mount,
          provide: {
            subscriptionsPath: mockSubscriptionsPath,
            glFeatures: { jiraConnectOauth: true },
          },
        });
      });

      it('dispatches `addSubscription` action', async () => {
        clickLinkButton();
        await nextTick();

        expect(store.dispatch).toHaveBeenCalledWith('addSubscription', {
          namespacePath: mockGroup1.full_path,
          subscriptionsPath: mockSubscriptionsPath,
        });
      });
    });
  });
});