summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/badge/coverage/template_spec.rb
blob: f010d1bce50dfd5eba7d2206a1e6709359e7f316 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Badge::Coverage::Template do
  let(:badge) { double(entity: 'coverage', status: 90.00, customization: {}) }
  let(:template) { described_class.new(badge) }

  describe '#key_text' do
    it 'says coverage by default' do
      expect(template.key_text).to eq 'coverage'
    end

    context 'when custom key_text is defined' do
      before do
        allow(badge).to receive(:customization).and_return({ key_text: "custom text" })
      end

      it 'returns custom value' do
        expect(template.key_text).to eq "custom text"
      end

      context 'when its size is larger than the max allowed value' do
        before do
          allow(badge).to receive(:customization).and_return({ key_text: 't' * 65 })
        end

        it 'returns default value' do
          expect(template.key_text).to eq 'coverage'
        end
      end
    end
  end

  describe '#value_text' do
    context 'when coverage is known' do
      it 'returns coverage percentage' do
        expect(template.value_text).to eq '90.00%'
      end
    end

    context 'when coverage is known to many digits' do
      before do
        allow(badge).to receive(:status).and_return(92.349)
      end

      it 'returns rounded coverage percentage' do
        expect(template.value_text).to eq '92.35%'
      end
    end

    context 'when coverage is unknown' do
      before do
        allow(badge).to receive(:status).and_return(nil)
      end

      it 'returns string that says coverage is unknown' do
        expect(template.value_text).to eq 'unknown'
      end
    end
  end

  describe '#key_width' do
    it 'is fixed by default' do
      expect(template.key_width).to eq 62
    end

    context 'when custom key_width is defined' do
      before do
        allow(badge).to receive(:customization).and_return({ key_width: 101 })
      end

      it 'returns custom value' do
        expect(template.key_width).to eq 101
      end

      context 'when it is larger than the max allowed value' do
        before do
          allow(badge).to receive(:customization).and_return({ key_width: 513 })
        end

        it 'returns default value' do
          expect(template.key_width).to eq 62
        end
      end
    end
  end

  describe '#value_width' do
    context 'when coverage is known' do
      it 'is narrower when coverage is known' do
        expect(template.value_width).to eq 54
      end
    end

    context 'when coverage is unknown' do
      before do
        allow(badge).to receive(:status).and_return(nil)
      end

      it 'is wider when coverage is unknown to fit text' do
        expect(template.value_width).to eq 58
      end
    end
  end

  describe '#key_color' do
    it 'always has the same color' do
      expect(template.key_color).to eq '#555'
    end
  end

  describe '#value_color' do
    context 'when coverage is good' do
      before do
        allow(badge).to receive(:status).and_return(98)
      end

      it 'is green' do
        expect(template.value_color).to eq '#4c1'
      end
    end

    context 'when coverage is acceptable' do
      before do
        allow(badge).to receive(:status).and_return(90)
      end

      it 'is green-orange' do
        expect(template.value_color).to eq '#a3c51c'
      end
    end

    context 'when coverage is medium' do
      before do
        allow(badge).to receive(:status).and_return(75)
      end

      it 'is orange-yellow' do
        expect(template.value_color).to eq '#dfb317'
      end
    end

    context 'when coverage is low' do
      before do
        allow(badge).to receive(:status).and_return(50)
      end

      it 'is red' do
        expect(template.value_color).to eq '#e05d44'
      end
    end

    context 'when coverage is unknown' do
      before do
        allow(badge).to receive(:status).and_return(nil)
      end

      it 'is grey' do
        expect(template.value_color).to eq '#9f9f9f'
      end
    end
  end

  describe '#width' do
    context 'when coverage is known' do
      it 'returns the key width plus value width' do
        expect(template.width).to eq 116
      end
    end

    context 'when coverage is unknown' do
      before do
        allow(badge).to receive(:status).and_return(nil)
      end

      it 'returns key width plus wider value width' do
        expect(template.width).to eq 120
      end
    end
  end
end