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

create_spec.rb « merge_requests « mutations « graphql « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1edb60e4ff96ad3610161f1b4f809d1cbdf88a9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::MergeRequests::Create do
  include GraphqlHelpers

  subject(:mutation) { described_class.new(object: nil, context: context, field: nil) }

  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:user) { create(:user) }

  let(:context) do
    GraphQL::Query::Context.new(
      query: query_double(schema: nil),
      values: { current_user: user },
      object: nil
    )
  end

  describe '#resolve' do
    subject do
      mutation.resolve(
        project_path: project.full_path,
        title: title,
        source_branch: source_branch,
        target_branch: target_branch,
        description: description,
        labels: labels
      )
    end

    let(:title) { 'MergeRequest' }
    let(:source_branch) { 'feature' }
    let(:target_branch) { 'master' }
    let(:description) { nil }
    let(:labels) { nil }

    let(:mutated_merge_request) { subject[:merge_request] }

    it 'raises an error if the resource is not accessible to the user' do
      expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end

    context 'when user does not have enough permissions to create a merge request' do
      before do
        project.add_guest(user)
      end

      it 'raises an error if the resource is not accessible to the user' do
        expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end

    context 'when the user can create a merge request' do
      before_all do
        project.add_developer(user)
      end

      it 'creates a new merge request' do
        expect { mutated_merge_request }.to change(MergeRequest, :count).by(1)
      end

      it 'returns a new merge request' do
        expect(mutated_merge_request.title).to eq(title)
        expect(subject[:errors]).to be_empty
      end

      context 'when optional description field is set' do
        let(:description) { 'content' }

        it 'returns a new merge request with a description' do
          expect(mutated_merge_request.description).to eq(description)
          expect(subject[:errors]).to be_empty
        end
      end

      context 'when optional labels field is set' do
        let(:labels) { %w[label-1 label-2] }

        it 'returns a new merge request with labels' do
          expect(mutated_merge_request.labels.map(&:title)).to eq(labels)
          expect(subject[:errors]).to be_empty
        end
      end

      context 'when service cannot create a merge request' do
        let(:title) { nil }

        it 'does not create a new merge request' do
          expect { mutated_merge_request }.not_to change(MergeRequest, :count)
        end

        it 'returns errors' do
          expect(mutated_merge_request).to be_nil
          expect(subject[:errors]).to eq(['Title can\'t be blank'])
        end
      end
    end
  end
end