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

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

require 'spec_helper'

RSpec.describe ListboxHelper do
  subject do
    tag = helper.gl_redirect_listbox_tag(items, selected, html_options)
    Nokogiri::HTML.fragment(tag).children.first
  end

  before do
    allow(helper).to receive(:sprite_icon).with(
      'chevron-down',
      css_class: 'gl-button-icon dropdown-chevron gl-icon'
    ).and_return('<span class="icon"></span>'.html_safe)
  end

  let(:selected) { 'bar' }
  let(:html_options) { {} }
  let(:items) do
    [
      { value: 'foo', text: 'Foo' },
      { value: 'bar', text: 'Bar' }
    ]
  end

  describe '#gl_redirect_listbox_tag' do
    it 'creates root element with expected classes' do
      expect(subject.classes).to include(
        *%w[
          dropdown
          b-dropdown
          gl-dropdown
          btn-group
          js-redirect-listbox
        ])
    end

    it 'sets data attributes for items and selected' do
      expect(subject.attributes['data-items'].value).to eq(items.to_json)
      expect(subject.attributes['data-selected'].value).to eq(selected)
    end

    it 'adds styled button' do
      expect(subject.at_css('button').classes).to include(
        *%w[
          btn
          dropdown-toggle
          btn-default
          btn-md
          gl-button
          gl-dropdown-toggle
        ])
    end

    it 'sets button text to selected item' do
      expect(subject.at_css('button').content).to eq('Bar')
    end

    context 'given html_options' do
      let(:html_options) { { class: 'test-class', data: { qux: 'qux' } } }

      it 'applies them to the root element' do
        expect(subject.attributes['data-qux'].value).to eq('qux')
        expect(subject.classes).to include('test-class')
      end
    end

    context 'when selected does not match any item' do
      where(selected: [nil, 'qux'])

      with_them do
        it 'selects first item' do
          expect(subject.at_css('button').content).to eq('Foo')
          expect(subject.attributes['data-selected'].value).to eq('foo')
        end
      end
    end
  end
end