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

button_helper_spec.rb « helpers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7153ca400634ea58ef8c3eacc4e80706d85d51e (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ButtonHelper do
  describe 'http_clone_button' do
    let(:user) { create(:user) }
    let(:project) { build_stubbed(:project) }
    let(:has_tooltip_class) { 'has-tooltip' }

    def element
      element = helper.http_clone_button(project)

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'with internal auth enabled' do
      context 'when user has a password' do
        it 'shows no tooltip' do
          expect(element.attr('class')).not_to include(has_tooltip_class)
        end
      end

      context 'when user has password automatically set' do
        let(:user) { create(:user, password_automatically_set: true) }

        it 'shows the password text on the dropdown' do
          description = element.search('.dropdown-menu-inner-content').first

          expect(description.inner_text).to eq 'Set a password on your account to pull or push via HTTP.'
        end
      end
    end

    context 'with internal auth disabled' do
      before do
        stub_application_setting(password_authentication_enabled_for_git?: false)
      end

      context 'when user has no personal access tokens' do
        it 'has a personal access token text on the dropdown description' do
          description = element.search('.dropdown-menu-inner-content').first

          expect(description.inner_text).to eq 'Create a personal access token on your account to pull or push via HTTP.'
        end
      end

      context 'when user has personal access tokens' do
        before do
          create(:personal_access_token, user: user)
        end

        it 'does not have a personal access token text on the dropdown description' do
          description = element.search('.dropdown-menu-inner-content').first

          expect(description).to be_nil
        end
      end
    end

    context 'when user is ldap user' do
      let(:user) { create(:omniauth_user, password_automatically_set: true) }

      it 'shows no tooltip' do
        expect(element.attr('class')).not_to include(has_tooltip_class)
      end
    end
  end

  describe 'ssh_button' do
    let(:user) { create(:user) }
    let(:project) { build_stubbed(:project) }

    def element
      element = helper.ssh_clone_button(project)

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'without an ssh key on the user' do
      it 'shows a warning on the dropdown description' do
        description = element.search('.dropdown-menu-inner-content').first

        expect(description.inner_text).to eq "You won't be able to pull or push repositories via SSH until you add an SSH key to your profile"
      end
    end

    context 'without an ssh key on the user and user_show_add_ssh_key_message unset' do
      before do
        stub_application_setting(user_show_add_ssh_key_message: false)
      end

      it 'there is no warning on the dropdown description' do
        description = element.search('.dropdown-menu-inner-content').first

        expect(description).to be_nil
      end
    end

    context 'with an ssh key on the user' do
      before do
        create(:key, user: user)
      end

      it 'there is no warning on the dropdown description' do
        description = element.search('.dropdown-menu-inner-content').first

        expect(description).to eq nil
      end
    end
  end

  describe 'ssh and http clone buttons' do
    let(:user) { create(:user) }
    let(:project) { build_stubbed(:project) }

    def http_button_element
      element = helper.http_clone_button(project, append_link: false)

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    def ssh_button_element
      element = helper.ssh_clone_button(project, append_link: false)

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    it 'only shows the title of any of the clone buttons when append_link is false' do
      expect(http_button_element.text).to eq('HTTP')
      expect(http_button_element.search('.dropdown-menu-inner-content').first).to eq(nil)
      expect(ssh_button_element.text).to eq('SSH')
      expect(ssh_button_element.search('.dropdown-menu-inner-content').first).to eq(nil)
    end
  end

  describe 'clipboard_button' do
    include IconsHelper

    let_it_be(:user) { create(:user) }
    let(:project) { build_stubbed(:project) }

    def element(data = {})
      element = helper.clipboard_button(data)
      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'with default options' do
      context 'when no `text` attribute is not provided' do
        it 'shows copy to clipboard button with default configuration and no text set to copy' do
          expect(element.attr('class')).to match('btn-sm')
          expect(element.attr('class')).to match('btn-default')
          expect(element.attr('class')).to match('btn-default-tertiary')
          expect(element.attr('title')).to eq('Copy')
          expect(element.attr('type')).to eq('button')
          expect(element.attr('aria-label')).to eq('Copy')
          expect(element.attr('aria-live')).to eq('polite')
          expect(element.attr('data-toggle')).to eq('tooltip')
          expect(element.attr('data-placement')).to eq('bottom')
          expect(element.attr('data-container')).to eq('body')
          expect(element.attr('data-clipboard-text')).to eq(nil)
          expect(element.attr('itemprop')).to eq(nil)
          expect(element.inner_text.strip).to eq('')

          expect(element.to_html).to match('svg#copy-to-clipboard')
        end
      end

      context 'when `text` attribute is provided' do
        it 'shows copy to clipboard button with provided `text` to copy' do
          expect(element(text: 'Hello World!').attr('data-clipboard-text')).to eq('Hello World!')
        end
      end

      context 'when `title` attribute is provided' do
        it 'shows copy to clipboard button with provided `title` as tooltip' do
          expect(element(title: 'Copy to my clipboard!').attr('aria-label')).to eq('Copy to my clipboard!')
        end
      end
    end

    context 'with `button_text` attribute provided' do
      it 'shows copy to clipboard button with provided `button_text` as button label' do
        expect(element(button_text: 'Copy text').inner_text.strip).to eq('Copy text')
      end

      it 'adds `gl-button-icon` class to icon' do
        expect(element(button_text: 'Copy text')).to have_css('svg.gl-button-icon')
      end
    end

    context 'with `hide_tooltip` attribute provided' do
      it 'shows copy to clipboard button without tooltip support' do
        expect(element(hide_tooltip: true).attr('data-placement')).to eq(nil)
        expect(element(hide_tooltip: true).attr('data-toggle')).to eq(nil)
        expect(element(hide_tooltip: true).attr('data-container')).to eq(nil)
      end
    end

    context 'with `hide_button_icon` attribute provided' do
      it 'shows copy to clipboard button without tooltip support' do
        expect(element(hide_button_icon: true).to_html).not_to match('svg#copy-to-clipboard')
      end
    end

    context 'with `itemprop` attribute provided' do
      it 'shows copy to clipboard button with `itemprop` attribute' do
        expect(element(itemprop: 'identifier').attr('itemprop')).to eq('identifier')
      end
    end

    context 'when variant option is provided' do
      it 'inherits the correct ButtonComponent class' do
        expect(element(variant: :confirm).attr('class')).to match('btn-confirm-tertiary')
      end
    end

    context 'when category option is provided' do
      it 'inherits the correct ButtonComponent class' do
        expect(element(category: :secondary).attr('class')).to match('btn-default-secondary')
      end
    end

    context 'when size option is provided' do
      it 'inherits the correct ButtonComponent class' do
        expect(element(size: :medium).attr('class')).to match('btn-md')
      end
    end
  end

  describe 'deprecated_clipboard_button' do
    include IconsHelper

    let(:user) { create(:user) }
    let(:project) { build_stubbed(:project) }

    def element(data = {})
      element = helper.deprecated_clipboard_button(data)
      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'with default options' do
      context 'when no `text` attribute is not provided' do
        it 'shows copy to clipboard button with default configuration and no text set to copy' do
          expect(element.attr('class')).to eq('btn btn-clipboard gl-button btn-default-tertiary btn-icon btn-sm')
          expect(element.attr('title')).to eq('Copy')
          expect(element.attr('type')).to eq('button')
          expect(element.attr('aria-label')).to eq('Copy')
          expect(element.attr('aria-live')).to eq('polite')
          expect(element.attr('data-toggle')).to eq('tooltip')
          expect(element.attr('data-placement')).to eq('bottom')
          expect(element.attr('data-container')).to eq('body')
          expect(element.attr('data-clipboard-text')).to eq(nil)
          expect(element.attr('itemprop')).to eq(nil)
          expect(element.inner_text).to eq("")

          expect(element.to_html).to include sprite_icon('copy-to-clipboard', css_class: 'gl-icon')
        end
      end

      context 'when `text` attribute is provided' do
        it 'shows copy to clipboard button with provided `text` to copy' do
          expect(element(text: 'Hello World!').attr('data-clipboard-text')).to eq('Hello World!')
        end
      end

      context 'when `title` attribute is provided' do
        it 'shows copy to clipboard button with provided `title` as tooltip' do
          expect(element(title: 'Copy to my clipboard!').attr('aria-label')).to eq('Copy to my clipboard!')
        end
      end
    end

    context 'with `hide_tooltip` attribute provided' do
      it 'shows copy to clipboard button without tooltip support' do
        expect(element(hide_tooltip: true).attr('data-placement')).to eq(nil)
        expect(element(hide_tooltip: true).attr('data-toggle')).to eq(nil)
        expect(element(hide_tooltip: true).attr('data-container')).to eq(nil)
      end
    end

    context 'with `hide_button_icon` attribute provided' do
      it 'shows copy to clipboard button without tooltip support' do
        expect(element(hide_button_icon: true).to_html).not_to include sprite_icon('duplicate')
      end
    end

    context 'with `itemprop` attribute provided' do
      it 'shows copy to clipboard button with `itemprop` attribute' do
        expect(element(itemprop: "identifier").attr('itemprop')).to eq("identifier")
      end
    end
  end

  describe '#link_button_to', feature_category: :design_system do
    let(:content) { 'Button content' }
    let(:href) { '#' }
    let(:options) { {} }

    RSpec.shared_examples 'basic behavior' do
      it 'renders a basic link button' do
        expect(subject.name).to eq('a')
        expect(subject.classes).to include(*%w[gl-button btn btn-md btn-default])
        expect(subject.attr('href')).to eq(href)
        expect(subject.content.strip).to eq(content)
      end

      describe 'variant option' do
        let(:options) { { variant: :danger } }

        it 'renders the variant class' do
          expect(subject.classes).to include('btn-danger')
        end
      end

      describe 'category option' do
        let(:options) { { category: :tertiary } }

        it 'renders the category class' do
          expect(subject.classes).to include('btn-default-tertiary')
        end
      end

      describe 'size option' do
        let(:options) { { size: :small } }

        it 'renders the small class' do
          expect(subject.classes).to include('btn-sm')
        end
      end

      describe 'block option' do
        let(:options) { { block: true } }

        it 'renders the block class' do
          expect(subject.classes).to include('btn-block')
        end
      end

      describe 'selected option' do
        let(:options) { { selected: true } }

        it 'renders the selected class' do
          expect(subject.classes).to include('selected')
        end
      end

      describe 'target option' do
        let(:options) { { target: '_blank' } }

        it 'renders the target attribute' do
          expect(subject.attr('target')).to eq('_blank')
        end
      end

      describe 'method option' do
        let(:options) { { method: :post } }

        it 'renders the data-method attribute' do
          expect(subject.attr('data-method')).to eq('post')
        end
      end

      describe 'icon option' do
        let(:options) { { icon: 'remove' } }

        it 'renders the icon' do
          icon = subject.at_css('svg.gl-icon')
          expect(icon.attr('data-testid')).to eq('remove-icon')
        end
      end

      describe 'icon only' do
        let(:content) { nil }
        let(:options) { { icon: 'remove' } }

        it 'renders the icon-only class' do
          expect(subject.classes).to include('btn-icon')
        end
      end

      describe 'arbitrary html options' do
        let(:content) { nil }
        let(:options) { { data: { foo: true }, aria: { labelledby: 'foo' } } }

        it 'renders the attributes' do
          expect(subject.attr('data-foo')).to eq('true')
          expect(subject.attr('aria-labelledby')).to eq('foo')
        end
      end
    end

    describe 'without block' do
      subject do
        tag = helper.link_button_to content, href, options
        Nokogiri::HTML.fragment(tag).first_element_child
      end

      include_examples 'basic behavior'
    end

    describe 'with block' do
      subject do
        tag = helper.link_button_to href, options do
          content
        end
        Nokogiri::HTML.fragment(tag).first_element_child
      end

      include_examples 'basic behavior'
    end
  end
end