report_spec.rb 1.88 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe Gitlab::Badge::Coverage::Report do
6 7 8 9 10 11 12 13 14
  let_it_be(:project)  { create(:project) }
  let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }

  let_it_be(:builds) do
    [
      create(:ci_build, :success, pipeline: pipeline, coverage: 40, name: 'first'),
      create(:ci_build, :success, pipeline: pipeline, coverage: 60)
    ]
  end
15

16
  let(:badge) do
17
    described_class.new(project, 'master', opts: { job: job_name })
18 19 20 21
  end

  let(:job_name) { nil }

22 23 24 25 26 27
  describe '#entity' do
    it 'describes a coverage' do
      expect(badge.entity).to eq 'coverage'
    end
  end

28 29 30 31 32 33 34 35 36 37 38 39
  describe '#metadata' do
    it 'returns correct metadata' do
      expect(badge.metadata.image_url).to include 'coverage.svg'
    end
  end

  describe '#template' do
    it 'returns correct template' do
      expect(badge.template.key_text).to eq 'coverage'
    end
  end

40 41 42 43
  describe '#status' do
    before do
      allow(badge).to receive(:pipeline).and_return(pipeline)
    end
44

45
    context 'with no pipeline' do
46 47
      let(:pipeline) { nil }

48 49
      it 'returns coverage from that job' do
        expect(badge.status).to eq(40)
50 51 52
      end
    end

53 54
    context 'with no job specified' do
      it 'returns the pipeline coverage value' do
55
        expect(badge.status).to eq(50.00)
56 57
      end
    end
58

59 60 61 62 63 64 65 66
    context 'with a blank job name' do
      let(:job_name) { ' ' }

      it 'returns the pipeline coverage value' do
        expect(badge.status).to eq(50.00)
      end
    end

67 68
    context 'with an unmatching job name specified' do
      let(:job_name) { 'incorrect name' }
69

70
      it 'returns nil' do
71
        expect(badge.status).to be_nil
72
      end
73
    end
74

75 76
    context 'with a matching job name specified' do
      let(:job_name) { 'first' }
77

78
      it 'returns the pipeline coverage value' do
79 80 81
        expect(badge.status).to eq(40.00)
      end
    end
82 83
  end
end