Skip to content

feat: teach JR in tests to parse the response #1437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/jsonapi/resources/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ class Railtie < ::Rails::Railtie
rake_tasks do
load 'tasks/check_upgrade.rake'
end


initializer "jsonapi_resources.testing", after: :initialize do
next unless Rails.env.test?
# Make response.parsed_body work
ActionDispatch::IntegrationTest.register_encoder :api_json,
param_encoder: ->(params) {
params
},
response_parser: ->(body) {
JSONAPI::MimeTypes.parser.call(body)
}
end
end
end
end
4 changes: 2 additions & 2 deletions test/helpers/functional_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module FunctionalHelpers
# end
#
def json_response
JSON.parse(@response.body)
@response.parsed_body
end
end
end
end
10 changes: 5 additions & 5 deletions test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ def test_post_sessions
'Accept' => JSONAPI::MEDIA_TYPE
}
assert_jsonapi_response 201
json_body = JSON.parse(response.body)
json_body = response.parsed_body
session_id = json_body["data"]["id"]

# Get what we just created
get "/sessions/#{session_id}?include=responses"
assert_jsonapi_response 200
json_body = JSON.parse(response.body)
json_body = response.parsed_body

assert(json_body.is_a?(Object));
assert(json_body["included"].is_a?(Array));
assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]);

get "/sessions/#{session_id}?include=responses,responses.paragraph"
assert_jsonapi_response 200
json_body = JSON.parse(response.body)
json_body = response.parsed_body

assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]);

Expand Down Expand Up @@ -348,7 +348,7 @@ def test_post_polymorphic_with_has_many_relationship

assert_jsonapi_response 201

body = JSON.parse(response.body)
body = response.parsed_body
person = Person.find(body.dig("data", "id"))

assert_equal "Reo", person.name
Expand Down Expand Up @@ -649,7 +649,7 @@ def test_patch_polymorphic_with_has_many_relationship

assert_jsonapi_response 200

body = JSON.parse(response.body)
body = response.parsed_body
person = Person.find(body.dig("data", "id"))

assert_equal "Reo", person.name
Expand Down