summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/issuable/issuable_blocked_icon_spec.js
blob: a0b1d64b97c8116f16e86842a4ea1cd901709dd9 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { GlIcon, GlLink, GlPopover, GlLoadingIcon } from '@gitlab/ui';
import { shallowMount, mount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import IssuableBlockedIcon from '~/vue_shared/components/issuable_blocked_icon/issuable_blocked_icon.vue';
import { blockingIssuablesQueries } from '~/vue_shared/components/issuable_blocked_icon/constants';
import { issuableTypes } from '~/boards/constants';
import { TYPE_ISSUE } from '~/issues/constants';
import { truncate } from '~/lib/utils/text_utility';
import {
  mockIssue,
  mockEpic,
  mockBlockingIssue1,
  mockBlockingIssue2,
  mockBlockingEpic1,
  mockBlockingIssuablesResponse1,
  mockBlockingIssuablesResponse2,
  mockBlockingIssuablesResponse3,
  mockBlockedIssue1,
  mockBlockedIssue2,
  mockBlockedEpic1,
  mockBlockingEpicIssuablesResponse1,
} from '../../boards/mock_data';

describe('IssuableBlockedIcon', () => {
  let wrapper;
  let mockApollo;

  const findGlIcon = () => wrapper.findComponent(GlIcon);
  const findGlPopover = () => wrapper.findComponent(GlPopover);
  const findGlLink = () => wrapper.findComponent(GlLink);
  const findPopoverTitle = () => wrapper.findByTestId('popover-title');
  const findIssuableTitle = () => wrapper.findByTestId('issuable-title');
  const findHiddenBlockingCount = () => wrapper.findByTestId('hidden-blocking-count');
  const findViewAllIssuableLink = () => wrapper.findByTestId('view-all-issues');

  const waitForApollo = async () => {
    jest.runOnlyPendingTimers();
    await waitForPromises();
  };

  const mouseenter = async () => {
    findGlIcon().vm.$emit('mouseenter');

    await nextTick();
    await waitForApollo();
  };

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

  const createWrapperWithApollo = ({
    item = mockBlockedIssue1,
    blockingIssuablesSpy = jest.fn().mockResolvedValue(mockBlockingIssuablesResponse1),
    issuableItem = mockIssue,
    issuableType = TYPE_ISSUE,
  } = {}) => {
    mockApollo = createMockApollo([
      [blockingIssuablesQueries[issuableType].query, blockingIssuablesSpy],
    ]);

    Vue.use(VueApollo);
    wrapper = extendedWrapper(
      mount(IssuableBlockedIcon, {
        apolloProvider: mockApollo,
        propsData: {
          item: {
            ...issuableItem,
            ...item,
          },
          uniqueId: 'uniqueId',
          issuableType,
        },
        attachTo: document.body,
      }),
    );
  };

  const createWrapper = ({
    item = {},
    queries = {},
    data = {},
    loading = false,
    mockIssuable = mockIssue,
    issuableType = TYPE_ISSUE,
  } = {}) => {
    wrapper = extendedWrapper(
      shallowMount(IssuableBlockedIcon, {
        propsData: {
          item: {
            ...mockIssuable,
            ...item,
          },
          uniqueId: 'uniqueid',
          issuableType,
        },
        data() {
          return {
            ...data,
          };
        },
        mocks: {
          $apollo: {
            queries: {
              blockingIssuables: { loading },
              ...queries,
            },
          },
        },
        stubs: {
          GlPopover,
        },
        attachTo: document.body,
      }),
    );
  };

  it.each`
    mockIssuable | issuableType          | expectedIcon
    ${mockIssue} | ${TYPE_ISSUE}         | ${'issue-block'}
    ${mockEpic}  | ${issuableTypes.epic} | ${'entity-blocked'}
  `(
    'should render blocked icon for $issuableType',
    ({ mockIssuable, issuableType, expectedIcon }) => {
      createWrapper({
        mockIssuable,
        issuableType,
      });

      expect(findGlIcon().exists()).toBe(true);
      const icon = findGlIcon();
      expect(icon.exists()).toBe(true);
      expect(icon.props('name')).toBe(expectedIcon);
    },
  );

  it('should display a loading spinner while loading', () => {
    createWrapper({ loading: true });

    expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
  });

  it('should not query for blocking issuables by default', async () => {
    createWrapperWithApollo();

    expect(findGlPopover().text()).not.toContain(mockBlockingIssue1.title);
  });

  describe('on mouseenter on blocked icon', () => {
    it.each`
      item                 | issuableType          | mockBlockingIssuable  | issuableItem | blockingIssuablesSpy
      ${mockBlockedIssue1} | ${TYPE_ISSUE}         | ${mockBlockingIssue1} | ${mockIssue} | ${jest.fn().mockResolvedValue(mockBlockingIssuablesResponse1)}
      ${mockBlockedEpic1}  | ${issuableTypes.epic} | ${mockBlockingEpic1}  | ${mockEpic}  | ${jest.fn().mockResolvedValue(mockBlockingEpicIssuablesResponse1)}
    `(
      'should query for blocking issuables and render the result for $issuableType',
      async ({ item, issuableType, issuableItem, mockBlockingIssuable, blockingIssuablesSpy }) => {
        createWrapperWithApollo({
          item,
          issuableType,
          issuableItem,
          blockingIssuablesSpy,
        });

        expect(findGlPopover().text()).not.toContain(mockBlockingIssuable.title);

        await mouseenter();

        expect(findGlPopover().exists()).toBe(true);
        expect(findIssuableTitle().text()).toContain(mockBlockingIssuable.title);
        expect(wrapper.vm.skip).toBe(true);
      },
    );

    it('should emit "blocking-issuables-error" event on query error', async () => {
      const mockError = new Error('mayday');
      createWrapperWithApollo({ blockingIssuablesSpy: jest.fn().mockRejectedValue(mockError) });

      await mouseenter();

      const [
        [
          {
            message,
            error: { networkError },
          },
        ],
      ] = wrapper.emitted('blocking-issuables-error');
      expect(message).toBe('Failed to fetch blocking issues');
      expect(networkError).toBe(mockError);
    });

    describe('with a single blocking issue', () => {
      beforeEach(async () => {
        createWrapperWithApollo();

        await mouseenter();
      });

      it('should render a title of the issuable', async () => {
        expect(findIssuableTitle().text()).toBe(mockBlockingIssue1.title);
      });

      it('should render issuable reference and link to the issuable', async () => {
        const formattedRef = mockBlockingIssue1.reference.split('/')[1];

        expect(findGlLink().text()).toBe(formattedRef);
        expect(findGlLink().attributes('href')).toBe(mockBlockingIssue1.webUrl);
      });

      it('should render popover title with correct blocking issuable count', async () => {
        expect(findPopoverTitle().text()).toBe('Blocked by 1 issue');
      });
    });

    describe('when issue has a long title', () => {
      it('should render a truncated title', async () => {
        createWrapperWithApollo({
          blockingIssuablesSpy: jest.fn().mockResolvedValue(mockBlockingIssuablesResponse2),
        });

        await mouseenter();

        const truncatedTitle = truncate(
          mockBlockingIssue2.title,
          wrapper.vm.$options.textTruncateWidth,
        );
        expect(findIssuableTitle().text()).toBe(truncatedTitle);
      });
    });

    describe('with more than three blocking issues', () => {
      beforeEach(async () => {
        createWrapperWithApollo({
          item: mockBlockedIssue2,
          blockingIssuablesSpy: jest.fn().mockResolvedValue(mockBlockingIssuablesResponse3),
        });

        await mouseenter();
      });

      it('matches the snapshot', () => {
        expect(wrapper.html()).toMatchSnapshot();
      });

      it('should render popover title with correct blocking issuable count', async () => {
        expect(findPopoverTitle().text()).toBe('Blocked by 4 issues');
      });

      it('should render the number of hidden blocking issuables', () => {
        expect(findHiddenBlockingCount().text()).toBe('+ 1 more issue');
      });

      it('should link to the blocked issue page at the related issue anchor', async () => {
        expect(findViewAllIssuableLink().text()).toBe('View all blocking issues');
        expect(findViewAllIssuableLink().attributes('href')).toBe(
          `${mockBlockedIssue2.webUrl}#related-issues`,
        );
      });
    });
  });
});