summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab_danger_spec.rb
blob: ed668c52a0e65e4e844a8ba9eaeeacffdc4a0788 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe GitlabDanger do
  let(:gitlab_danger_helper) { nil }

  subject { described_class.new(gitlab_danger_helper) }

  describe '.local_warning_message' do
    it 'returns an informational message with rules that can run' do
      expect(described_class.local_warning_message).to eq("==> Only the following Danger rules can be run locally: #{described_class::LOCAL_RULES.join(', ')}")
    end
  end

  describe '.success_message' do
    it 'returns an informational success message' do
      expect(described_class.success_message).to eq('==> No Danger rule violations!')
    end
  end

  describe '#rule_names' do
    context 'when running locally' do
      it 'returns local only rules' do
        expect(subject.rule_names).to eq(described_class::LOCAL_RULES)
      end
    end

    context 'when running under CI' do
      let(:gitlab_danger_helper) { double('danger_gitlab_helper') }

      it 'returns all rules' do
        expect(subject.rule_names).to eq(described_class::LOCAL_RULES | described_class::CI_ONLY_RULES)
      end
    end
  end

  describe '#html_link' do
    context 'when running locally' do
      it 'returns the same string' do
        str = 'something'

        expect(subject.html_link(str)).to eq(str)
      end
    end

    context 'when running under CI' do
      let(:gitlab_danger_helper) { double('danger_gitlab_helper') }

      it 'returns a HTML link formatted version of the string' do
        str = 'something'
        html_formatted_str = %Q{<a href="#{str}">#{str}</a>}

        expect(gitlab_danger_helper).to receive(:html_link).with(str).and_return(html_formatted_str)

        expect(subject.html_link(str)).to eq(html_formatted_str)
      end
    end
  end

  describe '#ci?' do
    context 'when gitlab_danger_helper is not available' do
      it 'returns false' do
        expect(subject.ci?).to be_falsey
      end
    end

    context 'when gitlab_danger_helper is available' do
      let(:gitlab_danger_helper) { double('danger_gitlab_helper') }

      it 'returns true' do
        expect(subject.ci?).to be_truthy
      end
    end
  end
end