rspec-graphql_matchers 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -3
- data/CHANGELOG.md +34 -0
- data/README.md +38 -25
- data/lib/rspec/graphql_matchers/have_a_field.rb +49 -0
- data/lib/rspec/graphql_matchers/matchers.rb +7 -1
- data/lib/rspec/graphql_matchers/types_helper.rb +2 -0
- data/lib/rspec/graphql_matchers/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeaf0169ad7a9777dcd7391a7bc3572183d26156
|
4
|
+
data.tar.gz: e0f63135573cc773951fb2ffcae9870257cee9f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99166b1bad15d1a1b8cf11dfbc5a7f73a52597b34dbc4fc09111761f848974bd664ca95aaf659c9020d0fc22d0a7833fb45f8bbd9a3df07016e1e28b60b3bb1f
|
7
|
+
data.tar.gz: 18a35b6bb8e5c8f56ca7a2b3b8f75cf95350a29b805589b665fa38d2514b6e378964a71ed56ba9e8a59327aef622afa7c0393f5a8b8b79bec1afaa6def0ecba7
|
data/.codeclimate.yml
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
---
|
2
2
|
engines:
|
3
|
-
bundler-audit:
|
4
|
-
enabled: true
|
5
3
|
duplication:
|
6
4
|
enabled: true
|
7
5
|
config:
|
@@ -16,7 +14,6 @@ engines:
|
|
16
14
|
enabled: true
|
17
15
|
ratings:
|
18
16
|
paths:
|
19
|
-
- Gemfile.lock
|
20
17
|
- "**.inc"
|
21
18
|
- "**.js"
|
22
19
|
- "**.jsx"
|
@@ -25,6 +22,7 @@ ratings:
|
|
25
22
|
- "**.py"
|
26
23
|
- "**.rb"
|
27
24
|
exclude_paths:
|
25
|
+
- Gemfile.lock
|
28
26
|
- spec/
|
29
27
|
- rspec-graphql_matchers.gemspec
|
30
28
|
- bin/
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
### Breaking changes
|
4
|
+
|
5
|
+
### Deprecations
|
6
|
+
|
7
|
+
### New features
|
8
|
+
|
9
|
+
### Bug fixes
|
10
|
+
|
11
|
+
## 0.3.0 (Sep 16, 2016)
|
12
|
+
|
13
|
+
### Breaking changes
|
14
|
+
|
15
|
+
### Deprecations
|
16
|
+
|
17
|
+
### New features
|
18
|
+
|
19
|
+
- New matcher have_field(field_name).of_type(type) for testing types and their fields
|
20
|
+
|
21
|
+
### Bug fixes
|
22
|
+
|
23
|
+
## 0.2.0 Initial public release
|
24
|
+
|
25
|
+
### Breaking changes
|
26
|
+
|
27
|
+
### Deprecations
|
28
|
+
|
29
|
+
### New features
|
30
|
+
|
31
|
+
- New matcher be_of_type for testing fields
|
32
|
+
- New matcher accept_arguments for testing fields
|
33
|
+
|
34
|
+
### Bug fixes
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rspec::GraphqlMatchers
|
2
2
|
|
3
|
-
Convenient rspec matchers for testing your (
|
3
|
+
Convenient rspec matchers for testing your [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) API/Schema.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -10,17 +10,19 @@ gem 'rspec-graphql_matchers'
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
The
|
14
|
-
-
|
15
|
-
-
|
13
|
+
The matchers currently supported are:
|
14
|
+
- `expect(graphql_type).to have_a_field(field_name).that_returns(valid_type)`
|
15
|
+
- `expect(graphql_field).to be_of_type(valid_type)`
|
16
|
+
- `expect(graphql_field).to accept_arguments(hash_of_arg_names_and_valid_types)`
|
16
17
|
|
17
|
-
Where a type
|
18
|
-
- A GraphQL
|
18
|
+
Where a valid type for the expectation is either:
|
19
|
+
- A `GraphQL::ObjectType` object (ex: `types.String`, `!types.Int`, `types[types.Int]`, or your own)
|
19
20
|
- A String representation of a type: `"String!"`, `"Int!"`, `"[String]!"`
|
21
|
+
(note the exclamation mark at the end, as required by the [GraphQL specs](http://graphql.org/).
|
20
22
|
|
21
23
|
## Examples
|
22
24
|
|
23
|
-
Given a GraphQL
|
25
|
+
Given a `GraphQL::ObjectType` defined as
|
24
26
|
|
25
27
|
```ruby
|
26
28
|
|
@@ -41,17 +43,27 @@ PostType = GraphQL::ObjectType.define do
|
|
41
43
|
end
|
42
44
|
```
|
43
45
|
|
44
|
-
### 1) Test
|
46
|
+
### 1) Test your type defines the correct fields:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
describe PostType do
|
50
|
+
it 'defines a field id of type ID!' do
|
51
|
+
expect(subject).to have_field(:id).that_returns(!types.ID)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Fluent alternatives
|
55
|
+
it { is_expected.to have_field(:id).of_type("ID!") }
|
56
|
+
it { is_expected.to have_a_field(:id).returning("ID!") }
|
57
|
+
end
|
58
|
+
```
|
59
|
+
### 2) Test a specific field type with `be_of_type` matcher:
|
45
60
|
|
46
61
|
```ruby
|
47
62
|
describe PostType do
|
48
63
|
describe 'id' do
|
49
64
|
subject { PostType.fields['id'] }
|
50
65
|
|
51
|
-
# These will match
|
52
66
|
it { is_expected.to be_of_type('ID!') }
|
53
|
-
|
54
|
-
# While 'Float!' will not match
|
55
67
|
it { is_expected.not_to be_of_type('Float!') }
|
56
68
|
end
|
57
69
|
|
@@ -74,10 +86,9 @@ type name (`Post`) and not the constant name (`PostType`).
|
|
74
86
|
|
75
87
|
Using your type objects directly has the advantage that if you
|
76
88
|
decide to rename the type your specs won't break, as they would had you
|
77
|
-
hardcoded
|
78
|
-
|
79
|
-
You can also use the built-in GraphQL scalar types:
|
89
|
+
hardcoded it as a String.
|
80
90
|
|
91
|
+
You can also use the built-in [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) scalar types:
|
81
92
|
|
82
93
|
```ruby
|
83
94
|
# ensure you have the GraphQL type definer available in your tests
|
@@ -94,14 +105,10 @@ end
|
|
94
105
|
|
95
106
|
Having to define `types` everywhere is quite annoying. If you prefer, you can
|
96
107
|
just `include RSpec::GraphqlMatchers::TypesHelper` once
|
97
|
-
(for example in your `spec_helper.rb
|
98
|
-
and the `types` shortcut will be available
|
99
|
-
Use at your own risk.
|
100
|
-
|
101
|
-
### 2) Test the arguments accepted by a field with `accept_arguments` matcher:
|
108
|
+
(for example in your `spec_helper.rb`)
|
109
|
+
and the `types` shortcut will be available within the include context.
|
102
110
|
|
103
|
-
|
104
|
-
keys represent the attribute name and values respresent the attribute type.
|
111
|
+
### 3) Test the arguments accepted by a field with `accept_arguments` matcher:
|
105
112
|
|
106
113
|
```ruby
|
107
114
|
describe PostType do
|
@@ -121,13 +128,19 @@ describe PostType do
|
|
121
128
|
end
|
122
129
|
end
|
123
130
|
```
|
124
|
-
The spec will pass only if all attributes/types specified in the hash are
|
125
|
-
defined on the field. Type specification follows the same rules from
|
126
|
-
`be_of_type` matcher.
|
127
131
|
|
128
|
-
|
132
|
+
The spec will only pass if all attributes/types specified in the hash are
|
133
|
+
defined on the field.
|
134
|
+
|
135
|
+
For better fluency, `accept_arguments` is also available in singular form, as
|
129
136
|
`accept_argument`.
|
130
137
|
|
138
|
+
## TODO
|
139
|
+
|
140
|
+
- Setup CI and integrate w/codeclimate
|
141
|
+
- Setup codeclimate / CI badges
|
142
|
+
- New matchers!
|
143
|
+
|
131
144
|
## Contributing
|
132
145
|
|
133
146
|
- Send Bug reports, suggestions or any general
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module RSpec
|
2
|
+
module GraphqlMatchers
|
3
|
+
class HaveAField
|
4
|
+
def initialize(field_name)
|
5
|
+
@field_name = field_name.to_s
|
6
|
+
@field_type = @graph_object = nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(graph_object)
|
10
|
+
@graph_object = graph_object
|
11
|
+
|
12
|
+
actual_field = @graph_object.fields[@field_name]
|
13
|
+
actual_field && types_match?(@field_type, actual_field.type)
|
14
|
+
end
|
15
|
+
|
16
|
+
def that_returns(field_type)
|
17
|
+
@field_type = field_type
|
18
|
+
|
19
|
+
self
|
20
|
+
end
|
21
|
+
alias returning that_returns
|
22
|
+
alias of_type that_returns
|
23
|
+
|
24
|
+
def failure_message
|
25
|
+
"expected #{describe_obj(@graph_object)} to #{description}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def description
|
29
|
+
"define field `#{@field_name}`" + of_type_description
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def of_type_description
|
35
|
+
return '' unless @field_type
|
36
|
+
|
37
|
+
" of type `#{@field_type}`"
|
38
|
+
end
|
39
|
+
|
40
|
+
def types_match?(expected_type, actual_type)
|
41
|
+
!expected_type || expected_type.to_s == actual_type.to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
def describe_obj(field)
|
45
|
+
field.respond_to?(:name) && field.name || field.inspect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rspec/matchers'
|
2
2
|
require 'rspec/graphql_matchers/be_of_type'
|
3
3
|
require 'rspec/graphql_matchers/accept_arguments'
|
4
|
+
require 'rspec/graphql_matchers/have_a_field'
|
4
5
|
|
5
6
|
module RSpec
|
6
7
|
module Matchers
|
@@ -11,7 +12,12 @@ module RSpec
|
|
11
12
|
def accept_arguments(expected_args)
|
12
13
|
RSpec::GraphqlMatchers::AcceptArguments.new(expected_args)
|
13
14
|
end
|
14
|
-
|
15
15
|
alias accept_argument accept_arguments
|
16
|
+
|
17
|
+
# rubocop:disable Style/PredicateName
|
18
|
+
def have_a_field(field_name)
|
19
|
+
RSpec::GraphqlMatchers::HaveAField.new(field_name)
|
20
|
+
end
|
21
|
+
alias have_field have_a_field
|
16
22
|
end
|
17
23
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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: 2016-09-
|
11
|
+
date: 2016-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- ".rspec"
|
100
100
|
- ".rubocop.yml"
|
101
101
|
- ".travis.yml"
|
102
|
+
- CHANGELOG.md
|
102
103
|
- CODE_OF_CONDUCT.md
|
103
104
|
- Gemfile
|
104
105
|
- LICENSE.txt
|
@@ -109,6 +110,7 @@ files:
|
|
109
110
|
- lib/rspec/graphql_matchers.rb
|
110
111
|
- lib/rspec/graphql_matchers/accept_arguments.rb
|
111
112
|
- lib/rspec/graphql_matchers/be_of_type.rb
|
113
|
+
- lib/rspec/graphql_matchers/have_a_field.rb
|
112
114
|
- lib/rspec/graphql_matchers/matchers.rb
|
113
115
|
- lib/rspec/graphql_matchers/types_helper.rb
|
114
116
|
- lib/rspec/graphql_matchers/version.rb
|
@@ -133,9 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
135
|
version: '0'
|
134
136
|
requirements: []
|
135
137
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
138
|
+
rubygems_version: 2.4.8
|
137
139
|
signing_key:
|
138
140
|
specification_version: 4
|
139
141
|
summary: Collection of rspec matchers to test your graphQL api schema.
|
140
142
|
test_files: []
|
141
|
-
has_rdoc:
|