diff --git a/lib/rspec/graphql_matchers/base_matcher.rb b/lib/rspec/graphql_matchers/base_matcher.rb index 6ef1b91..c90f298 100644 --- a/lib/rspec/graphql_matchers/base_matcher.rb +++ b/lib/rspec/graphql_matchers/base_matcher.rb @@ -16,7 +16,7 @@ def types_match?(actual_type, expected_type) end def type_name(a_type) - a_type = a_type.to_graphql if a_type.respond_to?(:to_graphql) + a_type = a_type.to_type_signature if a_type.respond_to?(:to_type_signature) a_type.to_s end diff --git a/lib/rspec/graphql_matchers/be_of_type.rb b/lib/rspec/graphql_matchers/be_of_type.rb index dfee88a..91281c7 100644 --- a/lib/rspec/graphql_matchers/be_of_type.rb +++ b/lib/rspec/graphql_matchers/be_of_type.rb @@ -12,7 +12,7 @@ def initialize(expected) end def matches?(actual_sample) - @sample = to_graphql(actual_sample) + @sample = to_type_signature(actual_sample) sample.respond_to?(:type) && types_match?(sample.type, @expected) end @@ -27,10 +27,10 @@ def description private - def to_graphql(field_sample) - return field_sample unless field_sample.respond_to?(:to_graphql) + def to_type_signature(field_sample) + return field_sample unless field_sample.respond_to?(:to_type_signature) - field_sample.to_graphql + field_sample.to_type_signature end end end diff --git a/lib/rspec/graphql_matchers/have_a_field.rb b/lib/rspec/graphql_matchers/have_a_field.rb index 44757d1..c2fec8f 100644 --- a/lib/rspec/graphql_matchers/have_a_field.rb +++ b/lib/rspec/graphql_matchers/have_a_field.rb @@ -80,7 +80,7 @@ def actual_field field = field_collection[@expected_field_name] field ||= field_collection[@expected_camel_field_name] - field.respond_to?(:to_graphql) ? field.to_graphql : field + field.respond_to?(:to_type_signature) ? field.to_type_signature : field end end diff --git a/lib/rspec/graphql_matchers/matchers.rb b/lib/rspec/graphql_matchers/matchers.rb index a90ee12..47f5894 100644 --- a/lib/rspec/graphql_matchers/matchers.rb +++ b/lib/rspec/graphql_matchers/matchers.rb @@ -6,6 +6,7 @@ require 'rspec/graphql_matchers/accept_argument' require 'rspec/graphql_matchers/have_a_field' require 'rspec/graphql_matchers/implement' +require 'rspec/graphql_matchers/return_graphql_error' module RSpec module Matchers @@ -41,5 +42,9 @@ def have_a_return_field(field_name) def implement(*interface_names) RSpec::GraphqlMatchers::Implement.new(interface_names.flatten) end + + def return_graphql_error(expected) + RSpec::GraphqlMatchers::ReturnGraphqlError.new(expected) + end end end diff --git a/lib/rspec/graphql_matchers/return_graphql_error.rb b/lib/rspec/graphql_matchers/return_graphql_error.rb new file mode 100644 index 0000000..eeabe58 --- /dev/null +++ b/lib/rspec/graphql_matchers/return_graphql_error.rb @@ -0,0 +1,45 @@ +require_relative 'base_matcher' + +module RSpec + module GraphqlMatchers + class ReturnGraphqlError < BaseMatcher + attr_reader :errors, :expected + + def initialize(expected = nil) + @expected = expected + end + + def matches?(resolved_data) + @errors = resolved_data['errors'] + @resolved_data = resolved_data.to_h + + return false if no_errors? + + return unless @expected + if @expected.is_a? Regexp + @errors.any? { |error| error['message'] =~ @expected } + else + @errors.any? { |error| error['message'] == @expected } + end + end + + def no_errors? + @errors.nil? || @errors.empty? + end + + def failure_message + if no_errors? + if @expected + "Expected to find an error with message `#{@expected}`, " \ + "but there were no errors in: `#{@resolved_data}`" + else + 'Expected to find errors, but there were no errors.' + end + else + "Expected result to have an error message `#{@expected}`, " \ + "but there were no errors in: `#{@resolved_data}`" + end + end + end + end +end