Commit acc6e675 authored by Alex Kalderimis's avatar Alex Kalderimis

Use a shared example, and a describe block per field

This also fixes todo-ed rubocop violations in graphql_matchers
parent 8c1c513f
......@@ -14,26 +14,37 @@ RSpec.describe GitlabSchema.types['SnippetBlob'] do
expect(described_class).to have_graphql_fields(*expected_fields)
end
specify { expect(described_class.fields['richData'].type).not_to be_non_null }
specify { expect(described_class.fields['plainData'].type).not_to be_non_null }
specify { expect(described_class.fields['rawPath'].type).to be_non_null }
specify { expect(described_class.fields['size'].type).to be_non_null }
specify { expect(described_class.fields['binary'].type).to be_non_null }
specify { expect(described_class.fields['name'].type).not_to be_non_null }
specify { expect(described_class.fields['path'].type).not_to be_non_null }
specify { expect(described_class.fields['simpleViewer'].type).to be_non_null }
specify { expect(described_class.fields['richViewer'].type).not_to be_non_null }
specify { expect(described_class.fields['mode'].type).not_to be_non_null }
specify { expect(described_class.fields['externalStorage'].type).not_to be_non_null }
specify { expect(described_class.fields['renderedAsText'].type).to be_non_null }
let_it_be(:nullity) do
{
'richData' => be_nullable,
'plainData' => be_nullable,
'rawPath' => be_non_null,
'size' => be_non_null,
'binary' => be_non_null,
'name' => be_nullable,
'path' => be_nullable,
'simpleViewer' => be_non_null,
'richViewer' => be_nullable,
'mode' => be_nullable,
'externalStorage' => be_nullable,
'renderedAsText' => be_non_null
}
end
let_it_be(:blob) { create(:snippet, :public, :repository).blobs.first }
described_class.fields.each_value do |field|
it "resolves #{field.graphql_name} using the presenter", :request_store do
shared_examples 'a field from the snippet blob presenter' do |field|
it "resolves using the presenter", :request_store do
presented = SnippetBlobPresenter.new(blob)
expect(resolve_field(field, blob)).to eq(presented.try(field.method_sym))
end
end
described_class.fields.each_value do |field|
describe field.graphql_name do
it_behaves_like 'a field from the snippet blob presenter', field
specify { expect(field.type).to match(nullity.fetch(field.graphql_name)) }
end
end
end
# frozen_string_literal: true
RSpec::Matchers.define_negated_matcher :be_nullable, :be_non_null
RSpec::Matchers.define :require_graphql_authorizations do |*expected|
match do |klass|
permissions = if klass.respond_to?(:required_permissions)
......@@ -90,7 +92,7 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected|
@names ||= Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
if field.type.try(:ancestors)&.include?(GraphQL::Types::Relay::BaseConnection)
@names | %w(after before first last)
@names | %w[after before first last]
else
@names
end
......@@ -103,9 +105,10 @@ RSpec::Matchers.define :have_graphql_arguments do |*expected|
end
failure_message do |field|
names = expected_names(field)
names = expected_names(field).inspect
args = field.arguments.keys.inspect
"expected that #{field.name} would have the following fields: #{names.inspect}, but it has #{field.arguments.keys.inspect}."
"expected that #{field.name} would have the following arguments: #{names}, but it has #{args}."
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment