summaryrefslogtreecommitdiff
path: root/spec/frontend/ci/pipeline_new/components/refs_dropdown_spec.js
blob: cf8009e388fcf7ca370eb77f110e361540127bf4 (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
import { GlListbox, GlListboxItem } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';

import RefsDropdown from '~/ci/pipeline_new/components/refs_dropdown.vue';

import { mockBranches, mockRefs, mockFilteredRefs, mockTags } from '../mock_data';

const projectRefsEndpoint = '/root/project/refs';
const refShortName = 'main';
const refFullName = 'refs/heads/main';

jest.mock('~/flash');

describe('Pipeline New Form', () => {
  let wrapper;
  let mock;

  const findDropdown = () => wrapper.findComponent(GlListbox);
  const findRefsDropdownItems = () => wrapper.findAllComponents(GlListboxItem);
  const findSearchBox = () => wrapper.findByTestId('listbox-search-input');
  const findListboxGroups = () => wrapper.findAll('ul[role="group"]');

  const createComponent = (props = {}, mountFn = shallowMountExtended) => {
    wrapper = mountFn(RefsDropdown, {
      provide: {
        projectRefsEndpoint,
      },
      propsData: {
        value: {
          shortName: refShortName,
          fullName: refFullName,
        },
        ...props,
      },
    });
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
    mock.onGet(projectRefsEndpoint, { params: { search: '' } }).reply(HTTP_STATUS_OK, mockRefs);
  });

  beforeEach(() => {
    createComponent();
  });

  it('displays empty dropdown initially', () => {
    findDropdown().vm.$emit('shown');

    expect(findRefsDropdownItems()).toHaveLength(0);
  });

  it('does not make requests immediately', async () => {
    expect(mock.history.get).toHaveLength(0);
  });

  describe('when user opens dropdown', () => {
    beforeEach(async () => {
      createComponent({}, mountExtended);
      findDropdown().vm.$emit('shown');
      await waitForPromises();
    });

    it('requests unfiltered tags and branches', () => {
      expect(mock.history.get).toHaveLength(1);
      expect(mock.history.get[0].url).toBe(projectRefsEndpoint);
      expect(mock.history.get[0].params).toEqual({ search: '' });
    });

    it('displays dropdown with branches and tags', () => {
      const refLength = mockRefs.Tags.length + mockRefs.Branches.length;
      expect(findRefsDropdownItems()).toHaveLength(refLength);
    });

    it('displays the names of refs', () => {
      // Branches
      expect(findRefsDropdownItems().at(0).text()).toBe(mockRefs.Branches[0]);

      // Tags (appear after branches)
      const firstTag = mockRefs.Branches.length;
      expect(findRefsDropdownItems().at(firstTag).text()).toBe(mockRefs.Tags[0]);
    });

    it('when user shows dropdown a second time, only one request is done', () => {
      expect(mock.history.get).toHaveLength(1);
    });

    describe('when user selects a value', () => {
      const selectedIndex = 1;

      beforeEach(async () => {
        findRefsDropdownItems().at(selectedIndex).vm.$emit('select', 'refs/heads/branch-1');
        await waitForPromises();
      });

      it('component emits @input', () => {
        const inputs = wrapper.emitted('input');

        expect(inputs).toHaveLength(1);
        expect(inputs[0]).toEqual([{ shortName: 'branch-1', fullName: 'refs/heads/branch-1' }]);
      });
    });

    describe('when user types searches for a tag', () => {
      const mockSearchTerm = 'my-search';

      beforeEach(async () => {
        mock
          .onGet(projectRefsEndpoint, { params: { search: mockSearchTerm } })
          .reply(HTTP_STATUS_OK, mockFilteredRefs);

        await findSearchBox().vm.$emit('input', mockSearchTerm);
        await waitForPromises();
      });

      it('requests filtered tags and branches', async () => {
        expect(mock.history.get).toHaveLength(2);
        expect(mock.history.get[1].params).toEqual({
          search: mockSearchTerm,
        });
      });

      it('displays dropdown with branches and tags', async () => {
        const filteredRefLength = mockFilteredRefs.Tags.length + mockFilteredRefs.Branches.length;

        expect(findRefsDropdownItems()).toHaveLength(filteredRefLength);
      });
    });
  });

  describe('when user has selected a value', () => {
    const selectedIndex = 1;
    const mockShortName = mockRefs.Branches[selectedIndex];
    const mockFullName = `refs/heads/${mockShortName}`;

    beforeEach(async () => {
      mock
        .onGet(projectRefsEndpoint, {
          params: { ref: mockFullName },
        })
        .reply(HTTP_STATUS_OK, mockRefs);

      createComponent(
        {
          value: {
            shortName: mockShortName,
            fullName: mockFullName,
          },
        },
        mountExtended,
      );
      findDropdown().vm.$emit('shown');
      await waitForPromises();
    });

    it('branch is checked', () => {
      expect(findRefsDropdownItems().at(selectedIndex).props('isSelected')).toBe(true);
    });
  });

  describe('when server returns an error', () => {
    beforeEach(async () => {
      mock
        .onGet(projectRefsEndpoint, { params: { search: '' } })
        .reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);

      findDropdown().vm.$emit('shown');
      await waitForPromises();
    });

    it('loading error event is emitted', () => {
      expect(wrapper.emitted('loadingError')).toHaveLength(1);
      expect(wrapper.emitted('loadingError')[0]).toEqual([expect.any(Error)]);
    });
  });

  describe('should display branches and tags based on its length', () => {
    it.each`
      mockData                                    | expectedGroupLength | expectedListboxItemsLength
      ${{ ...mockBranches, Tags: [] }}            | ${1}                | ${mockBranches.Branches.length}
      ${{ Branches: [], ...mockTags }}            | ${1}                | ${mockTags.Tags.length}
      ${{ ...mockRefs }}                          | ${2}                | ${mockBranches.Branches.length + mockTags.Tags.length}
      ${{ Branches: undefined, Tags: undefined }} | ${0}                | ${0}
    `(
      'should render branches and tags based on presence',
      async ({ mockData, expectedGroupLength, expectedListboxItemsLength }) => {
        mock.onGet(projectRefsEndpoint, { params: { search: '' } }).reply(HTTP_STATUS_OK, mockData);
        createComponent({}, mountExtended);
        findDropdown().vm.$emit('shown');
        await waitForPromises();

        expect(findListboxGroups()).toHaveLength(expectedGroupLength);
        expect(findRefsDropdownItems()).toHaveLength(expectedListboxItemsLength);
      },
    );
  });
});