Commit 7b2baef3 authored by Alex Kalderimis's avatar Alex Kalderimis

Add Graph-QL testing support

parent e7c31078
...@@ -105,12 +105,15 @@ module GraphqlHelpers ...@@ -105,12 +105,15 @@ module GraphqlHelpers
end end
def query_graphql_field(name, attributes = {}, fields = nil) def query_graphql_field(name, attributes = {}, fields = nil)
fields ||= all_graphql_fields_for(name.classify) field_params = if attributes.present?
attributes = attributes_to_graphql(attributes) "(#{attributes_to_graphql(attributes)})" if attributes.present?
attributes = "(#{attributes})" if attributes.present? else
''
end
<<~QUERY <<~QUERY
#{name}#{attributes} #{GraphqlHelpers.fieldnamerize(name.to_s)}#{field_params}
#{wrap_fields(fields)} #{wrap_fields(fields || all_graphql_fields_for(name.to_s.classify))}
QUERY QUERY
end end
...@@ -301,6 +304,17 @@ module GraphqlHelpers ...@@ -301,6 +304,17 @@ module GraphqlHelpers
def global_id_of(model) def global_id_of(model)
model.to_global_id.to_s model.to_global_id.to_s
end end
def missing_required_argument(path, argument)
a_hash_including(
'path' => ['query'].concat(path),
'extensions' => a_hash_including('code' => 'missingRequiredArguments', 'arguments' => argument.to_s)
)
end
def custom_graphql_error(path, msg)
a_hash_including('path' => path, 'message' => msg)
end
end end
# This warms our schema, doing this as part of loading the helpers to avoid # This warms our schema, doing this as part of loading the helpers to avoid
......
...@@ -11,8 +11,22 @@ RSpec::Matchers.define :have_graphql_fields do |*expected| ...@@ -11,8 +11,22 @@ RSpec::Matchers.define :have_graphql_fields do |*expected|
Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) } Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
end end
@allow_extra = false
chain :only do
@allow_extra = false
end
chain :at_least do
@allow_extra = true
end
match do |kls| match do |kls|
expect(kls.fields.keys).to contain_exactly(*expected_field_names) if @allow_extra
expect(kls.fields.keys).to include(*expected_field_names)
else
expect(kls.fields.keys).to contain_exactly(*expected_field_names)
end
end end
failure_message do |kls| failure_message do |kls|
...@@ -22,7 +36,7 @@ RSpec::Matchers.define :have_graphql_fields do |*expected| ...@@ -22,7 +36,7 @@ RSpec::Matchers.define :have_graphql_fields do |*expected|
message = [] message = []
message << "is missing fields: <#{missing.inspect}>" if missing.any? message << "is missing fields: <#{missing.inspect}>" if missing.any?
message << "contained unexpected fields: <#{extra.inspect}>" if extra.any? message << "contained unexpected fields: <#{extra.inspect}>" if extra.any? && !@allow_extra
message.join("\n") message.join("\n")
end end
......
# frozen_string_literal: true
require 'spec_helper'
# Shared example for legal queries that are expected to return nil.
# Requires the following let bindings to be defined:
# - post_query: action to send the query
# - path: array of keys from query root to the result
shared_examples 'a failure to find anything' do
it 'finds nothing' do
post_query
data = graphql_data.dig(*path)
expect(data).to be_nil
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