Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab_ui_form_builder_spec.rb « form_builders « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98fb154fb0501c34275400d46a1f2abf22d4b9de (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::FormBuilders::GitlabUiFormBuilder do
  include FormBuilderHelpers

  let_it_be(:user) { build(:user, :admin) }

  let_it_be(:form_builder) { described_class.new(:user, user, fake_action_view_base, {}) }

  describe '#submit' do
    context 'without pajamas_button enabled' do
      subject(:submit_html) do
        form_builder.submit('Save', class: 'gl-button btn-confirm custom-class', data: { test: true })
      end

      it 'renders a submit input' do
        expected_html = <<~EOS
        <input type="submit" name="commit" value="Save" class="gl-button btn-confirm custom-class" data-test="true" data-disable-with="Save" />
        EOS

        expect(html_strip_whitespace(submit_html)).to eq(html_strip_whitespace(expected_html))
      end
    end

    context 'with pajamas_button enabled' do
      subject(:submit_html) do
        form_builder.submit('Save', pajamas_button: true, class: 'custom-class', data: { test: true })
      end

      it 'renders a submit button' do
        expected_html = <<~EOS
        <button class="gl-button btn btn-md btn-confirm custom-class" data-test="true" type="submit">
          <span class="gl-button-text">
            Save
          </span>
        </button>
        EOS

        expect(html_strip_whitespace(submit_html)).to eq(html_strip_whitespace(expected_html))
      end
    end
  end

  describe '#gitlab_ui_checkbox_component' do
    context 'when not using slots' do
      let(:optional_args) { {} }

      subject(:checkbox_html) do
        form_builder.gitlab_ui_checkbox_component(
          :view_diffs_file_by_file,
          "Show one file at a time on merge request's Changes tab",
          **optional_args
        )
      end

      context 'without optional arguments' do
        it 'renders correct html' do
          expected_html = <<~EOS
            <div class="gl-form-checkbox custom-control custom-checkbox">
              <input name="user[view_diffs_file_by_file]" type="hidden" value="0" autocomplete="off" />
              <input class="custom-control-input" type="checkbox" value="1" name="user[view_diffs_file_by_file]" id="user_view_diffs_file_by_file" />
              <label class="custom-control-label" for="user_view_diffs_file_by_file">
                <span>Show one file at a time on merge request&#39;s Changes tab</span>
              </label>
            </div>
          EOS

          expect(html_strip_whitespace(checkbox_html)).to eq(html_strip_whitespace(expected_html))
        end
      end

      context 'with optional arguments' do
        let(:optional_args) do
          {
            help_text: 'Instead of all the files changed, show only one file at a time.',
            checkbox_options: { class: 'checkbox-foo-bar' },
            label_options: { class: 'label-foo-bar' },
            checked_value: '3',
            unchecked_value: '1'
          }
        end

        it 'renders help text' do
          expected_html = <<~EOS
            <div class="gl-form-checkbox custom-control custom-checkbox">
              <input name="user[view_diffs_file_by_file]" type="hidden" value="1" autocomplete="off" />
              <input class="custom-control-input checkbox-foo-bar" type="checkbox" value="3" name="user[view_diffs_file_by_file]" id="user_view_diffs_file_by_file" />
              <label class="custom-control-label label-foo-bar" for="user_view_diffs_file_by_file">
                <span>Show one file at a time on merge request&#39;s Changes tab</span>
                <p class="help-text" data-testid="pajamas-component-help-text">Instead of all the files changed, show only one file at a time.</p>
              </label>
            </div>
          EOS

          expect(html_strip_whitespace(checkbox_html)).to eq(html_strip_whitespace(expected_html))
        end
      end

      context 'with checkbox_options: { multiple: true }' do
        let(:optional_args) do
          {
            checkbox_options: { multiple: true },
            checked_value: 'one',
            unchecked_value: false
          }
        end

        it 'renders labels with correct for attributes' do
          expected_html = <<~EOS
            <div class="gl-form-checkbox custom-control custom-checkbox">
              <input class="custom-control-input" type="checkbox" value="one" name="user[view_diffs_file_by_file][]" id="user_view_diffs_file_by_file_one" />
              <label class="custom-control-label" for="user_view_diffs_file_by_file_one">
                <span>Show one file at a time on merge request&#39;s Changes tab</span>
              </label>
            </div>
          EOS

          expect(html_strip_whitespace(checkbox_html)).to eq(html_strip_whitespace(expected_html))
        end
      end
    end

    context 'when using slots' do
      subject(:checkbox_html) do
        form_builder.gitlab_ui_checkbox_component(
          :view_diffs_file_by_file
        ) do |c|
          c.label { "Show one file at a time on merge request's Changes tab" }
          c.help_text { 'Instead of all the files changed, show only one file at a time.' }
        end
      end

      it 'renders correct html' do
        expected_html = <<~EOS
          <div class="gl-form-checkbox custom-control custom-checkbox">
            <input name="user[view_diffs_file_by_file]" type="hidden" value="0" autocomplete="off" />
            <input class="custom-control-input" type="checkbox" value="1" name="user[view_diffs_file_by_file]" id="user_view_diffs_file_by_file" />
            <label class="custom-control-label" for="user_view_diffs_file_by_file">
              <span>Show one file at a time on merge request&#39;s Changes tab</span>
              <p class="help-text" data-testid="pajamas-component-help-text">Instead of all the files changed, show only one file at a time.</p>
            </label>
          </div>
        EOS

        expect(html_strip_whitespace(checkbox_html)).to eq(html_strip_whitespace(expected_html))
      end
    end
  end

  describe '#gitlab_ui_radio_component' do
    context 'when not using slots' do
      let(:optional_args) { {} }

      subject(:radio_html) do
        form_builder.gitlab_ui_radio_component(
          :access_level,
          :admin,
          "Admin",
          **optional_args
        )
      end

      context 'without optional arguments' do
        it 'renders correct html' do
          expected_html = <<~EOS
            <div class="gl-form-radio custom-control custom-radio">
              <input class="custom-control-input" type="radio" value="admin" checked="checked" name="user[access_level]" id="user_access_level_admin" />
              <label class="custom-control-label" for="user_access_level_admin">
                <span>Admin</span>
              </label>
            </div>
          EOS

          expect(html_strip_whitespace(radio_html)).to eq(html_strip_whitespace(expected_html))
        end
      end

      context 'with optional arguments' do
        let(:optional_args) do
          {
            help_text: 'Administrators have access to all groups, projects, and users and can manage all features in this installation',
            radio_options: { class: 'radio-foo-bar' },
            label_options: { class: 'label-foo-bar' }
          }
        end

        it 'renders help text' do
          expected_html = <<~EOS
            <div class="gl-form-radio custom-control custom-radio">
              <input class="custom-control-input radio-foo-bar" type="radio" value="admin" checked="checked" name="user[access_level]" id="user_access_level_admin" />
              <label class="custom-control-label label-foo-bar" for="user_access_level_admin">
                <span>Admin</span>
                <p class="help-text" data-testid="pajamas-component-help-text">Administrators have access to all groups, projects, and users and can manage all features in this installation</p>
              </label>
            </div>
          EOS

          expect(html_strip_whitespace(radio_html)).to eq(html_strip_whitespace(expected_html))
        end
      end
    end

    context 'when using slots' do
      subject(:radio_html) do
        form_builder.gitlab_ui_radio_component(
          :access_level,
          :admin
        ) do |c|
          c.label { "Admin" }
          c.help_text { 'Administrators have access to all groups, projects, and users and can manage all features in this installation' }
        end
      end

      it 'renders correct html' do
        expected_html = <<~EOS
          <div class="gl-form-radio custom-control custom-radio">
            <input class="custom-control-input" type="radio" value="admin" checked="checked" name="user[access_level]" id="user_access_level_admin" />
            <label class="custom-control-label" for="user_access_level_admin">
              <span>Admin</span>
              <p class="help-text" data-testid="pajamas-component-help-text">Administrators have access to all groups, projects, and users and can manage all features in this installation</p>
            </label>
          </div>
        EOS

        expect(html_strip_whitespace(radio_html)).to eq(html_strip_whitespace(expected_html))
      end
    end
  end

  describe '#gitlab_ui_datepicker' do
    subject(:datepicker_html) do
      form_builder.gitlab_ui_datepicker(
        :expires_at,
        **optional_args
      )
    end

    let(:optional_args) { {} }

    context 'without optional arguments' do
      it 'renders correct html' do
        expected_html = <<~EOS
          <input class="datepicker form-control gl-form-input" type="text" name="user[expires_at]" id="user_expires_at" />
        EOS

        expect(html_strip_whitespace(datepicker_html)).to eq(html_strip_whitespace(expected_html))
      end
    end

    context 'with optional arguments' do
      let(:optional_args) do
        {
          id: 'milk_gone_bad',
          data: { action: 'throw' },
          value: '2022-08-01'
        }
      end

      it 'renders correct html' do
        expected_html = <<~EOS
          <input id="milk_gone_bad" data-action="throw" value="2022-08-01" class="datepicker form-control gl-form-input" type="text" name="user[expires_at]" />
        EOS

        expect(html_strip_whitespace(datepicker_html)).to eq(html_strip_whitespace(expected_html))
      end
    end
  end

  private

  def html_strip_whitespace(html)
    html.lines.map(&:strip).join('')
  end
end