summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/branches/create_spec.rb
blob: 744f8f1f2bc14f95046c9be51afdd9b19aa29a40 (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
# frozen_string_literal: true

require 'spec_helper'

describe Mutations::Branches::Create do
  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_it_be(:context) do
    GraphQL::Query::Context.new(
      query: OpenStruct.new(schema: nil),
      values: { current_user: user },
      object: nil
    )
  end

  describe '#resolve' do
    subject { mutation.resolve(project_path: project.full_path, name: branch, ref: ref) }

    let(:branch) { 'new_branch' }
    let(:ref) { 'master' }
    let(:mutated_branch) { subject[:branch] }

    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 the user can create a branch' do
      before do
        project.add_developer(user)

        allow_next_instance_of(::Branches::CreateService, project, user) do |create_service|
          allow(create_service).to receive(:execute).with(branch, ref) { service_result }
        end
      end

      context 'when service successfully creates a new branch' do
        let(:service_result) { { status: :success, branch: double(name: branch) } }

        it 'returns a new branch' do
          expect(mutated_branch.name).to eq(branch)
          expect(subject[:errors]).to be_empty
        end
      end

      context 'when service fails to create a new branch' do
        let(:service_result) { { status: :error, message: 'Branch already exists' } }

        it { expect(mutated_branch).to be_nil }
        it { expect(subject[:errors]).to eq(['Branch already exists']) }
      end
    end
  end
end