summaryrefslogtreecommitdiff
path: root/spec/models/design_management/design_action_spec.rb
blob: 958b1dd9124ce00adce09794739d38f0a021c243 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe DesignManagement::DesignAction do
  describe 'validations' do
    describe 'the design' do
      let(:fail_validation) { raise_error(/design/i) }

      it 'must not be nil' do
        expect { described_class.new(nil, :create, :foo) }.to fail_validation
      end
    end

    describe 'the action' do
      let(:fail_validation) { raise_error(/action/i) }

      it 'must not be nil' do
        expect { described_class.new(double, nil, :foo) }.to fail_validation
      end

      it 'must be a known action' do
        expect { described_class.new(double, :wibble, :foo) }.to fail_validation
      end
    end

    describe 'the content' do
      context 'content is necesary' do
        let(:fail_validation) { raise_error(/needs content/i) }

        %i[create update].each do |action|
          it "must not be nil if the action is #{action}" do
            expect { described_class.new(double, action, nil) }.to fail_validation
          end
        end
      end

      context 'content is forbidden' do
        let(:fail_validation) { raise_error(/forbids content/i) }

        it "must not be nil if the action is delete" do
          expect { described_class.new(double, :delete, :foo) }.to fail_validation
        end
      end
    end
  end

  describe '#gitaly_action' do
    let(:path) { 'some/path/somewhere' }
    let(:design) { OpenStruct.new(full_path: path) }

    subject { described_class.new(design, action, content) }

    context 'the action needs content' do
      let(:action) { :create }
      let(:content) { :foo }

      it 'produces a good gitaly action' do
        expect(subject.gitaly_action).to eq(
          action: action,
          file_path: path,
          content: content
        )
      end
    end

    context 'the action forbids content' do
      let(:action) { :delete }
      let(:content) { nil }

      it 'produces a good gitaly action' do
        expect(subject.gitaly_action).to eq(action: action, file_path: path)
      end
    end
  end

  describe '#issue_id' do
    let(:issue_id) { :foo }
    let(:design) { OpenStruct.new(issue_id: issue_id) }

    subject { described_class.new(design, :delete) }

    it 'delegates to the design' do
      expect(subject.issue_id).to eq(issue_id)
    end
  end

  describe '#performed' do
    let(:design) { double }

    subject { described_class.new(design, :delete) }

    it 'calls design#clear_version_cache when the action has been performed' do
      expect(design).to receive(:clear_version_cache)

      subject.performed
    end
  end
end