Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-error-matcher #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rspec/graphql_matchers/base_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/rspec/graphql_matchers/be_of_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/graphql_matchers/have_a_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/graphql_matchers/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
45 changes: 45 additions & 0 deletions lib/rspec/graphql_matchers/return_graphql_error.rb
Original file line number Diff line number Diff line change
@@ -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