summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/graphql/mutations/work_items/update_description_widget_shared_examples.rb
blob: 2ec48aa405b91d119d83b5fa0af0489bb32261d6 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

RSpec.shared_examples 'update work item description widget' do
  it 'updates the description widget' do
    expect do
      post_graphql_mutation(mutation, current_user: current_user)
      work_item.reload
    end.to change { work_item.description }.from(nil).to(new_description)

    expect(response).to have_gitlab_http_status(:success)
    expect(mutation_response['workItem']['widgets']).to include(
      {
        'description' => new_description,
        'type' => 'DESCRIPTION'
      }
    )
  end

  context 'when the updated work item is not valid' do
    it 'returns validation errors without the work item' do
      errors = ActiveModel::Errors.new(work_item).tap { |e| e.add(:description, 'error message') }

      allow_next_found_instance_of(::WorkItem) do |instance|
        allow(instance).to receive(:valid?).and_return(false)
        allow(instance).to receive(:errors).and_return(errors)
      end

      post_graphql_mutation(mutation, current_user: current_user)

      expect(mutation_response['workItem']).to be_nil
      expect(mutation_response['errors']).to match_array(['Description error message'])
    end
  end

  context 'when the edited description includes quick action(s)' do
    let(:input) { { 'descriptionWidget' => { 'description' => new_description } } }

    shared_examples 'quick action is applied' do
      before do
        post_graphql_mutation(mutation, current_user: current_user)
      end

      it 'applies the quick action(s)' do
        expect(response).to have_gitlab_http_status(:success)
        expect(mutation_response['workItem']).to include(expected_response)
      end
    end

    context 'with /title quick action' do
      it_behaves_like 'quick action is applied' do
        let(:new_description) { "updated description\n/title updated title" }
        let(:filtered_description) { "updated description" }

        let(:expected_response) do
          {
            'title' => 'updated title',
            'widgets' => include({
              'description' => filtered_description,
              'type' => 'DESCRIPTION'
            })
          }
        end
      end
    end

    context 'with /shrug, /tableflip and /cc quick action' do
      it_behaves_like 'quick action is applied' do
        let(:new_description) { "/tableflip updated description\n/shrug\n/cc @#{developer.username}" }
        # note: \cc performs no action since 15.0
        let(:filtered_description) { "updated description (╯°□°)╯︵ ┻━┻\n ¯\\_(ツ)_/¯\n/cc @#{developer.username}" }
        let(:expected_response) do
          {
            'widgets' => include({
              'description' => filtered_description,
              'type' => 'DESCRIPTION'
            })
          }
        end
      end
    end

    context 'with /close' do
      it_behaves_like 'quick action is applied' do
        let(:new_description) { "Resolved work item.\n/close" }
        let(:filtered_description) { "Resolved work item." }
        let(:expected_response) do
          {
            'state' => 'CLOSED',
            'widgets' => include({
              'description' => filtered_description,
              'type' => 'DESCRIPTION'
            })
          }
        end
      end
    end

    context 'with /reopen' do
      before do
        work_item.close!
      end

      it_behaves_like 'quick action is applied' do
        let(:new_description) { "Re-opening this work item.\n/reopen" }
        let(:filtered_description) { "Re-opening this work item." }
        let(:expected_response) do
          {
            'state' => 'OPEN',
            'widgets' => include({
              'description' => filtered_description,
              'type' => 'DESCRIPTION'
            })
          }
        end
      end
    end
  end
end