Skip to content

Commit e25dc6b

Browse files
committed
Rewrite the conditional sharing specs to document the behavior of frozen shared data
1 parent 93a0034 commit e25dc6b

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class InertiaConditionalSharingController < ApplicationController
2+
before_action :conditionally_share_a_prop, only: :show_with_a_problem
3+
24
inertia_share normal_shared_prop: 1
35

46
inertia_share do
@@ -16,4 +18,16 @@ def show
1618
show_only_prop: 1,
1719
}
1820
end
21+
22+
def show_with_a_problem
23+
render inertia: 'EmptyTestComponent', props: {
24+
show_only_prop: 1,
25+
}
26+
end
27+
28+
protected
29+
30+
def conditionally_share_a_prop
31+
self.class.inertia_share incorrectly_conditionally_shared_prop: 1
32+
end
1933
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050

5151
get 'conditional_share_index' => 'inertia_conditional_sharing#index'
5252
get 'conditional_share_show' => 'inertia_conditional_sharing#show'
53+
get 'conditional_share_show_with_a_problem' => 'inertia_conditional_sharing#show_with_a_problem'
5354
end
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1+
# Specs as documentation. Per-action shared data isn't explicity supported,
2+
# but it can be done by referencing the action name in an inertia_share block.
13
RSpec.describe "conditionally shared data in a controller", type: :request do
2-
context "when there is conditional data inside inertia_share" do
3-
it "does not leak data between requests" do
4-
get conditional_share_index_path, headers: {'X-Inertia' => true}
5-
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
6-
index_only_prop: 1,
7-
normal_shared_prop: 1,
8-
})
9-
10-
# NOTE: we actually have to run the show action twice since the new implementation
11-
# sets up a before_action within a before_action to share the data.
12-
# In effect, that means that the shared data isn't rendered until the second time the action is run.
13-
get conditional_share_show_path, headers: {'X-Inertia' => true}
4+
context "when there is data inside inertia_share only applicable to a single action" do
5+
it "does not leak the data between requests" do
146
get conditional_share_show_path, headers: {'X-Inertia' => true}
157
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
16-
normal_shared_prop: 1,
17-
show_only_prop: 1,
18-
conditionally_shared_show_prop: 1,
19-
})
8+
normal_shared_prop: 1,
9+
show_only_prop: 1,
10+
conditionally_shared_show_prop: 1,
11+
})
2012

2113
get conditional_share_index_path, headers: {'X-Inertia' => true}
22-
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
23-
index_only_prop: 1,
24-
normal_shared_prop: 1,
25-
})
14+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).not_to include({
15+
conditionally_shared_show_prop: 1,
16+
})
17+
end
18+
end
19+
20+
context "when there is conditional data shared via before_action" do
21+
it "raises an error because it is frozen" do
22+
expect {
23+
get conditional_share_show_with_a_problem_path, headers: {'X-Inertia' => true}
24+
}.to raise_error(FrozenError)
2625
end
2726
end
2827
end

0 commit comments

Comments
 (0)