summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/graphql/mutations/boards_create_shared_examples.rb
blob: 2e3a3ce6b41fc0a69eeeb791a385b2ad2ce7f9f6 (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
# frozen_string_literal: true

RSpec.shared_examples 'boards create mutation' do
  include GraphqlHelpers

  let_it_be(:current_user, reload: true) { create(:user) }
  let(:name) { 'board name' }
  let(:mutation) { graphql_mutation(:create_board, params) }

  subject { post_graphql_mutation(mutation, current_user: current_user) }

  def mutation_response
    graphql_mutation_response(:create_board)
  end

  context 'when the user does not have permission' do
    it_behaves_like 'a mutation that returns a top-level access error'

    it 'does not create the board' do
      expect { subject }.not_to change { Board.count }
    end
  end

  context 'when the user has permission' do
    before do
      parent.add_maintainer(current_user)
    end

    context 'when the parent (project_path or group_path) param is given' do
      context 'when everything is ok' do
        it 'creates the board' do
          expect { subject }.to change { Board.count }.from(0).to(1)
        end

        it 'returns the created board' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response).to have_key('board')
          expect(mutation_response['board']['name']).to eq(name)
        end
      end

      context 'when hide_backlog_list parameter is true' do
        before do
          params[:hide_backlog_list] = true
        end

        it 'returns the board with correct hide_backlog_list field' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['board']['hideBacklogList']).to eq(true)
        end
      end

      context 'when hide_closed_list parameter is true' do
        before do
          params[:hide_closed_list] = true
        end

        it 'returns the board with correct hide_closed_list field' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response['board']['hideClosedList']).to eq(true)
        end
      end

      context 'when the Boards::CreateService returns an error response' do
        before do
          params[:name] = ''
        end

        it 'does not create a board' do
          expect { subject }.not_to change { Board.count }
        end

        it 'returns an error' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(mutation_response).to have_key('board')
          expect(mutation_response['board']).to be_nil
          expect(mutation_response['errors'].first).to eq('There was an error when creating a board.')
        end
      end
    end

    context 'when neither project_path nor group_path param is given' do
      let(:params) { { name: name } }

      it_behaves_like 'a mutation that returns top-level errors',
        errors: ['Exactly one of group_path or project_path arguments is required']

      it 'does not create the board' do
        expect { subject }.not_to change { Board.count }
      end
    end
  end
end