Sha256: 4979421cd0d92ffa4d81d4ea25cf330d4736d3e0940bc762797d88b84e9767e4

Contents?: true

Size: 972 Bytes

Versions: 2

Compression:

Stored size: 972 Bytes

Contents

# frozen_string_literal: true

module RSpec
  module GraphqlMatchers
    module HaveAFieldMatchers
      class WithHashKey
        def initialize(expected_hash_key)
          @expected_hash_key = expected_hash_key
        end

        def description
          "with hash key `#{@expected_hash_key}`"
        end

        def matches?(actual_field)
          @actual_hash_key = get_hash_key(actual_field)
          @actual_hash_key == @expected_hash_key.to_sym
        end

        def failure_message
          "#{description}, but it was `#{@actual_hash_key}`"
        end

        private

        def get_hash_key(actual_field)
          if actual_field.respond_to?(:hash_key)
            return actual_field.hash_key.to_sym if actual_field.hash_key
          end

          if actual_field.respond_to?(:metadata)
            return actual_field.metadata[:type_class].method_sym
          end

          actual_field.method_sym
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rspec-graphql_matchers-2.0.0.pre.rc.0 lib/rspec/graphql_matchers/have_a_field_matchers/with_hash_key.rb
rspec-graphql_matchers-1.4.0 lib/rspec/graphql_matchers/have_a_field_matchers/with_hash_key.rb