File: notes_on_noteables_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (161 lines) | stat: -rw-r--r-- 5,071 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

RSpec.shared_context 'exposing regular notes on a noteable in GraphQL' do
  include GraphqlHelpers

  let(:note) do
    create(
      :note,
      noteable: noteable,
      project: (noteable.project if noteable.respond_to?(:project))
    )
  end

  let(:user) { note.author }

  context 'for regular notes' do
    let!(:system_note) do
      create(
        :note,
        system: true,
        noteable: noteable,
        project: (noteable.project if noteable.respond_to?(:project))
      )
    end

    let(:filters) { "" }

    let(:query) do
      note_fields = <<~NOTES
      notes #{filters} {
        count
        edges {
          node {
            #{all_graphql_fields_for('Note', max_depth: 1)}
            awardEmoji {
              nodes {
                name
                user {
                  name
                }
              }
            }
          }
        }
      }
      NOTES

      noteable_query(note_fields)
    end

    it_behaves_like 'a working graphql query' do
      before do
        post_graphql(query, current_user: user)
      end
    end

    it 'includes all notes' do
      post_graphql(query, current_user: user)

      expect(noteable_data['notes']['count']).to eq(2)
      expect(noteable_data['notes']['edges'][0]['node']['body']).to eq(system_note.note)
      expect(noteable_data['notes']['edges'][1]['node']['body']).to eq(note.note)
    end

    it 'avoids N+1 queries' do
      create(:award_emoji, awardable: note, name: 'star', user: user)
      another_user = create(:user, developer_of: note.resource_parent)
      create(:note, project: note.project, noteable: noteable, author: another_user)
      note_from_external_participant = create(:note,
        project: note.project, noteable: noteable, author: Users::Internal.support_bot)
      create(:note_metadata, note: note_from_external_participant)

      post_graphql(query, current_user: user)

      control = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: user) }

      expect_graphql_errors_to_be_empty

      another_note = create(:note, project: note.project, noteable: noteable, author: user)
      create(:award_emoji, awardable: another_note, name: 'star', user: user)
      another_user = create(:user, developer_of: note.resource_parent)
      note_with_different_user = create(:note, project: note.project, noteable: noteable, author: another_user)
      create(:award_emoji, awardable: note_with_different_user, name: 'star', user: user)
      another_note_from_external_participant = create(:note,
        project: note.project, noteable: noteable, author: Users::Internal.support_bot)
      create(:note_metadata, note: another_note_from_external_participant)

      expect { post_graphql(query, current_user: user) }.not_to exceed_query_limit(control)
      expect_graphql_errors_to_be_empty
    end

    context 'when filter is provided' do
      context 'when filter is set to ALL_NOTES' do
        let(:filters) { "(filter: ALL_NOTES)" }

        it 'returns all the notes' do
          post_graphql(query, current_user: user)

          expect(noteable_data['notes']['count']).to eq(2)
          expect(noteable_data['notes']['edges'][0]['node']['body']).to eq(system_note.note)
          expect(noteable_data['notes']['edges'][1]['node']['body']).to eq(note.note)
        end
      end

      context 'when filter is set to ONLY_COMMENTS' do
        let(:filters) { "(filter: ONLY_COMMENTS)" }

        it 'returns only the comments' do
          post_graphql(query, current_user: user)

          expect(noteable_data['notes']['count']).to eq(1)
          expect(noteable_data['notes']['edges'][0]['node']['body']).to eq(note.note)
        end
      end

      context 'when filter is set to ONLY_ACTIVITY' do
        let(:filters) { "(filter: ONLY_ACTIVITY)" }

        it 'returns only the activity notes' do
          post_graphql(query, current_user: user)

          expect(noteable_data['notes']['count']).to eq(1)
          expect(noteable_data['notes']['edges'][0]['node']['body']).to eq(system_note.note)
        end
      end
    end
  end

  context "for discussions" do
    let(:query) do
      discussion_fields = <<~DISCUSSIONS
      discussions {
        edges {
          node {
            #{all_graphql_fields_for('Discussion', max_depth: 4, excluded: ['productAnalyticsState'])}
          }
        }
      }
      DISCUSSIONS

      noteable_query(discussion_fields)
    end

    let!(:reply) { create(:note, noteable: noteable, in_reply_to: note, discussion_id: note.discussion_id) }

    it_behaves_like 'a working graphql query' do
      before do
        post_graphql(query, current_user: user)
      end
    end

    it 'includes all discussion notes' do
      post_graphql(query, current_user: user)

      discussion = noteable_data['discussions']['edges'].first['node']
      ids = discussion['notes']['edges'].map { |note_edge| note_edge['node']['id'] }

      expect(ids).to eq([note.to_global_id.to_s, reply.to_global_id.to_s])
    end
  end
end