Welcome to mirror list, hosted at ThFree Co, Russian Federation.

target_service_spec.rb « quick_actions « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1b0a5d4ae730528bb23b07a4049f203d7de4bb78 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe QuickActions::TargetService do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:service) { described_class.new(project, user) }

  before do
    project.add_maintainer(user)
  end

  describe '#execute' do
    shared_examples 'no target' do |type_iid:|
      it 'returns nil' do
        target = service.execute(type, type_iid)

        expect(target).to be_nil
      end
    end

    shared_examples 'find target' do
      it 'returns the target' do
        found_target = service.execute(type, target_iid)

        expect(found_target).to eq(target)
      end
    end

    shared_examples 'build target' do |type_iid:|
      it 'builds a new target' do
        target = service.execute(type, type_iid)

        expect(target.project).to eq(project)
        expect(target).to be_new_record
      end
    end

    context 'for issue' do
      let(:target) { create(:issue, project: project) }
      let(:target_iid) { target.iid }
      let(:type) { 'Issue' }

      it_behaves_like 'find target'
      it_behaves_like 'build target', type_iid: nil
      it_behaves_like 'build target', type_iid: -1
    end

    context 'for work item' do
      let(:target) { create(:work_item, :task, project: project) }
      let(:target_iid) { target.iid }
      let(:type) { 'WorkItem' }

      it_behaves_like 'find target'
    end

    context 'for merge request' do
      let(:target) { create(:merge_request, source_project: project) }
      let(:target_iid) { target.iid }
      let(:type) { 'MergeRequest' }

      it_behaves_like 'find target'
      it_behaves_like 'build target', type_iid: nil
      it_behaves_like 'build target', type_iid: -1
    end

    context 'for commit' do
      let(:project) { create(:project, :repository) }
      let(:target) { project.commit.parent }
      let(:target_iid) { target.sha }
      let(:type) { 'Commit' }

      it_behaves_like 'find target'
      it_behaves_like 'no target', type_iid: 'invalid_sha'

      context 'with nil target_iid' do
        let(:target) { project.commit }
        let(:target_iid) { nil }

        it_behaves_like 'find target'
      end
    end

    context 'for unknown type' do
      let(:type) { 'unknown' }

      it_behaves_like 'no target', type_iid: :unused
    end
  end
end