summaryrefslogtreecommitdiff
path: root/spec/helpers/badges_helper_spec.rb
blob: 8e1f92305da80d5f53749c3769bb1e24a0070716 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BadgesHelper do
  let(:label) { "Test" }

  describe '#gl_badge_tag' do
    it 'creates a badge with given text' do
      expect(helper.gl_badge_tag(label)).to match(%r{<span .*>Test</span>})
    end

    describe 'block content' do
      it 'renders block content' do
        expect(helper.gl_badge_tag { label }).to match(%r{<span .*>Test</span>})
      end

      it 'changes the function signature' do
        options = { variant: :danger }
        html_options = { class: 'foo-bar' }

        tag = helper.gl_badge_tag(label, options, html_options)
        tag_with_block = helper.gl_badge_tag options, html_options do
          label
        end

        expect(tag).to eql(tag_with_block)
      end
    end

    it 'adds style classes' do
      expect(helper.gl_badge_tag(label)).to match(%r{class="gl-badge badge badge-pill badge-muted md"})
    end

    it 'adds custom classes' do
      expect(helper.gl_badge_tag(label, nil, class: "test-class" )).to match(%r{class=".*test-class.*"})
    end

    describe 'variants' do
      where(:variant) do
        [
          [:muted],
          [:neutral],
          [:info],
          [:success],
          [:warning],
          [:danger]
        ]
      end

      with_them do
        it 'sets the variant class' do
          expected_class = "badge-#{variant}"
          expect(helper.gl_badge_tag(label, variant: variant)).to match(%r{class=".*#{expected_class}.*"})
        end
      end

      it 'defaults to muted' do
        expect(helper.gl_badge_tag(label)).to match(%r{class=".*badge-muted.*"})
      end

      it 'falls back to default given an unknown variant' do
        expect(helper.gl_badge_tag(label, variant: :foo)).to match(%r{class=".*badge-muted.*"})
      end
    end

    describe 'sizes' do
      where(:size) do
        [[:sm], [:md], [:lg]]
      end

      with_them do
        it 'sets the size class' do
          expect(helper.gl_badge_tag(label, size: size)).to match(%r{class=".*#{size}.*"})
        end
      end

      it 'defaults to md' do
        expect(helper.gl_badge_tag(label)).to match(%r{class=".*md.*"})
      end

      it 'falls back to default given an unknown size' do
        expect(helper.gl_badge_tag(label, size: :foo)).to match(%r{class=".*md.*"})
      end
    end

    it 'applies custom html attributes' do
      expect(helper.gl_badge_tag(label, nil, data: { foo: "bar" })).to match(%r{<span .*data-foo="bar".*>})
    end

    describe 'icons' do
      let(:spacing_class_regex) { %r{<svg .*class=".*my-icon-class gl-mr-2".*>.*</svg>} }

      describe 'with text' do
        subject { helper.gl_badge_tag(label, icon: "question-o", icon_classes: 'my-icon-class') }

        it 'renders an icon' do
          expect(subject).to match(%r{<svg .*#question-o".*>.*</svg>})
        end

        it 'adds a spacing class and any custom classes to the icon' do
          expect(subject).to match(spacing_class_regex)
        end
      end

      describe 'icon only' do
        subject { helper.gl_badge_tag(label, icon: 'question-o', icon_only: true) }

        it 'adds an img role to element' do
          expect(subject).to match(%r{<span .*role="img".*>})
        end

        it 'adds aria-label to element' do
          expect(subject).to match(%r{<span .*aria-label="#{label}".*>})
        end

        it 'does not add a spacing class to the icon' do
          expect(subject).not_to match(spacing_class_regex)
        end
      end
    end

    describe 'given an href' do
      it 'creates a badge link' do
        expect(helper.gl_badge_tag(label, nil, href: 'foo')).to match(%r{<a .*href="foo".*>})
      end
    end
  end
end