summaryrefslogtreecommitdiff
path: root/spec/javascripts/ci_variable_list/ajax_variable_list_spec.js
blob: 481b1a4d4b090c33d09ce3ba50caa9d6b7487a68 (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
import $ from 'jquery';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import AjaxFormVariableList from '~/ci_variable_list/ajax_variable_list';

const VARIABLE_PATCH_ENDPOINT = 'http://test.host/frontend-fixtures/builds-project/variables';
const HIDE_CLASS = 'hide';

describe('AjaxFormVariableList', () => {
  preloadFixtures('projects/ci_cd_settings.html');
  preloadFixtures('projects/ci_cd_settings_with_variables.html');

  let container;
  let saveButton;
  let errorBox;

  let mock;
  let ajaxVariableList;

  beforeEach(() => {
    loadFixtures('projects/ci_cd_settings.html');
    container = document.querySelector('.js-ci-variable-list-section');

    mock = new MockAdapter(axios);

    const ajaxVariableListEl = document.querySelector('.js-ci-variable-list-section');
    saveButton = ajaxVariableListEl.querySelector('.js-ci-variables-save-button');
    errorBox = container.querySelector('.js-ci-variable-error-box');
    ajaxVariableList = new AjaxFormVariableList({
      container,
      formField: 'variables',
      saveButton,
      errorBox,
      saveEndpoint: container.dataset.saveEndpoint,
    });

    spyOn(ajaxVariableList, 'updateRowsWithPersistedVariables').and.callThrough();
    spyOn(ajaxVariableList.variableList, 'toggleEnableRow').and.callThrough();
  });

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

  describe('onSaveClicked', () => {
    it('shows loading spinner while waiting for the request', done => {
      const loadingIcon = saveButton.querySelector('.js-ci-variables-save-loading-icon');

      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(() => {
        expect(loadingIcon.classList.contains(HIDE_CLASS)).toEqual(false);

        return [200, {}];
      });

      expect(loadingIcon.classList.contains(HIDE_CLASS)).toEqual(true);

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(loadingIcon.classList.contains(HIDE_CLASS)).toEqual(true);
        })
        .then(done)
        .catch(done.fail);
    });

    it('calls `updateRowsWithPersistedVariables` with the persisted variables', done => {
      const variablesResponse = [{ id: 1, key: 'foo', value: 'bar' }];
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(200, {
        variables: variablesResponse,
      });

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(ajaxVariableList.updateRowsWithPersistedVariables).toHaveBeenCalledWith(
            variablesResponse,
          );
        })
        .then(done)
        .catch(done.fail);
    });

    it('hides any previous error box', done => {
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(200);

      expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(true);

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(true);
        })
        .then(done)
        .catch(done.fail);
    });

    it('disables remove buttons while waiting for the request', done => {
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(() => {
        expect(ajaxVariableList.variableList.toggleEnableRow).toHaveBeenCalledWith(false);

        return [200, {}];
      });

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(ajaxVariableList.variableList.toggleEnableRow).toHaveBeenCalledWith(true);
        })
        .then(done)
        .catch(done.fail);
    });

    it('hides secret values', done => {
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(200, {});

      const row = container.querySelector('.js-row:first-child');
      const valueInput = row.querySelector('.js-ci-variable-input-value');
      const valuePlaceholder = row.querySelector('.js-secret-value-placeholder');

      valueInput.value = 'bar';
      $(valueInput).trigger('input');

      expect(valuePlaceholder.classList.contains(HIDE_CLASS)).toBe(true);
      expect(valueInput.classList.contains(HIDE_CLASS)).toBe(false);

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(valuePlaceholder.classList.contains(HIDE_CLASS)).toBe(false);
          expect(valueInput.classList.contains(HIDE_CLASS)).toBe(true);
        })
        .then(done)
        .catch(done.fail);
    });

    it('shows error box with validation errors', done => {
      const validationError = 'some validation error';
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(400, [validationError]);

      expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(true);

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(false);
          expect(errorBox.textContent.trim().replace(/\n+\s+/m, ' ')).toEqual(
            `Validation failed ${validationError}`,
          );
        })
        .then(done)
        .catch(done.fail);
    });

    it('shows flash message when request fails', done => {
      mock.onPatch(VARIABLE_PATCH_ENDPOINT).reply(500);

      expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(true);

      ajaxVariableList
        .onSaveClicked()
        .then(() => {
          expect(errorBox.classList.contains(HIDE_CLASS)).toEqual(true);
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('updateRowsWithPersistedVariables', () => {
    beforeEach(() => {
      loadFixtures('projects/ci_cd_settings_with_variables.html');
      container = document.querySelector('.js-ci-variable-list-section');

      const ajaxVariableListEl = document.querySelector('.js-ci-variable-list-section');
      saveButton = ajaxVariableListEl.querySelector('.js-ci-variables-save-button');
      errorBox = container.querySelector('.js-ci-variable-error-box');
      ajaxVariableList = new AjaxFormVariableList({
        container,
        formField: 'variables',
        saveButton,
        errorBox,
        saveEndpoint: container.dataset.saveEndpoint,
      });
    });

    it('removes variable that was removed', () => {
      expect(container.querySelectorAll('.js-row').length).toBe(3);

      container.querySelector('.js-row-remove-button').click();

      expect(container.querySelectorAll('.js-row').length).toBe(3);

      ajaxVariableList.updateRowsWithPersistedVariables([]);

      expect(container.querySelectorAll('.js-row').length).toBe(2);
    });

    it('updates new variable row with persisted ID', () => {
      const row = container.querySelector('.js-row:last-child');
      const idInput = row.querySelector('.js-ci-variable-input-id');
      const keyInput = row.querySelector('.js-ci-variable-input-key');
      const valueInput = row.querySelector('.js-ci-variable-input-value');

      keyInput.value = 'foo';
      $(keyInput).trigger('input');
      valueInput.value = 'bar';
      $(valueInput).trigger('input');

      expect(idInput.value).toEqual('');

      ajaxVariableList.updateRowsWithPersistedVariables([
        {
          id: 3,
          key: 'foo',
          value: 'bar',
        },
      ]);

      expect(idInput.value).toEqual('3');
      expect(row.dataset.isPersisted).toEqual('true');
    });
  });
});