Skip to content
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

Use to_json when rendering json response #181

Merged
merged 2 commits into from
Jan 12, 2025
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
2 changes: 1 addition & 1 deletion lib/inertia_rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def render
end
if @request.headers['X-Inertia']
@response.set_header('X-Inertia', 'true')
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
@render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json]
else
return render_ssr if configuration.ssr_enabled rescue nil
@render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page)
Expand Down
11 changes: 5 additions & 6 deletions lib/inertia_rails/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def set_values(params)
else
# Sequential Inertia request
@view_data = {}
@props = params[:json][:props]
@component = params[:json][:component]
json = JSON.parse(params[:json])
@props = json["props"]
@component = json["component"]
end
end
end
Expand Down Expand Up @@ -81,8 +82,7 @@ def inertia_tests_setup?

RSpec::Matchers.define :have_exact_props do |expected_props|
match do |inertia|
# Computed props have symbolized keys.
expect(inertia.props).to eq expected_props.deep_symbolize_keys
expect(inertia.props).to eq expected_props
end

failure_message do |inertia|
Expand All @@ -92,8 +92,7 @@ def inertia_tests_setup?

RSpec::Matchers.define :include_props do |expected_props|
match do |inertia|
# Computed props have symbolized keys.
expect(inertia.props).to include expected_props.deep_symbolize_keys
expect(inertia.props).to include expected_props
end

failure_message do |inertia|
Expand Down
4 changes: 2 additions & 2 deletions spec/inertia/rails_mimic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
it 'has the props' do
get instance_props_test_path

expect_inertia.to have_exact_props({'name' => 'Brandon', 'sport' => 'hockey'})
expect_inertia.to have_exact_props({name: 'Brandon', sport: 'hockey'})
end
end

Expand All @@ -31,7 +31,7 @@
get default_render_test_path

expect_inertia.to render_component('inertia_rails_mimic/default_render_test')
expect_inertia.to include_props({'name' => 'Brian'})
expect_inertia.to include_props({name: 'Brian'})
end

context 'a rendering transformation is provided' do
Expand Down
8 changes: 4 additions & 4 deletions spec/inertia/rspec_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def puts(thing)
before { get props_path, headers: {'X-Inertia': true} }

it 'has props' do
expect_inertia.to have_exact_props({name: 'Brandon', sport: 'hockey'})
expect_inertia.to have_exact_props({"name" => 'Brandon', "sport" => 'hockey'})
end

it 'includes props' do
expect_inertia.to include_props({sport: 'hockey'})
expect_inertia.to include_props({"sport" => 'hockey'})
end

it 'can retrieve props' do
expect(inertia.props[:name]).to eq 'Brandon'
expect(inertia.props["name"]).to eq 'Brandon'
end
end

Expand Down Expand Up @@ -139,7 +139,7 @@ def puts(thing)
expect_inertia.to have_exact_props({
someProperty: {
property_a: "some value",
'property_b' => "this value",
property_b: "this value",
},
property_c: "some other value"
})
Expand Down
Loading