summaryrefslogtreecommitdiff
path: root/spec/helpers/labels_helper_spec.rb
blob: 012678db9c2520001e279243073b497ef6b5fa0c (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
require 'spec_helper'

describe LabelsHelper do
  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|
      let(:context_project) { project }

      context "when asking for a #{issuables_type} link" do
        subject { show_label_issuables_link?(label, issuables_type, project: context_project) }

        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
      set(:group) { create(:group) }
      let(:label) { create(:group_label, group: group, title: 'bug') }

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

        context 'in the context of a group' do
          let(:context_project) { nil }

          it_behaves_like 'a valid response to show_label_issuables_link?', :issues, true, true
        end
      end

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

        context 'in the context of a group' do
          let(:context_project) { nil }

          it_behaves_like 'a valid response to show_label_issuables_link?', :merge_requests, true, true
        end
      end
    end
  end

  describe 'link_to_label' do
    let(:project) { create(:project) }
    let(:label) { create(:label, project: project) }

    context 'without subject' do
      it "uses the label's project" do
        expect(link_to_label(label)).to match %r{<a href="/#{label.project.full_path}/issues\?label_name%5B%5D=#{label.name}">.*</a>}
      end
    end

    context 'with a project as subject' do
      let(:namespace) { build(:namespace, name: 'foo3') }
      let(:another_project) { build(:project, namespace: namespace, name: 'bar3') }

      it 'links to project issues page' do
        expect(link_to_label(label, subject: another_project)).to match %r{<a href="/foo3/bar3/issues\?label_name%5B%5D=#{label.name}">.*</a>}
      end
    end

    context 'with a group as subject' do
      let(:group) { build(:group, name: 'bar') }

      it 'links to group issues page' do
        expect(link_to_label(label, subject: group)).to match %r{<a href="/groups/bar/-/issues\?label_name%5B%5D=#{label.name}">.*</a>}
      end
    end

    context 'with a type argument' do
      ['issue', :issue, 'merge_request', :merge_request].each do |type|
        context "set to #{type}" do
          it 'links to correct page' do
            expect(link_to_label(label, type: type)).to match %r{<a href="/#{label.project.full_path}/#{type.to_s.pluralize}\?label_name%5B%5D=#{label.name}">.*</a>}
          end
        end
      end
    end

    context 'with a tooltip argument' do
      context 'set to false' do
        it 'does not include the has-tooltip class' do
          expect(link_to_label(label, tooltip: false)).not_to match /has-tooltip/
        end
      end
    end

    context 'with block' do
      it 'passes the block to link_to' do
        link = link_to_label(label) { 'Foo' }
        expect(link).to match('Foo')
      end
    end

    context 'without block' do
      it 'uses render_colored_label as the link content' do
        expect(self).to receive(:render_colored_label)
          .with(label, tooltip: true).and_return('Foo')
        expect(link_to_label(label)).to match('Foo')
      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

    it 'supports RGB triplets' do
      expect(text_color_for_bg('#FFF')).to eq '#333333'
      expect(text_color_for_bg('#000')).to eq '#FFFFFF'
    end
  end

  describe 'create_label_title' do
    set(:group) { create(:group) }

    context 'with a group as subject' do
      it 'returns "Create group label"' do
        expect(create_label_title(group)).to eq 'Create group label'
      end
    end

    context 'with a project as subject' do
      set(:project) { create(:project, namespace: group) }

      it 'returns "Create project label"' do
        expect(create_label_title(project)).to eq 'Create project label'
      end
    end

    context 'with no subject' do
      it 'returns "Create new label"' do
        expect(create_label_title(nil)).to eq 'Create new label'
      end
    end
  end

  describe 'manage_labels_title' do
    set(:group) { create(:group) }

    context 'with a group as subject' do
      it 'returns "Manage group labels"' do
        expect(manage_labels_title(group)).to eq 'Manage group labels'
      end
    end

    context 'with a project as subject' do
      set(:project) { create(:project, namespace: group) }

      it 'returns "Manage project labels"' do
        expect(manage_labels_title(project)).to eq 'Manage project labels'
      end
    end

    context 'with no subject' do
      it 'returns "Manage labels"' do
        expect(manage_labels_title(nil)).to eq 'Manage labels'
      end
    end
  end

  describe 'view_labels_title' do
    set(:group) { create(:group) }

    context 'with a group as subject' do
      it 'returns "View group labels"' do
        expect(view_labels_title(group)).to eq 'View group labels'
      end
    end

    context 'with a project as subject' do
      set(:project) { create(:project, namespace: group) }

      it 'returns "View project labels"' do
        expect(view_labels_title(project)).to eq 'View project labels'
      end
    end

    context 'with no subject' do
      it 'returns "View labels"' do
        expect(view_labels_title(nil)).to eq 'View labels'
      end
    end
  end

  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

    it 'links to the project labels page' do
      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

  describe 'labels_sorted_by_title' do
    it 'sorts labels alphabetically' do
      label1 = double(:label, title: 'a')
      label2 = double(:label, title: 'B')
      label3 = double(:label, title: 'c')
      label4 = double(:label, title: 'D')
      labels = [label1, label2, label3, label4]

      expect(labels_sorted_by_title(labels))
        .to match_array([label2, label4, label1, label3])
    end
  end
end