-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from marcgreenstock/master
added interface matcher
- Loading branch information
Showing
4 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative 'base_matcher' | ||
|
||
module RSpec | ||
module GraphqlMatchers | ||
class Implement < BaseMatcher | ||
def initialize(interface_names) | ||
@expected = interface_names.map(&:to_s) | ||
end | ||
|
||
def matches?(graph_object) | ||
@graph_object = graph_object | ||
@actual = actual | ||
@expected.each do |name| | ||
return false unless @actual.include?(name) | ||
end | ||
end | ||
|
||
def failure_message | ||
message = "expected interfaces: #{@expected.join(', ')}\n" | ||
message += "actual interfaces: #{@actual.join(', ')}" | ||
message | ||
end | ||
|
||
def failure_message_when_negated | ||
message = "unexpected interfaces: #{@expected.join(', ')}\n" | ||
message += "actual interfaces: #{@actual.join(', ')}" | ||
message | ||
end | ||
|
||
def description | ||
"implement #{@expected.join(', ')}" | ||
end | ||
|
||
private | ||
|
||
def actual | ||
if @graph_object.respond_to?(:interfaces) | ||
@graph_object.interfaces.map(&:to_s) | ||
else | ||
raise "Invalid object #{@graph_object} provided to #{matcher_name} " \ | ||
'matcher. It does not seem to be a valid GraphQL object type.' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'spec_helper' | ||
|
||
module RSpec::GraphqlMatchers | ||
describe 'expect(a_type).to implement(interface_names)' do | ||
AnInterface = GraphQL::InterfaceType.define do | ||
name 'AnInterface' | ||
end | ||
|
||
AnotherInterface = GraphQL::InterfaceType.define do | ||
name 'AnotherInterface' | ||
end | ||
|
||
subject(:a_type) do | ||
GraphQL::ObjectType.define do | ||
name 'TestObject' | ||
interfaces [ | ||
GraphQL::Relay::Node.interface, | ||
AnInterface | ||
] | ||
end | ||
end | ||
|
||
it { is_expected.to implement('Node') } | ||
it { is_expected.to implement('AnInterface') } | ||
it { is_expected.to implement('Node', 'AnInterface') } | ||
it { is_expected.to implement(['Node']) } | ||
it { is_expected.to implement(['AnInterface']) } | ||
it { is_expected.to implement(['Node', 'AnInterface']) } | ||
it { is_expected.to implement(GraphQL::Relay::Node.interface, AnInterface) } | ||
it { is_expected.to implement([GraphQL::Relay::Node.interface, AnInterface]) } | ||
|
||
it { is_expected.not_to implement('AnotherInterface') } | ||
it { is_expected.not_to implement(['AnotherInterface']) } | ||
it { is_expected.not_to implement(AnotherInterface) } | ||
it { is_expected.not_to implement([AnotherInterface]) } | ||
|
||
it 'fails with a failure message when the type does include the interfaces' do | ||
expect { expect(a_type).to implement('AnotherInterface') } | ||
.to fail_with( | ||
"expected interfaces: AnotherInterface\n" \ | ||
'actual interfaces: Node, AnInterface' | ||
) | ||
end | ||
|
||
it 'provides a description' do | ||
matcher = implement('Node, AnInterface') | ||
matcher.matches?(a_type) | ||
|
||
expect(matcher.description).to eq('implement Node, AnInterface') | ||
end | ||
|
||
context 'when an invalid type is passed' do | ||
let(:a_type) { double(to_s: 'InvalidObject') } | ||
|
||
it 'fails with a Runtime error' do | ||
expect { expect(a_type).to have_a_field(:id) } | ||
.to raise_error( | ||
RuntimeError, | ||
'Invalid object InvalidObject provided to have_a_field matcher. ' \ | ||
'It does not seem to be a valid GraphQL object type.' | ||
) | ||
end | ||
end | ||
end | ||
end |