summaryrefslogtreecommitdiff
path: root/spec/javascripts/pipeline_schedules/setup_pipeline_variable_list_spec.js
blob: 5b316b319a56155b15c16bc522c8852fbca2b458 (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
import {
  setupPipelineVariableList,
  insertRow,
  removeRow,
} from '~/pipeline_schedules/setup_pipeline_variable_list';

describe('Pipeline Variable List', () => {
  let $markup;

  describe('insertRow', () => {
    it('should insert another row', () => {
      $markup = $(`<div>
        <li class="js-row">
          <input>
          <textarea></textarea>
        </li>
      </div>`);

      insertRow($markup.find('.js-row'));

      expect($markup.find('.js-row').length).toBe(2);
    });

    it('should clear `data-is-persisted` on cloned row', () => {
      $markup = $(`<div>
        <li class="js-row" data-is-persisted="true"></li>
      </div>`);

      insertRow($markup.find('.js-row'));

      const $lastRow = $markup.find('.js-row').last();
      expect($lastRow.attr('data-is-persisted')).toBe(undefined);
    });

    it('should clear inputs on cloned row', () => {
      $markup = $(`<div>
        <li class="js-row">
          <input value="foo">
          <textarea>bar</textarea>
        </li>
      </div>`);

      insertRow($markup.find('.js-row'));

      const $lastRow = $markup.find('.js-row').last();
      expect($lastRow.find('input').val()).toBe('');
      expect($lastRow.find('textarea').val()).toBe('');
    });
  });

  describe('removeRow', () => {
    it('should remove dynamic row', () => {
      $markup = $(`<div>
        <li class="js-row">
          <input>
          <textarea></textarea>
        </li>
      </div>`);

      removeRow($markup.find('.js-row'));

      expect($markup.find('.js-row').length).toBe(0);
    });

    it('should hide and mark to destroy with already persisted rows', () => {
      $markup = $(`<div>
        <li class="js-row" data-is-persisted="true">
          <input class="js-destroy-input">
        </li>
      </div>`);

      const $row = $markup.find('.js-row');
      removeRow($row);

      expect($row.find('.js-destroy-input').val()).toBe('1');
      expect($markup.find('.js-row').length).toBe(1);
    });
  });

  describe('setupPipelineVariableList', () => {
    beforeEach(() => {
      $markup = $(`<form>
        <li class="js-row">
          <input class="js-user-input" name="schedule[variables_attributes][][key]">
          <textarea class="js-user-input" name="schedule[variables_attributes][][value]"></textarea>
          <button class="js-row-remove-button"></button>
          <button class="js-row-add-button"></button>
        </li>
      </form>`);

      setupPipelineVariableList($markup);
    });

    it('should remove the row when clicking the remove button', () => {
      $markup.find('.js-row-remove-button').trigger('click');

      expect($markup.find('.js-row').length).toBe(0);
    });

    it('should add another row when editing the last rows key input', () => {
      const $row = $markup.find('.js-row');
      $row.find('input.js-user-input')
        .val('foo')
        .trigger('input');

      expect($markup.find('.js-row').length).toBe(2);
    });

    it('should add another row when editing the last rows value textarea', () => {
      const $row = $markup.find('.js-row');
      $row.find('textarea.js-user-input')
        .val('foo')
        .trigger('input');

      expect($markup.find('.js-row').length).toBe(2);
    });

    it('should remove empty row after blurring', () => {
      const $row = $markup.find('.js-row');
      $row.find('input.js-user-input')
        .val('foo')
        .trigger('input');

      expect($markup.find('.js-row').length).toBe(2);

      $row.find('input.js-user-input')
        .val('')
        .trigger('input')
        .trigger('blur');

      expect($markup.find('.js-row').length).toBe(1);
    });

    it('should clear out the `name` attribute on the inputs for the last empty row on form submission (avoid BE validation)', () => {
      const $row = $markup.find('.js-row');
      expect($row.find('input').attr('name')).toBe('schedule[variables_attributes][][key]');
      expect($row.find('textarea').attr('name')).toBe('schedule[variables_attributes][][value]');

      $markup.filter('form').submit();

      expect($row.find('input').attr('name')).toBe('');
      expect($row.find('textarea').attr('name')).toBe('');
    });
  });
});