Skip to content

Commit 052b608

Browse files
authored
Merge pull request #105 from inertiajs/broken-have-exact-props
Fix `have_exact_props` RSpec matcher.
2 parents b189200 + 49212c8 commit 052b608

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

lib/inertia_rails/inertia_rails.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def self.configure
1414
# "Getters"
1515
def self.shared_data(controller)
1616
shared_plain_data.
17-
merge!(evaluated_blocks(controller, shared_blocks)).
18-
with_indifferent_access
17+
merge!(evaluated_blocks(controller, shared_blocks))
1918
end
2019

2120
def self.version

lib/inertia_rails/renderer.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(component, controller, request, response, render_method, props: n
1212
@request = request
1313
@response = response
1414
@render_method = render_method
15-
@props = props ? props.with_indifferent_access : controller.inertia_view_assigns.with_indifferent_access
15+
@props = props ? props : controller.inertia_view_assigns
1616
@view_data = view_data || {}
1717
@deep_merge = !deep_merge.nil? ? deep_merge : InertiaRails.deep_merge_shared_data?
1818
end
@@ -43,15 +43,25 @@ def layout
4343
end
4444

4545
def computed_props
46-
_props = ::InertiaRails.shared_data(@controller).send(prop_merge_method, @props).select do |key, prop|
46+
# Cast props to symbol keyed hash before merging so that we have a consistent data structure and
47+
# avoid duplicate keys after merging.
48+
#
49+
# Functionally, this permits using either string or symbol keys in the controller. Since the results
50+
# is cast to json, we should treat string/symbol keys as identical.
51+
_props = ::InertiaRails.shared_data(@controller).deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
4752
if rendering_partial_component?
48-
key.to_sym.in? partial_keys
53+
key.in? partial_keys
4954
else
5055
!prop.is_a?(InertiaRails::Lazy)
5156
end
5257
end
5358

54-
deep_transform_values(_props, lambda {|prop| prop.respond_to?(:call) ? @controller.instance_exec(&prop) : prop }).with_indifferent_access
59+
deep_transform_values(
60+
_props,
61+
lambda do |prop|
62+
prop.respond_to?(:call) ? @controller.instance_exec(&prop) : prop
63+
end
64+
)
5565
end
5666

5767
def page

lib/inertia_rails/rspec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def inertia_tests_setup?
7474

7575
RSpec::Matchers.define :have_exact_props do |expected_props|
7676
match do |inertia|
77-
expect(inertia.props).to eq expected_props.with_indifferent_access
77+
# Computed props have symbolized keys.
78+
expect(inertia.props).to eq expected_props.deep_symbolize_keys
7879
end
7980

8081
failure_message do |inertia|
@@ -84,7 +85,8 @@ def inertia_tests_setup?
8485

8586
RSpec::Matchers.define :include_props do |expected_props|
8687
match do |inertia|
87-
expect(inertia.props).to include expected_props.with_indifferent_access
88+
# Computed props have symbolized keys.
89+
expect(inertia.props).to include expected_props.deep_symbolize_keys
8890
end
8991

9092
failure_message do |inertia|
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class InertiaLambdaSharedPropsController < ApplicationController
2+
inertia_share someProperty: -> {
3+
{
4+
property_a: "some value",
5+
property_b: "this value"
6+
}
7+
}
8+
9+
def lamda_shared_props
10+
render inertia: 'ShareTestComponent', props: { property_c: "some other value" }
11+
end
12+
end

spec/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@
3939
get 'deep_merge_shared' => 'inertia_merge_shared#deep_merge_shared'
4040
get 'shallow_merge_shared' => 'inertia_merge_shared#shallow_merge_shared'
4141
get 'merge_instance_props' => 'inertia_merge_instance_props#merge_instance_props'
42+
43+
get 'lamda_shared_props' => 'inertia_lambda_shared_props#lamda_shared_props'
4244
end

spec/inertia/rspec_helper_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,20 @@ def puts(thing)
114114
end
115115
end
116116
end
117+
118+
describe '.have_exact_props' do
119+
context 'when shared props are wrapped in a callable', inertia: true do
120+
it 'compares props with either string or symbol keys' do
121+
get lamda_shared_props_path
122+
123+
expect_inertia.to have_exact_props({
124+
someProperty: {
125+
property_a: "some value",
126+
'property_b' => "this value",
127+
},
128+
property_c: "some other value"
129+
})
130+
end
131+
end
132+
end
117133
end

0 commit comments

Comments
 (0)