Skip to content

Commit 077f5ad

Browse files
bknolesPedroAugustoRamalhoDuarte
authored andcommitted
conditionally shared data specs
1 parent 7c3152a commit 077f5ad

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class InertiaConditionalSharingController < ApplicationController
2+
before_action :conditionally_share_props, only: [:show]
3+
inertia_share normal_shared_prop: 1
4+
5+
def index
6+
render inertia: 'EmptyTestComponent', props: {
7+
index_only_prop: 1,
8+
}
9+
end
10+
11+
def show
12+
render inertia: 'EmptyTestComponent', props: {
13+
show_only_prop: 1,
14+
}
15+
end
16+
17+
protected
18+
19+
def conditionally_share_props
20+
self.class.inertia_share conditionally_shared_show_prop: 1
21+
end
22+
end

spec/dummy/config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@
4848
get 'initialize_session' => 'inertia_session_continuity_test#initialize_session'
4949
post 'submit_form_to_test_csrf' => 'inertia_session_continuity_test#submit_form_to_test_csrf'
5050
delete 'clear_session' => 'inertia_session_continuity_test#clear_session'
51+
52+
get 'conditional_share_index' => 'inertia_conditional_sharing#index'
53+
get 'conditional_share_show' => 'inertia_conditional_sharing#show'
5154
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
RSpec.describe "conditionally shared data in a controller", type: :request do
2+
it 'does not leak data between requests' do
3+
get conditional_share_index_path, headers: {'X-Inertia' => true}
4+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
5+
index_only_prop: 1,
6+
normal_shared_prop: 1,
7+
})
8+
9+
# NOTE: we actually have to run the show action twice since the new implementation
10+
# sets up a before_action within a before_action to share the data.
11+
# In effect, that means that the shared data isn't rendered until the second time the action is run.
12+
get conditional_share_show_path, headers: {'X-Inertia' => true}
13+
get conditional_share_show_path, headers: {'X-Inertia' => true}
14+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
15+
normal_shared_prop: 1,
16+
show_only_prop: 1,
17+
conditionally_shared_show_prop: 1,
18+
})
19+
20+
get conditional_share_index_path, headers: {'X-Inertia' => true}
21+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
22+
index_only_prop: 1,
23+
normal_shared_prop: 1,
24+
})
25+
end
26+
end

0 commit comments

Comments
 (0)