summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/environments_app_spec.js
blob: 1abdeff614c2344b084ba048f3fc08487818ab8c (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { GlTabs } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import Container from '~/environments/components/container.vue';
import DeployBoard from '~/environments/components/deploy_board.vue';
import EmptyState from '~/environments/components/empty_state.vue';
import EnableReviewAppModal from '~/environments/components/enable_review_app_modal.vue';
import EnvironmentsApp from '~/environments/components/environments_app.vue';
import axios from '~/lib/utils/axios_utils';
import * as urlUtils from '~/lib/utils/url_utility';
import { environment, folder } from './mock_data';

describe('Environment', () => {
  let mock;
  let wrapper;

  const mockData = {
    endpoint: 'environments.json',
    canCreateEnvironment: true,
    canReadEnvironment: true,
    newEnvironmentPath: 'environments/new',
    helpPagePath: 'help',
    userCalloutsPath: '/callouts',
    lockPromotionSvgPath: '/assets/illustrations/lock-promotion.svg',
    helpCanaryDeploymentsPath: 'help/canary-deployments',
  };

  const mockRequest = (response, body) => {
    mock.onGet(mockData.endpoint).reply(response, body, {
      'X-nExt-pAge': '2',
      'x-page': '1',
      'X-Per-Page': '1',
      'X-Prev-Page': '',
      'X-TOTAL': '37',
      'X-Total-Pages': '2',
    });
  };

  const createWrapper = (shallow = false, props = {}) => {
    const fn = shallow ? shallowMount : mount;
    wrapper = extendedWrapper(fn(EnvironmentsApp, { propsData: { ...mockData, ...props } }));
    return axios.waitForAll();
  };

  const findEnableReviewAppButton = () => wrapper.findByTestId('enable-review-app');
  const findEnableReviewAppModal = () => wrapper.findAll(EnableReviewAppModal);
  const findNewEnvironmentButton = () => wrapper.findByTestId('new-environment');
  const findEnvironmentsTabAvailable = () => wrapper.find('.js-environments-tab-available > a');
  const findEnvironmentsTabStopped = () => wrapper.find('.js-environments-tab-stopped > a');

  beforeEach(() => {
    mock = new MockAdapter(axios);
  });

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

  describe('successful request', () => {
    describe('without environments', () => {
      beforeEach(() => {
        mockRequest(200, { environments: [] });
        return createWrapper();
      });

      it('should render the empty state', () => {
        expect(wrapper.find(EmptyState).exists()).toBe(true);
      });
    });

    describe('with paginated environments', () => {
      const environmentList = [environment];

      beforeEach(() => {
        mockRequest(200, {
          environments: environmentList,
          stopped_count: 1,
          available_count: 0,
        });
        return createWrapper();
      });

      it('should render a container table with environments', () => {
        const containerTable = wrapper.find(Container);

        expect(containerTable.exists()).toBe(true);
        expect(containerTable.props('environments').length).toEqual(environmentList.length);
        expect(containerTable.find('.environment-name').text()).toEqual(environmentList[0].name);
      });

      describe('pagination', () => {
        it('should render pagination', () => {
          expect(wrapper.findAll('.gl-pagination li').length).toEqual(9);
        });

        it('should make an API request when page is clicked', () => {
          jest.spyOn(wrapper.vm, 'updateContent').mockImplementation(() => {});

          wrapper.find('.gl-pagination li:nth-child(3) .page-link').trigger('click');
          expect(wrapper.vm.updateContent).toHaveBeenCalledWith({
            scope: 'available',
            page: '2',
            nested: true,
          });
        });

        it('should make an API request when using tabs', () => {
          jest.spyOn(wrapper.vm, 'updateContent').mockImplementation(() => {});
          findEnvironmentsTabStopped().trigger('click');
          expect(wrapper.vm.updateContent).toHaveBeenCalledWith({
            scope: 'stopped',
            page: '1',
            nested: true,
          });
        });

        it('should not make the same API request when clicking on the current scope tab', () => {
          // component starts at available
          jest.spyOn(wrapper.vm, 'updateContent').mockImplementation(() => {});
          findEnvironmentsTabAvailable().trigger('click');
          expect(wrapper.vm.updateContent).toHaveBeenCalledTimes(0);
        });
      });

      describe('deploy boards', () => {
        beforeEach(() => {
          const deployEnvironment = {
            ...environment,
            rollout_status: {
              status: 'found',
            },
          };

          mockRequest(200, {
            environments: [deployEnvironment],
            stopped_count: 1,
            available_count: 0,
          });

          return createWrapper();
        });

        it('should render deploy boards', () => {
          expect(wrapper.find(DeployBoard).exists()).toBe(true);
        });

        it('should render arrow to open deploy boards', () => {
          expect(
            wrapper.find('.deploy-board-icon [data-testid="chevron-down-icon"]').exists(),
          ).toBe(true);
        });
      });
    });
  });

  describe('unsuccessful request', () => {
    beforeEach(() => {
      mockRequest(500, {});
      return createWrapper();
    });

    it('should render empty state', () => {
      expect(wrapper.find(EmptyState).exists()).toBe(true);
    });
  });

  describe('expandable folders', () => {
    beforeEach(() => {
      mockRequest(200, {
        environments: [folder],
        stopped_count: 1,
        available_count: 0,
      });

      mock.onGet(environment.folder_path).reply(200, { environments: [environment] });

      return createWrapper().then(() => {
        // open folder
        wrapper.find('.folder-name').trigger('click');
        return axios.waitForAll();
      });
    });

    it('should open a closed folder', () => {
      expect(wrapper.find('.folder-icon[data-testid="chevron-right-icon"]').exists()).toBe(false);
    });

    it('should close an opened folder', () => {
      expect(wrapper.find('.folder-icon[data-testid="chevron-down-icon"]').exists()).toBe(true);

      // close folder
      wrapper.find('.folder-name').trigger('click');
      wrapper.vm.$nextTick(() => {
        expect(wrapper.find('.folder-icon[data-testid="chevron-down-icon"]').exists()).toBe(false);
      });
    });

    it('should show children environments', () => {
      expect(wrapper.findAll('.js-child-row').length).toEqual(1);
    });

    it('should show a button to show all environments', () => {
      expect(wrapper.find('.text-center > a.btn').text()).toContain('Show all');
    });
  });

  describe('environment button', () => {
    describe('when user can create environment', () => {
      beforeEach(() => {
        mockRequest(200, { environments: [] });
        return createWrapper(true);
      });

      it('should render', () => {
        expect(findNewEnvironmentButton().exists()).toBe(true);
      });
    });

    describe('when user can not create environment', () => {
      beforeEach(() => {
        mockRequest(200, { environments: [] });
        return createWrapper(true, { ...mockData, canCreateEnvironment: false });
      });

      it('should not render', () => {
        expect(findNewEnvironmentButton().exists()).toBe(false);
      });
    });
  });

  describe('review app modal', () => {
    describe('when it is not possible to enable a review app', () => {
      beforeEach(() => {
        mockRequest(200, { environments: [] });
        return createWrapper(true);
      });

      it('should not render the enable review app button', () => {
        expect(findEnableReviewAppButton().exists()).toBe(false);
      });

      it('should not render a review app modal', () => {
        const modal = findEnableReviewAppModal();
        expect(modal).toHaveLength(0);
        expect(modal.exists()).toBe(false);
      });
    });

    describe('when it is possible to enable a review app', () => {
      beforeEach(() => {
        mockRequest(200, { environments: [], review_app: { can_setup_review_app: true } });
        return createWrapper(true);
      });

      it('should render the enable review app button', () => {
        expect(findEnableReviewAppButton().exists()).toBe(true);
        expect(findEnableReviewAppButton().text()).toContain('Enable review app');
      });

      it('should render only one review app modal', () => {
        const modal = findEnableReviewAppModal();
        expect(modal).toHaveLength(1);
        expect(modal.at(0).exists()).toBe(true);
      });
    });
  });

  describe('tabs', () => {
    beforeEach(() => {
      mockRequest(200, { environments: [] });
      jest
        .spyOn(urlUtils, 'getParameterByName')
        .mockImplementation((param) => (param === 'scope' ? 'stopped' : null));
      return createWrapper(true);
    });

    it('selects the tab for the parameter', () => {
      expect(wrapper.findComponent(GlTabs).attributes('value')).toBe('1');
    });
  });
});