|
| 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. |
1 | 3 | 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 |
14 | 6 | get conditional_share_show_path, headers: {'X-Inertia' => true} |
15 | 7 | 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 | + }) |
20 | 12 |
|
21 | 13 | 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 | + # InertiaSharedData isn't frozen until after the first time it's accessed. |
| 23 | + InertiaConditionalSharingController.send(:_inertia_shared_data) |
| 24 | + |
| 25 | + expect { |
| 26 | + get conditional_share_show_with_a_problem_path, headers: {'X-Inertia' => true} |
| 27 | + }.to raise_error(FrozenError) |
26 | 28 | end |
27 | 29 | end |
28 | 30 | end |
0 commit comments