rspec-graphql_matchers 0.6 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb768f28fb26de27d50fa7f3cc904e59538e0f90
4
- data.tar.gz: d1e7f770c4db3ee7dedb58635bf37be01fd50a5a
3
+ metadata.gz: 2d5a665edd8997d234fa2d6d029c7ede2e6f5686
4
+ data.tar.gz: 8c558b4b4438bcd32d5dfb4b969f10c7a775fdb9
5
5
  SHA512:
6
- metadata.gz: b97f59f215849f232d468f982ce25395c80a24242a16a28f9efc9de451b43228dd375aa098a72e64eb97d65c3c6480576ed2dccce8fdd3ff23c644fd8aec3a3e
7
- data.tar.gz: 12082a0895c45baae469d80a86832d5d17fa75750e3a8150d816346b7b52d85921f541a58ee564f12ae3b5f92cbda7b3da7c0db9e9c89b78a64b8ce11f7ae52b
6
+ metadata.gz: 36298dc58bb067d78d0997e5ca7537f09eddfa00931aeec6be867cde92b581e13f83c062417c7b052dd280df4a0a93c30b117b1113a99ee290b27bb800ebb5e7
7
+ data.tar.gz: 4338619fb13dec26dc567f50cc4bc6fbd826b27d524d61e50be69393b087b152dfca14b728041de0883390dbb8e7384f8f376f8618db86460203cbe8d2cbb702
@@ -1,3 +1,7 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+
1
5
  Style/Documentation:
2
6
  Enabled: false
3
7
 
@@ -26,3 +30,11 @@ Metrics/LineLength:
26
30
 
27
31
  Metrics/MethodLength:
28
32
  Max: 15
