2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
RSpec.describe Gitlab::Ci::Badge::Coverage::Template do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:badge) { double(entity: 'coverage', status: 90.00, customization: {}) }
|
2016-09-13 17:45:13 +05:30
|
|
|
let(:template) { described_class.new(badge) }
|
|
|
|
|
|
|
|
describe '#key_text' do
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'says coverage by default' do
|
2016-09-13 17:45:13 +05:30
|
|
|
expect(template.key_text).to eq 'coverage'
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
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
|
2020-11-24 15:15:51 +05:30
|
|
|
allow(badge).to receive(:customization).and_return({ key_text: 't' * 65 })
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns default value' do
|
|
|
|
expect(template.key_text).to eq 'coverage'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#value_text' do
|
|
|
|
context 'when coverage is known' do
|
|
|
|
it 'returns coverage percentage' do
|
2018-03-17 18:26:18 +05:30
|
|
|
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%'
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'is fixed by default' do
|
2016-09-13 17:45:13 +05:30
|
|
|
expect(template.key_width).to eq 62
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
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
|
2020-11-24 15:15:51 +05:30
|
|
|
allow(badge).to receive(:customization).and_return({ key_width: 513 })
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns default value' do
|
|
|
|
expect(template.key_width).to eq 62
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#value_width' do
|
|
|
|
context 'when coverage is known' do
|
|
|
|
it 'is narrower when coverage is known' do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(template.value_width).to eq 54
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(template.width).to eq 116
|
2016-09-13 17:45:13 +05:30
|
|
|
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
|