summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/badge/build_spec.rb
blob: 329792bb68542657809239fc031d53f7812172c7 (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
require 'spec_helper'

describe Gitlab::Badge::Build do
  let(:project) { create(:project) }
  let(:sha) { project.commit.sha }
  let(:branch) { 'master' }
  let(:badge) { described_class.new(project, branch) }

  describe '#type' do
    subject { badge.type }
    it { is_expected.to eq 'image/svg+xml' }
  end

  describe '#to_html' do
    let(:html) { Nokogiri::HTML.parse(badge.to_html) }
    let(:a_href) { html.at('a') }

    it 'points to link' do
      expect(a_href[:href]).to eq badge.link_url
    end

    it 'contains clickable image' do
      expect(a_href.children.first.name).to eq 'img'
    end
  end

  describe '#to_markdown' do
    subject { badge.to_markdown }

    it { is_expected.to include badge.image_url }
    it { is_expected.to include badge.link_url }
  end

  describe '#image_url' do
    subject { badge.image_url }
    it { is_expected.to include "badges/#{branch}/build.svg" }
  end

  describe '#link_url' do
    subject { badge.link_url }
    it { is_expected.to include "commits/#{branch}" }
  end

  context 'build exists' do
    let(:ci_commit) { create(:ci_commit, project: project, sha: sha) }
    let!(:build) { create(:ci_build, commit: ci_commit) }


    context 'build success' do
      before { build.success! }

      describe '#to_s' do
        subject { badge.to_s }
        it { is_expected.to eq 'build-success' }
      end

      describe '#data' do
        let(:data) { badge.data }

        it 'contains infromation about success' do
          expect(status_node(data, 'success')).to be_truthy
        end
      end
    end

    context 'build failed' do
      before { build.drop! }

      describe '#to_s' do
        subject { badge.to_s }
        it { is_expected.to eq 'build-failed' }
      end

      describe '#data' do
        let(:data) { badge.data }

        it 'contains infromation about failure' do
          expect(status_node(data, 'failed')).to be_truthy
        end
      end
    end
  end

  context 'build does not exist' do
    describe '#to_s' do
      subject { badge.to_s }
      it { is_expected.to eq 'build-unknown' }
    end

    describe '#data' do
      let(:data) { badge.data }

      it 'contains infromation about unknown build' do
        expect(status_node(data, 'unknown')).to be_truthy
      end
    end
  end

  def status_node(data, status)
    xml = Nokogiri::XML.parse(data)
    xml.at(%Q{text:contains("#{status}")})
  end
end