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

require 'spec_helper'

RSpec.describe Gitlab::Badge::Coverage::Report do
  let_it_be(:project) { create(:project) }
  let_it_be(:success_pipeline) { create(:ci_pipeline, :success, project: project) }
  let_it_be(:running_pipeline) { create(:ci_pipeline, :running, project: project) }
  let_it_be(:failure_pipeline) { create(:ci_pipeline, :failed, project: project) }

  let_it_be(:builds) do
    [
      create(:ci_build, :success, pipeline: success_pipeline, coverage: 40, created_at: 9.seconds.ago, name: 'coverage'),
      create(:ci_build, :success, pipeline: success_pipeline, coverage: 60, created_at: 8.seconds.ago)
    ]
  end

  let(:badge) do
    described_class.new(project, 'master', opts: { job: job_name })
  end

  let(:job_name) { nil }

  describe '#entity' do
    it 'describes a coverage' do
      expect(badge.entity).to eq 'coverage'
    end
  end

  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

  describe '#status' do
    context 'with no job specified' do
      it 'returns the most recent successful pipeline coverage value' do
        expect(badge.status).to eq(50.00)
      end

      context 'and no successful pipelines' do
        before do
          allow(badge).to receive(:successful_pipeline).and_return(nil)
        end

        it 'returns nil' do
          expect(badge.status).to eq(nil)
        end
      end
    end

    context 'with a blank job name' do
      let(:job_name) { ' ' }

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

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

      it 'returns nil' do
        expect(badge.status).to be_nil
      end
    end

    context 'with a matching job name specified' do
      let(:job_name) { 'coverage' }

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

      context 'with a more recent running pipeline' do
        let!(:another_build) { create(:ci_build, :success, pipeline: running_pipeline, coverage: 20, created_at: 7.seconds.ago, name: 'coverage') }

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

      context 'with a more recent failed pipeline' do
        let!(:another_build) { create(:ci_build, :success, pipeline: failure_pipeline, coverage: 10, created_at: 6.seconds.ago, name: 'coverage') }

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