Skip to content

Commit

Permalink
Merge pull request #9 from testdouble/migrate--have_operation-helper
Browse files Browse the repository at this point in the history
`have_operation` ->  `have_field`
  • Loading branch information
mxriverlynn authored Apr 15, 2021
2 parents d4c4736 + 724202c commit a227d78
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rspec-graphql_response (0.4.1)
rspec-graphql_response (0.5.0)
graphql (>= 1.0)
rspec (>= 3.0)

Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Configuration:
Custom Matchers:

- [have_errors](/docs/have_errors.md) - validates errors, or lack of, on the GraphQL response
- [have_operation](/docs/have_operation.md) - validates the presence of a specified graphql operation in the graphql response
- [have_field](/docs/have_field.md) - validates the presence of a specified graphql operation in the graphql response

Context / Describe Helper Methods:

Expand Down Expand Up @@ -162,11 +162,11 @@ RSpec.describe Some::Thing, type: :graphql do
end
```

#### Retrieve operation results with `operation`
#### Retrieve response results with `response_data`

Now that the GraphQL query has been executed and a response has been obtained, it's time to check for the results of a GraphQL
operation. In the previous example, the spec is expecting to find `data` with `characters` in the response hash. To reduce the
nested hash checking, use the built-in `operation` method to retrieve the `characters`:
nested hash checking, use the built-in `response_data` method to retrieve the `characters`:

```ruby
RSpec.describe Some::Thing, type: :graphql do
Expand All @@ -180,18 +180,17 @@ RSpec.describe Some::Thing, type: :graphql do
GQL

it "executes the query" do
characters = operation(:characters)

expect(characters).to include(
expect(response_data :characters).to include(
# ...
)
end
end
```

Note the lack of `response` use here. Internally, the `operation` method uses the `response` to obtain the data requested. This
Note the lack of `response` use here. Internally, the `response_data` method uses the `response` to obtain the data requested. This
means the entire chain of operations from executing the GraphQL request, to converting the response into a hash, and digging
through the results to find the correction operation, has been handled behind the scenes.
through the results to find the correction operation, has been handled behind the scenes. To see more examples of how to use
`response_data` dig through your response check out it's full documenation [here.](/docs/response_data.md)

## Development

Expand Down
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Release notes for various versions of RSpec::GraphQLResponse

See [the upgrade guide](/UPGRADE.md) for details on changes between versions and how to upgrade.

## v0.5.0 - Helper API change

- Fully deprecates `operation`.
- Renames `have_operation` to `have_field` to clarify its use.

## v0.4.0 - Helper API change

### Breaking Changes
Expand Down
6 changes: 3 additions & 3 deletions docs/have_operation.md → docs/have_field.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Validate a response operation with `have_operation(name)`
# Validate a response operation with `have_field(name)`

Check for the presence of an operation's results. Useful when you want to ensure an operation exists before
retrieving the operation's results.
Expand All @@ -18,7 +18,7 @@ RSpec.describe My::Characters, type: :graphql do

it "has the characters" do

expect(response).to have_operation(:characters)
expect(response).to have_field(:characters)

end
end
Expand All @@ -39,7 +39,7 @@ RSpec.describe My::Characters, type: :graphql do

it "does not have books" do

expect(response).to_not have_operation(:books)
expect(response).to_not have_field(:books)

end
end
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/graphql_response/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ def self.add_helper(name, scope: :spec, &helper)

# spec level helpers
require_relative "helpers/execute_graphql"
require_relative "helpers/operation"
require_relative "helpers/response"
require_relative "helpers/response_data"
6 changes: 0 additions & 6 deletions lib/rspec/graphql_response/helpers/operation.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rspec/graphql_response/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def self.add_matcher(name, &matcher)
end

require_relative "matchers/have_errors"
require_relative "matchers/have_operation"
require_relative "matchers/have_field"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RSpec::GraphQLResponse.add_matcher :have_operation do |operation_name|
RSpec::GraphQLResponse.add_matcher :have_field do |operation_name|
match do |response|
validator = RSpec::GraphQLResponse.validator(:have_operation)
validator = RSpec::GraphQLResponse.validator(:have_field)

@result = validator.validate(response, operation_name: operation_name)
@result.valid?
Expand All @@ -11,7 +11,7 @@
end

match_when_negated do |response|
validator = RSpec::GraphQLResponse.validator(:have_operation)
validator = RSpec::GraphQLResponse.validator(:have_field)

@result = validator.validate_negated(response, operation_name: operation_name)
@result.valid?
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/graphql_response/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def self.validator(name)
end

require_relative "validators/have_errors"
require_relative "validators/have_operation"
require_relative "validators/have_field"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec::GraphQLResponse.add_validator :have_operation do
RSpec::GraphQLResponse.add_validator :have_field do
failure_message :nil, "Cannot evaluate operations on nil"
failure_message :not_found, ->(expected, actual) { "Expected to find operation result named #{expected}, but did not find it\n\t#{actual}" }

Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/graphql_response/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module RSpec
module GraphQLResponse
VERSION = "0.4.1"
VERSION = "0.5.0"
end
end
31 changes: 0 additions & 31 deletions spec/graphql_response/helpers/operation_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe RSpec::GraphQLResponse, "matcher#have_operation", type: :graphql do
RSpec.describe RSpec::GraphQLResponse, "matcher#have_field", type: :graphql do
graphql_operation <<-GQL
query CharacterList {
characters {
Expand All @@ -12,14 +12,14 @@
it "is valid" do
expect(response).to_not have_errors

expect(response).to have_operation(:characters)
expect(response).to have_field(:characters)
end

it "is invalid when negated" do
expect(response).to_not have_errors

expect {
expect(response).to_not have_operation(:characters)
expect(response).to_not have_field(:characters)
}.to raise_error(/Expected not to find operation result named characters/)
end
end
Expand All @@ -29,14 +29,14 @@
expect(response).to_not have_errors

expect {
expect(response).to have_operation(:operation_not_present)
expect(response).to have_field(:operation_not_present)
}.to raise_error(/Expected to find operation result named operation_not_present/)
end

it "is valid when negated" do
expect(response).to_not have_errors

expect(response).to_not have_operation(:operation_not_present)
expect(response).to_not have_field(:operation_not_present)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe RSpec::GraphQLResponse, "#have_operation validator", type: :graphql do
RSpec.describe RSpec::GraphQLResponse, "#have_field validator", type: :graphql do
graphql_operation <<-GQL
query CharacterList {
characters {
Expand All @@ -11,7 +11,7 @@
let(:operation_name) { nil }

subject(:result) do
validator = RSpec::GraphQLResponse.validator :have_operation
validator = RSpec::GraphQLResponse.validator :have_field
validator.validate_negated(response, operation_name: operation_name)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe RSpec::GraphQLResponse, "#have_operation validator", type: :graphql do
RSpec.describe RSpec::GraphQLResponse, "#have_field validator", type: :graphql do
graphql_operation <<-GQL
query CharacterList {
characters {
Expand All @@ -11,7 +11,7 @@
let(:operation_name) { nil }

subject(:result) do
validator = RSpec::GraphQLResponse.validator :have_operation
validator = RSpec::GraphQLResponse.validator :have_field
validator.validate(response, operation_name: operation_name)
end

Expand Down

0 comments on commit a227d78

Please sign in to comment.