Skip to content

Commit

Permalink
Add with_deprecation_reason matcher to have_a_field
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyArra committed May 4, 2020
1 parent 051a510 commit 7c89b9e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rspec/graphql_matchers/have_a_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative './have_a_field_matchers/with_property'
require_relative './have_a_field_matchers/with_metadata'
require_relative './have_a_field_matchers/with_hash_key'
require_relative './have_a_field_matchers/with_deprecation_reason'

module RSpec
module GraphqlMatchers
Expand Down Expand Up @@ -54,6 +55,11 @@ def with_metadata(expected_metadata)
self
end

def with_deprecation_reason(expected_reason = nil)
@expectations << HaveAFieldMatchers::WithDeprecationReason.new(expected_reason)
self
end

def failure_message
base_msg = "expected #{member_name(@graph_object)} " \
"to define field `#{@expected_field_name}`" \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module RSpec
module GraphqlMatchers
module HaveAFieldMatchers
class WithDeprecationReason
def initialize(expected_reason)
@expected_reason = expected_reason
end

def matches?(actual_field)
@actual_reason = actual_field.deprecation_reason

if @expected_reason.nil?
!actual_field.deprecation_reason.nil?
else
actual_field.deprecation_reason == @expected_reason
end
end

def failure_message
message = "#{description}, but it was "
message + (@actual_reason.nil? ? 'not deprecated' : "`#{@actual_reason}`")
end

def description
if @expected_reason.nil?
'with a deprecation reason'
else
"with deprecation reason `#{@expected_reason}`"
end
end
end
end
end
end
60 changes: 60 additions & 0 deletions spec/rspec/have_a_field_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,60 @@ module RSpec::GraphqlMatchers
)
end
end

describe '.with_deprecation_reason' do
context 'when the field has a deprecation reason' do
let(:deprecated_field) { :deprecated_field }

context 'with an expected deprecation reason' do
it 'passes when the deprecation reasons match' do
expect(a_type).to have_a_field(deprecated_field)
.with_deprecation_reason('deprecated')
end

it 'fails when the deprecation reasons do not match' do
expect do
expect(a_type).to have_a_field(deprecated_field)
.with_deprecation_reason('different deprecation reason')
end.to fail_with(
'expected TestObject to define field `deprecated_field` with ' \
'deprecation reason `different deprecation reason`, ' \
'but it was `deprecated`'
)
end
end

context 'without an expected deprecation reason' do
it 'passes' do
expect(a_type).to have_a_field(deprecated_field).with_deprecation_reason
end
end
end

context 'when the field does not have a deprecation reason' do
let(:non_deprecated_field) { :id }

it 'fails without an expected deprecation reason' do
expect do
expect(a_type).to have_a_field(non_deprecated_field)
.with_deprecation_reason
end.to fail_with(
'expected TestObject to define field `id` with a deprecation reason, ' \
'but it was not deprecated'
)
end

it 'fails with an expected deprecation reason' do
expect do
expect(a_type).to have_a_field(non_deprecated_field)
.with_deprecation_reason('deprecated')
end.to fail_with(
'expected TestObject to define field `id` with deprecation reason `deprecated`, ' \
'but it was not deprecated'
)
end
end
end
end

context 'using the new class-based api' do
Expand All @@ -115,6 +169,8 @@ module RSpec::GraphqlMatchers
field :other, types.String, hash_key: :other_on_hash, null: true
field :is_test, types.Boolean, null: true
field :not_camelized, types.String, null: false, camelize: false
field :deprecated_field, types.String, null: true,
deprecation_reason: 'deprecated'
end
end

Expand All @@ -129,6 +185,8 @@ module RSpec::GraphqlMatchers
field :other, types.String, hash_key: :other_on_hash, null: true
field :is_test, types.Boolean, null: true
field :not_camelized, types.String, null: false, camelize: false
field :deprecated_field, types.String, null: true,
deprecation_reason: 'deprecated'
end

Class.new(GraphQL::Schema::Object) do
Expand Down Expand Up @@ -161,6 +219,8 @@ module RSpec::GraphqlMatchers
field :isTest, types.Boolean

field :not_camelized, types.String, camelize: false

field :deprecated_field, types.String, deprecation_reason: 'deprecated'
end
end

Expand Down

0 comments on commit 7c89b9e

Please sign in to comment.