33
+
34
+ Metrics/BlockLength:
35
+ Exclude:
36
+ - spec/rspec/*.rb
37
+
38
+ Metrics/ModuleLength:
39
+ Exclude:
40
+ - spec/rspec/*.rb
@@ -8,6 +8,16 @@
8
8
 
9
9
  ### Bug fixes
10
10
 
11
+ ## 0.7.0 (July 27, 2017)
12
+
13
+ ### New features
14
+
15
+ - (#9, #8) New chainable matchers `with_property`, `with_hash_key` and `with_metadata` (Thanks to @marcgreenstock).
16
+
17
+ ### Improvements
18
+
19
+ - Default Raketask runs rubocop specs as well (Thanks to @marcgreenstock).
20
+
11
21
  ## 0.6.0 (July 25, 2017)
12
22
 
13
23
  ### New features
data/README.md CHANGED
@@ -12,7 +12,7 @@ gem 'rspec-graphql_matchers'
12
12
 
13
13
  The matchers currently supported are:
14
14
  - `expect(a_graphql_object).to have_a_field(field_name).that_returns(valid_type)`
15
- - `expect(a_graphql_object).to interface(interface_name, ...)`
15
+ - `expect(a_graphql_object).to implement(interface_name, ...)`
16
16
  - `expect(a_mutation_type).to have_a_return_field(field_name).returning(valid_type)`
17
17
  - `expect(a_mutation_type).to have_an_input_field(field_name).of_type(valid_type)`
18
18
  - `expect(a_field).to be_of_type(valid_type)`
@@ -24,6 +24,11 @@ Where a valid type for the expectation is either:
24
24
  - A String representation of a type: `"String!"`, `"Int!"`, `"[String]!"`
25
25
  (note the exclamation mark at the end, as required by the [GraphQL specs](http://graphql.org/).
26
26
 
27
+ Testing `:property`, `:hash_key` and *metadata* is also possible by chaining `.with_property`, `.with_hash_key` and `.with_metadata`. For example:
28
+
29
+ - `expect(a_graphql_object).to have_a_field(field_name).with_property(property_name).with_metadata(metadata_hash)`
30
+ - `expect(a_graphql_object).to have_a_field(field_name).with_hash_key(hash_key)`
31
+
27
32
  ## Examples
28
33
 
29
34
  Given a `GraphQL::ObjectType` defined as
@@ -36,9 +41,15 @@ PostType = GraphQL::ObjectType.define do
36
41
 
37
42
  interfaces [GraphQL::Relay::Node.interface]
38
43
 
39
- field :id, !types.ID
44
+ field :id, !types.ID,
45
+ property: :post_id
46
+
47
+ field :comments,
48
+ !types[types.String],
49
+ hash_key: :post_comments
40
50
 
41
- field :comments, !types[types.String]
51
+ field :isPublished,
52
+ admin_only: true
42
53
 
43
54
  field :subposts, PostType do
44
55
  type !PostType
@@ -109,7 +120,17 @@ describe PostType do
109
120
  end
110
121
  ```
111
122
 
112
- ### 3) Test the arguments accepted by a field with `accept_arguments` matcher:
123
+ ### 3) Test a specific field with `with_property`, `with_hash_key` and `with_metadata`
124
+
125
+ ```ruby
126
+ describe PostType do
127
+ it { is_expected.to have_a_field(:id).with_property(:post_id) }
128
+ it { is_expected.to have_a_field(:comments).with_hash_key(:post_comments) }
129
+ it { is_expected.to have_a_field(:isPublished).with_metadata(admin_only: true) }
130
+ end
131
+ ```
132
+
133
+ ### 4) Test the arguments accepted by a field with `accept_arguments` matcher:
113
134
 
114
135
  ```ruby
115
136
  describe PostType do
@@ -130,11 +151,17 @@ describe PostType do
130
151
  end
131
152
  ```
132
153
 
133
- ### 4) Test an object's interface implementations:
154
+ The spec will only pass if all attributes/types specified in the hash are
155
+ defined on the field.
156
+
157
+ For better fluency, `accept_arguments` is also available in singular form, as
158
+ `accept_argument`.
159
+
160
+ ### 5) Test an object's interface implementations:
134
161
 
135
162
  ```ruby
136
163
  describe PostType do
137
- it 'interfaces Node' do
164
+ it 'implements interface Node' do
138
165
  expect(subject).to implement('Node')
139
166
  end
140
167
 
@@ -144,12 +171,6 @@ describe PostType do
144
171
  end
145
172
  ```
146
173
 
147
- The spec will only pass if all attributes/types specified in the hash are
148
- defined on the field.
149
-
150
- For better fluency, `accept_arguments` is also available in singular form, as
151
- `accept_argument`.
152
-
153
174
  ## TODO
154
175
 
155
176
  - New matchers!
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
5
7
 
6
- task :default => :spec
8
+ task default: %i[spec rubocop]
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "rspec/graphql_matchers"
3
+ require 'bundler/setup'
4
+ require 'rspec/graphql_matchers'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "rspec/graphql_matchers"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -3,60 +3,99 @@ require_relative 'base_matcher'
3
3
  module RSpec
4
4
  module GraphqlMatchers
5
5
  class HaveAField < BaseMatcher
6
+ DESCRIPTIONS = {
7
+ type: 'of type `%s`',
8
+ property: 'reading from the `%s` property',
9
+ hash_key: 'reading from the `%s` hash_key',
10
+ metadata: 'with metadata `%s`'
11
+ }.freeze
12
+
6
13
  def initialize(expected_field_name, fields = :fields)
7
14
  @expected_field_name = expected_field_name.to_s
8
- @expected_field_type = @graph_object = nil
9
15
  @fields = fields.to_sym
16
+ @expectations = []
17
+ @descriptions = []
10
18
  end
11
19
 
12
20
  def matches?(graph_object)
13
21
  @graph_object = graph_object
14
22
 
15
23
  @actual_field = field_collection[@expected_field_name]
16
- valid_field? && types_match?(@actual_field.type, @expected_field_type)
24
+ return false if @actual_field.nil?
25
+
26
+ @results = @expectations.map do |expectaiton|
27
+ name, expected_value = expectaiton
28
+ [name, expectation_matches?(name, expected_value)]
29
+ end.to_h
30
+ @results.values.all?
17
31
  end
18
32
 
19
33
  def that_returns(expected_field_type)
20
- @expected_field_type = expected_field_type
21
-
34
+ @expectations << [:type, expected_field_type]
22
35
  self
23
36
  end
24
37
  alias returning that_returns
25
38
  alias of_type that_returns
26
39
 
40
+ def with_property(expected_property_name)
41
+ @expectations << [:property, expected_property_name]
42
+ self
43
+ end
44
+
45
+ def with_hash_key(expected_hash_key)
46
+ @expectations << [:hash_key, expected_hash_key]
47
+ self
48
+ end
49
+
50
+ def with_metadata(expected_metadata)
51
+ @expectations << [:metadata, expected_metadata]
52
+ self
53
+ end
54
+
27
55
  def failure_message
28
56
  "expected #{describe_obj(@graph_object)} to " \
29
57
  "#{description}, #{explanation}."
30
58
  end
31
59
 
32
60
  def description
33
- "define field `#{@expected_field_name}`" + of_type_description
61
+ ["define field `#{@expected_field_name}`"]
62
+ .concat(descriptions).join(', ')
34
63
  end
35
64
 
36
65
  private
37
66
 
67
+ def descriptions
68
+ @expectations.map do |expectation|
69
+ name, expected_value = expectation
70
+ format(DESCRIPTIONS[name], expected_value)
71
+ end
72
+ end
73
+
38
74
  def explanation
39
75
  return 'but no field was found with that name' unless @actual_field
40
-
41
- "but the field type was `#{@actual_field.type}`"
76
+ @results.each do |result|
77
+ name, match = result
78
+ next if match
79
+ return format('but the %s was `%s`', name, @actual_field.send(name))
80
+ end
42
81
  end
43
82
 
44
- def valid_field?
45
- unless @expected_field_type.nil? || @actual_field.respond_to?(:type)
46
- error_msg = "The `#{@expected_field_name}` field defined by the GraphQL " \
47
- 'object does\'t seem valid as it does not respond to #type. ' \
48
- "\n\n\tThe field found was #{@actual_field.inspect}. "
49
- puts error_msg
50
- raise error_msg
83
+ def expectation_matches?(name, expected_value)
84
+ ensure_method_exists!(name)
85
+ if expected_value.is_a?(Hash)
86
+ @actual_field.send(name) == expected_value
87
+ else
88
+ @actual_field.send(name).to_s == expected_value.to_s
51
89
  end
52
-
53
- @actual_field
54
90
  end
55
91
 
56
- def of_type_description
57
- return '' unless @expected_field_type
58
-
59
- " of type `#{@expected_field_type}`"
92
+ def ensure_method_exists!(method_name)
93
+ return if @actual_field.respond_to?(method_name)
94
+ raise(
95
+ "The `#{@expected_field_name}` field defined by the GraphQL object " \
96
+ "does\'t seem valid as it does not respond to ##{method_name}. " \
97
+ "\n\n\tThe field found was #{@actual_field.inspect}. "
98
+ )
60
99
  end
61
100
 
62
101
  def describe_obj(field)
@@ -20,14 +20,10 @@ module RSpec
20
20
  RSpec::GraphqlMatchers::HaveAField.new(field_name)
21
21
  end
22
22
  alias have_field have_a_field
23
-
24
- # rubocop:disable Style/PredicateName
25
23
  def have_an_input_field(field_name)
26
24
  RSpec::GraphqlMatchers::HaveAField.new(field_name, :input_fields)
27
25
  end
28
26
  alias have_input_field have_an_input_field
29
-
30
- # rubocop:disable Style/PredicateName
31
27
  def have_a_return_field(field_name)
32
28
  RSpec::GraphqlMatchers::HaveAField.new(field_name, :return_fields)
33
29
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module GraphqlMatchers
3
- VERSION = '0.6'.freeze
3
+ VERSION = '0.7.1'.freeze
4
4
  end
5
5
  end
@@ -1,26 +1,31 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'rspec/graphql_matchers/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = 'rspec-graphql_matchers'
8
- spec.version = Rspec::GraphqlMatchers::VERSION
9
- spec.authors = ['Samuel Brandão']
10
- spec.email = ['gb.samuel@gmail.com']
8
+ spec.name = 'rspec-graphql_matchers'
9
+ spec.version = Rspec::GraphqlMatchers::VERSION
10
+ spec.authors = ['Samuel Brandão']
11
+ spec.email = ['gb.samuel@gmail.com']
11
12
 
12
- spec.summary = 'Collection of rspec matchers to test your graphQL api schema.'
13
- spec.homepage = 'https://github.com/khamusa/rspec-graphql_matchers'
14
- spec.license = 'MIT'
13
+ spec.summary = 'Collection of rspec matchers to test your graphQL api schema.'
14
+ spec.homepage = 'https://github.com/khamusa/rspec-graphql_matchers'
15
+ spec.license = 'MIT'
15
16
 
16
- # raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
17
+ # raise 'RubyGems 2.0 or newer is required to protect against public gem ' \
18
+ # 'pushes.'
17
19
 
18
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
- spec.bindir = 'exe'
20
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.files =
21
+ `git ls-files -z`
22
+ .split("\x0")
23
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
26
  spec.require_paths = ['lib']
22
27
 
23
- spec.add_runtime_dependency 'graphql', '>= 0.9', '< 2'
28
+ spec.add_runtime_dependency 'graphql', '>= 0.9', '< 2'
24
29
  spec.add_development_dependency 'bundler', '~> 1.12'
25
30
  spec.add_development_dependency 'rubocop', '~> 0'
26
31
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-graphql_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Brandão
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-25 00:00:00.000000000 Z
11
+ date: 2017-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql