From 9642964e4dcfa084ee95fd9fdaa1961bbd3f7726 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thiago=20Figueir=C3=B3?= <tfigueiro@gitlab.com>
Date: Tue, 25 Jul 2023 04:13:55 +0000
Subject: [PATCH] Create shared example for graphql query with data

---
 .../vulnerabilities/vulnerability_spec.rb     | 27 ++++---------------
 .../requests/graphql_shared_examples.rb       | 18 ++++++++-----
 2 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/ee/spec/requests/api/graphql/vulnerabilities/vulnerability_spec.rb b/ee/spec/requests/api/graphql/vulnerabilities/vulnerability_spec.rb
index 11e65db2140c4..d37232304c23e 100644
--- a/ee/spec/requests/api/graphql/vulnerabilities/vulnerability_spec.rb
+++ b/ee/spec/requests/api/graphql/vulnerabilities/vulnerability_spec.rb
@@ -22,30 +22,13 @@
       context "for a #{report_type} vulnerability" do
         let_it_be(:vulnerability) { create(:vulnerability, project: project, report_type: report_type) }
 
-        it_behaves_like "a working graphql query"
-
-        it 'contains data' do
-          expect(graphql_data['vulnerability']).not_to be_nil
+        it_behaves_like 'a working graphql query that returns data' do
+          it 'returns the vulnerability' do
+            expect(graphql_data.dig('vulnerability', 'id')).to eq "gid://gitlab/Vulnerability/#{vulnerability.id}"
+            expect(graphql_data['vulnerability'].keys).to match_array(vulnerability_fields.to_h.keys)
+          end
         end
       end
     end
   end
-
-  describe 'hasRemediations' do
-    let(:vulnerability_fields) { 'hasRemediations' }
-
-    subject { graphql_data.dig('vulnerability', 'hasRemediations') }
-
-    context 'when remediations exist' do
-      let(:vulnerability) { create(:vulnerability, :with_remediation, project: project) }
-
-      it { is_expected.to be(true) }
-    end
-
-    context 'when remediations do not exist' do
-      let(:vulnerability) { create(:vulnerability, project: project) }
-
-      it { is_expected.to be(false) }
-    end
-  end
 end
diff --git a/spec/support/shared_examples/requests/graphql_shared_examples.rb b/spec/support/shared_examples/requests/graphql_shared_examples.rb
index 2c08f9464688e..270c069a62f1e 100644
--- a/spec/support/shared_examples/requests/graphql_shared_examples.rb
+++ b/spec/support/shared_examples/requests/graphql_shared_examples.rb
@@ -5,11 +5,21 @@
 
   it 'returns a successful response', :aggregate_failures do
     expect(response).to have_gitlab_http_status(:success)
-    expect(graphql_errors).to be_nil
+    expect_graphql_errors_to_be_empty
     expect(json_response.keys).to include('data')
   end
 end
 
+RSpec.shared_examples 'a working graphql query that returns data' do
+  include GraphqlHelpers
+
+  it_behaves_like 'a working graphql query'
+
+  it 'contains data' do
+    expect(graphql_data.compact).not_to be_empty
+  end
+end
+
 RSpec.shared_examples 'a working GraphQL mutation' do
   include GraphqlHelpers
 
@@ -20,11 +30,7 @@
   shared_examples 'allows access to the mutation' do
     let(:scopes) { ['api'] }
 
-    it_behaves_like 'a working graphql query' do
-      it 'returns data' do
-        expect(graphql_data.compact).not_to be_empty
-      end
-    end
+    it_behaves_like 'a working graphql query that returns data'
   end
 
   shared_examples 'prevents access to the mutation' do
-- 
GitLab