summaryrefslogtreecommitdiff
path: root/spec/models/protectable_dropdown_spec.rb
blob: a256f9e0ab16cab79b7c3b22e96510618fdd31c8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ProtectableDropdown do
  subject(:dropdown) { described_class.new(project, ref_type) }

  let(:project) { create(:project, :repository) }

  describe 'initialize' do
    it 'raises ArgumentError for invalid ref type' do
      expect { described_class.new(double, :foo) }
        .to raise_error(ArgumentError, "invalid ref type `foo`")
    end
  end

  shared_examples 'protectable_ref_names' do
    context 'when project repository is not empty' do
      it 'includes elements matching a protected ref wildcard' do
        is_expected.to include(matching_ref)

        factory = ref_type == :branches ? :protected_branch : :protected_tag

        create(factory, name: "#{matching_ref[0]}*", project: project)

        subject = described_class.new(project.reload, ref_type)

        expect(subject.protectable_ref_names).to include(matching_ref)
      end
    end

    context 'when project repository is empty' do
      let(:project) { create(:project) }

      it 'returns empty list' do
        is_expected.to be_empty
      end
    end
  end

  describe '#protectable_ref_names' do
    subject { dropdown.protectable_ref_names }

    context 'for branches' do
      let(:ref_type) { :branches }
      let(:matching_ref) { 'feature' }

      before do
        create(:protected_branch, project: project, name: 'master')
      end

      it { is_expected.to include(matching_ref) }
      it { is_expected.not_to include('master') }

      it_behaves_like 'protectable_ref_names'
    end

    context 'for tags' do
      let(:ref_type) { :tags }
      let(:matching_ref) { 'v1.0.0' }

      before do
        create(:protected_tag, project: project, name: 'v1.1.0')
      end

      it { is_expected.to include(matching_ref) }
      it { is_expected.not_to include('v1.1.0') }

      it_behaves_like 'protectable_ref_names'
    end
  end

  describe '#hash' do
    subject { dropdown.hash }

    context 'for branches' do
      let(:ref_type) { :branches }

      it { is_expected.to include(id: 'feature', text: 'feature', title: 'feature') }
    end

    context 'for tags' do
      let(:ref_type) { :tags }

      it { is_expected.to include(id: 'v1.0.0', text: 'v1.0.0', title: 'v1.0.0') }
    end
  end
end