Commit 7535be32 authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'mo-refactor-badge-template' into 'master'

Refactor badge template

See merge request gitlab-org/gitlab!63224
parents 9fcfe629 11017bf8
......@@ -24,26 +24,10 @@ module Gitlab::Ci
@key_width = badge.customization.dig(:key_width)
end
def key_text
if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end
def value_text
@status ? ("%.2f%%" % @status) : 'unknown'
end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
62
end
end
def value_width
@status ? 54 : 58
end
......
......@@ -28,26 +28,10 @@ module Gitlab::Ci
@key_width = badge.customization.dig(:key_width)
end
def key_text
if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end
def value_text
STATUS_RENAME[@status.to_s] || @status.to_s
end
def key_width
if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
62
end
end
def value_width
54
end
......
......@@ -8,6 +8,7 @@ module Gitlab::Ci
class Template
MAX_KEY_TEXT_SIZE = 64
MAX_KEY_WIDTH = 512
DEFAULT_KEY_WIDTH = 62
def initialize(badge)
@entity = badge.entity
......@@ -15,7 +16,11 @@ module Gitlab::Ci
end
def key_text
raise NotImplementedError
if @key_text && @key_text.size <= MAX_KEY_TEXT_SIZE
@key_text
else
@entity.to_s
end
end
def value_text
......@@ -23,7 +28,11 @@ module Gitlab::Ci
end
def key_width
raise NotImplementedError
if @key_width && @key_width.between?(1, MAX_KEY_WIDTH)
@key_width
else
DEFAULT_KEY_WIDTH
end
end
def value_width
......
......@@ -6,31 +6,7 @@ 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
it_behaves_like 'a badge template', 'coverage'
describe '#value_text' do
context 'when coverage is known' do
......@@ -60,32 +36,6 @@ RSpec.describe Gitlab::Ci::Badge::Coverage::Template do
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
......
......@@ -6,31 +6,7 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do
let(:badge) { double(entity: 'pipeline', status: 'success', customization: {}) }
let(:template) { described_class.new(badge) }
describe '#key_text' do
it 'says pipeline by default' do
expect(template.key_text).to eq 'pipeline'
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 'pipeline'
end
end
end
end
it_behaves_like 'a badge template', 'pipeline'
describe '#value_text' do
it 'is status value' do
......@@ -38,32 +14,6 @@ RSpec.describe Gitlab::Ci::Badge::Pipeline::Template do
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 'widths and text anchors' do
it 'has fixed width and text anchors' do
expect(template.width).to eq 116
......
# frozen_string_literal: true
RSpec.shared_examples 'a badge template' do |badge_type|
describe '#key_text' do
it "says #{badge_type} by default" do
expect(template.key_text).to eq(badge_type)
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' * (::Gitlab::Ci::Badge::Template::MAX_KEY_TEXT_SIZE + 1) } )
end
it 'returns default value' do
expect(template.key_text).to eq(badge_type)
end
end
end
end
describe '#key_width' do
let_it_be(:default_key_width) { ::Gitlab::Ci::Badge::Template::DEFAULT_KEY_WIDTH }
it 'is fixed by default' do
expect(template.key_width).to eq(default_key_width)
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: ::Gitlab::Ci::Badge::Template::MAX_KEY_WIDTH + 1 })
end
it 'returns default value' do
expect(template.key_width).to eq(default_key_width)
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment