Class: RSpec::GraphqlMatchers::AcceptArgument

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rspec/graphql_matchers/accept_argument.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected_arg_name) ⇒ AcceptArgument

Returns a new instance of AcceptArgument.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/graphql_matchers/accept_argument.rb', line 9

def initialize(expected_arg_name)
  @expectations = []

  if expected_arg_name.is_a?(Hash)
    (expected_arg_name, expected_type) = expected_arg_name.to_a.first
    of_type(expected_type)

    warn 'DEPRECATION WARNING: using accept_arguments with a hash will be '\
         'deprecated on the next major release. Use the format ' \
         "`accept_argument(:argument_name).of_type('ExpectedType!') instead.`"
  end

  @expected_arg_name = expected_arg_name.to_s
  @expected_camel_arg_name = GraphQL::Schema::Member::BuildType.camelize(
    @expected_arg_name
  )
end

Instance Method Details

#descriptionObject



55
56
57
# File 'lib/rspec/graphql_matchers/accept_argument.rb', line 55

def description
  ["accept argument `#{@expected_arg_name}`"].concat(descriptions).join(', ')
end

#failure_messageObject



46
47
48
49
50
51
52
53
# File 'lib/rspec/graphql_matchers/accept_argument.rb', line 46

def failure_message
  base_msg = "expected #{member_name(@graph_object)} " \
    "to accept argument `#{@expected_arg_name}`" \

  return "#{base_msg} #{failure_messages.join(', ')}" if @actual_argument

  "#{base_msg} but no argument was found with that name"
end

#matches?(graph_object) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rspec/graphql_matchers/accept_argument.rb', line 27

def matches?(graph_object)
  @graph_object = graph_object

  @actual_argument = field_arguments[@expected_arg_name]
  @actual_argument ||= field_arguments[@expected_camel_arg_name]
  return false if @actual_argument.nil?

  @results = @expectations.reject do |matcher|
    matcher.matches?(@actual_argument)
  end

  @results.empty?
end

#of_type(expected_field_type) ⇒ Object



41
42
43
44
# File 'lib/rspec/graphql_matchers/accept_argument.rb', line 41

def of_type(expected_field_type)
  @expectations << HaveAFieldMatchers::OfType.new(expected_field_type)
  self
end