summaryrefslogtreecommitdiff
path: root/spec/frontend/jira_connect/components/groups_list_item_spec.js
blob: bb247534acaca4f09c2aa11289547caebb272a53 (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
import { GlAvatar, GlButton } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';

import * as JiraConnectApi from '~/jira_connect/api';
import GroupsListItem from '~/jira_connect/components/groups_list_item.vue';
import { mockGroup1 } from '../mock_data';

describe('GroupsListItem', () => {
  let wrapper;
  const mockSubscriptionPath = 'subscriptionPath';

  const reloadSpy = jest.fn();

  global.AP = {
    navigator: {
      reload: reloadSpy,
    },
  };

  const createComponent = ({ mountFn = shallowMount } = {}) => {
    wrapper = extendedWrapper(
      mountFn(GroupsListItem, {
        propsData: {
          group: mockGroup1,
        },
        provide: {
          subscriptionsPath: mockSubscriptionPath,
        },
      }),
    );
  };

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

  const findGlAvatar = () => wrapper.find(GlAvatar);
  const findGroupName = () => wrapper.findByTestId('group-list-item-name');
  const findGroupDescription = () => wrapper.findByTestId('group-list-item-description');
  const findLinkButton = () => wrapper.find(GlButton);
  const clickLinkButton = () => findLinkButton().trigger('click');

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

    it('renders group avatar', () => {
      expect(findGlAvatar().exists()).toBe(true);
      expect(findGlAvatar().props('src')).toBe(mockGroup1.avatar_url);
    });

    it('renders group name', () => {
      expect(findGroupName().text()).toBe(mockGroup1.full_name);
    });

    it('renders group description', () => {
      expect(findGroupDescription().text()).toBe(mockGroup1.description);
    });

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

  describe('on Link button click', () => {
    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 wrapper.vm.$nextTick();

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

      expect(addSubscriptionSpy).toHaveBeenCalledWith(mockSubscriptionPath, mockGroup1.full_path);
    });

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

        await waitForPromises();

        expect(reloadSpy).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(reloadSpy).not.toHaveBeenCalled();
        expect(wrapper.emitted('error')[0][0]).toBe(mockErrorMessage);
      });
    });
  });
});