labels_helper_spec.rb 9.21 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe LabelsHelper do
6 7 8
  describe '#show_label_issuables_link?' do
    shared_examples 'a valid response to show_label_issuables_link?' do |issuables_type, when_enabled = true, when_disabled = false|
      context "when asking for a #{issuables_type} link" do
9
        subject { show_label_issuables_link?(label.present(issuable_subject: nil), issuables_type) }
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

        context "when #{issuables_type} are enabled for the project" do
          let(:project) { create(:project, "#{issuables_type}_access_level": ProjectFeature::ENABLED) }

          it { is_expected.to be(when_enabled) }
        end

        context "when #{issuables_type} are disabled for the project" do
          let(:project) { create(:project, :public, "#{issuables_type}_access_level": ProjectFeature::DISABLED) }

          it { is_expected.to be(when_disabled) }
        end
      end
    end

    context 'with a project label' do
      let(:label) { create(:label, project: project, title: 'bug') }

      context 'when asking for an issue link' do
        it_behaves_like 'a valid response to show_label_issuables_link?', :issues
      end

      context 'when asking for a merge requests link' do
        it_behaves_like 'a valid response to show_label_issuables_link?', :merge_requests
      end
    end

    context 'with a group label' do
38
      let_it_be(:group) { create(:group) }
39 40 41
      let(:label) { create(:group_label, group: group, title: 'bug') }

      context 'when asking for an issue link' do
42
        it_behaves_like 'a valid response to show_label_issuables_link?', :issues, true, true
43 44 45
      end

      context 'when asking for a merge requests link' do
46
        it_behaves_like 'a valid response to show_label_issuables_link?', :merge_requests, true, true
47 48 49 50
      end
    end
  end

Robert Speicher's avatar
Robert Speicher committed
51
  describe 'link_to_label' do
52
    let(:project) { create(:project) }
53
    let(:label) { create(:label, project: project) }
54 55
    let(:subject) { nil }
    let(:label_presenter) { label.present(issuable_subject: subject) }
Robert Speicher's avatar
Robert Speicher committed
56

57 58
    context 'without subject' do
      it "uses the label's project" do
