Skip to content

Commit

Permalink
Add (an admittedly contrived) failing spec for conditionally shared data
Browse files Browse the repository at this point in the history
  • Loading branch information
bknoles committed Apr 28, 2024
1 parent 0158d32 commit 01d32ae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class InertiaConditionalSharingController < ApplicationController
before_action :conditionally_share_props, only: [:show]
inertia_share normal_shared_prop: 1

def index
render inertia: 'EmptyTestComponent', props: {
index_only_prop: 1,
}
end

def show
render inertia: 'EmptyTestComponent', props: {
show_only_prop: 1,
}
end

protected

def conditionally_share_props
self.class.inertia_share conditionally_shared_show_prop: 1
end
end
3 changes: 3 additions & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@
get 'merge_instance_props' => 'inertia_merge_instance_props#merge_instance_props'

get 'lamda_shared_props' => 'inertia_lambda_shared_props#lamda_shared_props'

get 'conditional_share_index' => 'inertia_conditional_sharing#index'
get 'conditional_share_show' => 'inertia_conditional_sharing#show'
end
26 changes: 26 additions & 0 deletions spec/inertia/conditional_sharing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
RSpec.describe "conditionally shared data in a controller", type: :request do
it 'does not leak data between requests' do
get conditional_share_index_path, headers: {'X-Inertia' => true}
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
index_only_prop: 1,
normal_shared_prop: 1,
})

# NOTE: we actually have to run the show action twice since the new implementation
# sets up a before_action within a before_action to share the data.
# In effect, that means that the shared data isn't rendered until the second time the action is run.
get conditional_share_show_path, headers: {'X-Inertia' => true}
get conditional_share_show_path, headers: {'X-Inertia' => true}
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
normal_shared_prop: 1,
show_only_prop: 1,
conditionally_shared_show_prop: 1,
})

get conditional_share_index_path, headers: {'X-Inertia' => true}
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
index_only_prop: 1,
normal_shared_prop: 1,
})
end
end

0 comments on commit 01d32ae

Please sign in to comment.