59
        expect(link_to_label(label_presenter)).to match %r{<a.*href="/#{label.project.full_path}/-/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
Robert Speicher's avatar
Robert Speicher committed
60 61 62
      end
    end

63 64
    context 'with a project as subject' do
      let(:namespace) { build(:namespace, name: 'foo3') }
65
      let(:subject) { build(:project, namespace: namespace, name: 'bar3') }
66 67

      it 'links to project issues page' do
68
        expect(link_to_label(label_presenter)).to match %r{<a.*href="/foo3/bar3/-/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
Robert Speicher's avatar
Robert Speicher committed
69 70 71
      end
    end

72
    context 'with a group as subject' do
73
      let(:subject) { build(:group, name: 'bar') }
Robert Speicher's avatar
Robert Speicher committed
74

75
      it 'links to group issues page' do
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
76
        expect(link_to_label(label_presenter)).to match %r{<a.*href="/groups/bar/-/issues\?label_name%5B%5D=#{label.name}".*>.*</a>}m
Robert Speicher's avatar
Robert Speicher committed
77
      end
78
    end
Robert Speicher's avatar
Robert Speicher committed
79

80
    context 'with a type argument' do
81
      ['issue', :issue, 'merge_request', :merge_request].each do |type|
82 83
        context "set to #{type}" do
          it 'links to correct page' do
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
84
            expect(link_to_label(label_presenter, type: type)).to match %r{<a.*href="/#{label.project.full_path}/-/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}".*>.*</a>}m
85 86 87
          end
        end
      end
Robert Speicher's avatar
Robert Speicher committed
88 89
    end

Rémy Coutable's avatar
Rémy Coutable committed
90 91
    context 'with a tooltip argument' do
      context 'set to false' do
92
        it 'does not include the has-tooltip class' do
93
          expect(link_to_label(label_presenter, tooltip: false)).not_to match /has-tooltip/
Rémy Coutable's avatar
Rémy Coutable committed
94 95 96 97
        end
      end
    end

Robert Speicher's avatar
Robert Speicher committed
98 99
    context 'with block' do
      it 'passes the block to link_to' do
100
        link = link_to_label(label_presenter) { 'Foo' }
Robert Speicher's avatar
Robert Speicher committed
101 102 103 104 105 106
        expect(link).to match('Foo')
      end
    end

    context 'without block' do
      it 'uses render_colored_label as the link content' do
107
        expect(self).to receive(:render_colored_label)
108
          .with(label_presenter).and_return('Foo')
109
        expect(link_to_label(label_presenter)).to match('Foo')
Robert Speicher's avatar
Robert Speicher committed
110 111 112 113 114 115 116 117 118 119 120 121
      end
    end
  end

  describe 'text_color_for_bg' do
    it 'uses light text on dark backgrounds' do
      expect(text_color_for_bg('#222E2E')).to eq('#FFFFFF')
    end

    it 'uses dark text on light backgrounds' do
      expect(text_color_for_bg('#EEEEEE')).to eq('#333333')
    end
122 123 124 125 126

    it 'supports RGB triplets' do
      expect(text_color_for_bg('#FFF')).to eq '#333333'
      expect(text_color_for_bg('#000')).to eq '#FFFFFF'
    end
Robert Speicher's avatar
Robert Speicher committed
127
  end
128 129

  describe 'create_label_title' do
130
    let_it_be(:group) { create(:group) }
131 132 133

    context 'with a group as subject' do
      it 'returns "Create group label"' do
134
        expect(create_label_title(group)).to eq _('Create group label')
135 136 137 138
      end
    end

    context 'with a project as subject' do
139
      let_it_be(:project) { create(:project, namespace: group) }
140 141

      it 'returns "Create project label"' do
142
        expect(create_label_title(project)).to eq _('Create project label')
143 144 145 146 147
      end
    end

    context 'with no subject' do
      it 'returns "Create new label"' do
148
        expect(create_label_title(nil)).to eq _('Create new label')
149 150 151 152 153
      end
    end
  end

  describe 'manage_labels_title' do
154
    let_it_be(:group) { create(:group) }
155 156 157

    context 'with a group as subject' do
      it 'returns "Manage group labels"' do
158
        expect(manage_labels_title(group)).to eq _('Manage group labels')
159 160 161 162
      end
    end

    context 'with a project as subject' do
163
      let_it_be(:project) { create(:project, namespace: group) }
164 165

      it 'returns "Manage project labels"' do
166
        expect(manage_labels_title(project)).to eq _('Manage project labels')
167 168 169 170 171
      end
    end

    context 'with no subject' do
      it 'returns "Manage labels"' do
172
        expect(manage_labels_title(nil)).to eq _('Manage labels')
173 174 175 176 177
      end
    end
  end

  describe 'view_labels_title' do
178
    let_it_be(:group) { create(:group) }
179 180 181

    context 'with a group as subject' do
      it 'returns "View group labels"' do
182
        expect(view_labels_title(group)).to eq _('View group labels')
183 184 185 186
      end
    end

    context 'with a project as subject' do
187
      let_it_be(:project) { create(:project, namespace: group) }
188 189

      it 'returns "View project labels"' do
190
        expect(view_labels_title(project)).to eq _('View project labels')
191 192 193 194 195
      end
    end

    context 'with no subject' do
      it 'returns "View labels"' do
196
        expect(view_labels_title(nil)).to eq _('View labels')
197 198 199
      end
    end
  end
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

  describe 'labels_filter_path' do
    let(:group) { create(:group) }
    let(:project) { create(:project) }

    it 'links to the dashboard labels page' do
      expect(labels_filter_path).to eq(dashboard_labels_path)
    end

    it 'links to the group labels page' do
      assign(:group, group)

      expect(helper.labels_filter_path).to eq(group_labels_path(group))
    end

215
    it 'links to the project labels page' do
216 217 218 219 220 221 222 223 224
      assign(:project, project)

      expect(helper.labels_filter_path).to eq(project_labels_path(project))
    end

    it 'supports json format' do
      expect(labels_filter_path(format: :json)).to eq(dashboard_labels_path(format: :json))
    end
  end
225

226 227 228 229 230 231 232 233
  describe 'presented_labels_sorted_by_title' do
    let(:labels) do
      [build(:label, title: 'a'),
       build(:label, title: 'B'),
       build(:label, title: 'c'),
       build(:label, title: 'D')]
    end

234
    it 'sorts labels alphabetically' do
235 236 237 238 239 240 241 242 243
      sorted_ids = presented_labels_sorted_by_title(labels, nil).map(&:id)

      expect(sorted_ids)
        .to match_array([labels[1].id, labels[3].id, labels[0].id, labels[2].id])
    end

    it 'returns an array of label presenters' do
      expect(presented_labels_sorted_by_title(labels, nil))
        .to all(be_a(LabelPresenter))
244 245
    end
  end
246

247 248
  describe '#label_status_tooltip' do
    let(:status) { 'unsubscribed'.inquiry }
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263
    subject { label_status_tooltip(label.present(issuable_subject: nil), status) }

    context 'with a project label' do
      let(:label) { create(:label, title: 'bug') }

      it { is_expected.to eq('Subscribe at project level') }
    end

    context 'with a group label' do
      let(:label) { create(:group_label, title: 'bug') }

      it { is_expected.to eq('Subscribe at group level') }
    end
  end
264 265 266 267 268 269 270 271 272 273

  describe '#label_tooltip_title' do
    let(:html) { '<img src="example.png">This is an image</img>' }
    let(:label_with_html_content) { create(:label, title: 'test', description: html) }

    it 'removes HTML' do
      tooltip = label_tooltip_title(label_with_html_content)
      expect(tooltip).to eq('This is an image')
    end
  end
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

  describe '#show_labels_full_path?' do
    let_it_be(:group) { create(:group) }
    let_it_be(:subgroup) { create(:group, parent: group) }
    let_it_be(:project) { create(:project, group: group) }

    context 'within a project' do
      it 'returns truthy' do
        expect(show_labels_full_path?(project, nil)).to be_truthy
      end
    end

    context 'within a subgroup' do
      it 'returns truthy' do
        expect(show_labels_full_path?(nil, subgroup)).to be_truthy
      end
    end

    context 'within a group' do
      it 'returns falsey' do
        expect(show_labels_full_path?(nil, group)).to be_falsey
      end
    end

    context 'within the admin area' do
      it 'returns falsey' do
        expect(show_labels_full_path?(nil, nil)).to be_falsey
      end
    end
  end
304
end