Skip to content

Commit 56dfc76

Browse files
authored
Merge pull request #139 from inertiajs/add-specs-from-config-refactor
Add specs from config refactor
2 parents 15d0d2d + a887fcc commit 56dfc76

File tree

8 files changed

+52
-26
lines changed

8 files changed

+52
-26
lines changed

inertia_rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
66
spec.name = "inertia_rails"
77
spec.version = InertiaRails::VERSION
88
spec.authors = ["Brian Knoles", "Brandon Shar", "Eugene Granovsky"]
9-
spec.email = ["brain@bellawatt.com", "[email protected]", "[email protected]"]
9+
spec.email = ["brian@bellawatt.com", "[email protected]", "[email protected]"]
1010

1111
spec.summary = %q{Inertia adapter for Rails}
1212
spec.homepage = "https://github.com/inertiajs/inertia-rails"

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/app/controllers/inertia_share_test_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class InertiaShareTestController < ApplicationController
22
inertia_share name: 'Brandon'
33
inertia_share sport: -> { 'hockey' }
4+
inertia_share({a_hash: 'also works'})
45
inertia_share do
56
{
67
position: 'center',

spec/dummy/config/environments/test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
config.cache_store = :null_store
3030

3131
# Raise exceptions instead of rendering exception templates.
32-
config.action_dispatch.show_exceptions = :none
32+
config.action_dispatch.show_exceptions = false
3333

3434
# Disable request forgery protection in test environment.
3535
config.action_controller.allow_forgery_protection = false

spec/dummy/config/routes.rb

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

5353
get 'conditional_share_index' => 'inertia_conditional_sharing#index'
5454
get 'conditional_share_show' => 'inertia_conditional_sharing#show'
55+
get 'conditional_share_show_with_a_problem' => 'inertia_conditional_sharing#show_with_a_problem'
5556
end
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
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+
# 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)
2628
end
2729
end
2830
end

spec/inertia/request_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,12 @@
154154
end
155155
end
156156
end
157+
158+
describe 'a non existent route' do
159+
it 'raises a 404 exception' do
160+
expect {
161+
get '/non_existent_route', headers: { 'X-Inertia' => true }
162+
}.to raise_error(ActionController::RoutingError, /No route matches/)
163+
end
164+
end
157165
end

spec/inertia/sharing_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
subject { JSON.parse(response.body)['props'].deep_symbolize_keys }
33

44
context 'using inertia share' do
5-
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29} }
5+
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29, a_hash: 'also works'} }
66
before { get share_path, headers: {'X-Inertia' => true} }
77

88
it { is_expected.to eq props }
@@ -18,7 +18,7 @@
1818
end
1919

2020
context 'using inertia share in subsequent requests' do
21-
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29} }
21+
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29, a_hash: 'also works'} }
2222

2323
before do
2424
get share_path, headers: {'X-Inertia' => true}
@@ -29,7 +29,7 @@
2929
end
3030

3131
context 'using inertia share with inheritance' do
32-
let(:props) { {name: 'No Longer Brandon', sport: 'hockey', position: 'center', number: 29} }
32+
let(:props) { {name: 'No Longer Brandon', sport: 'hockey', position: 'center', number: 29, a_hash: 'also works'} }
3333

3434
before do
3535
get share_with_inherited_path, headers: {'X-Inertia' => true}
@@ -39,7 +39,7 @@
3939
end
4040

4141
context 'with errors' do
42-
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29} }
42+
let(:props) { {name: 'Brandon', sport: 'hockey', position: 'center', number: 29, a_hash: 'also works'} }
4343
let(:errors) { 'rearview mirror is present' }
4444
before {
4545
allow_any_instance_of(ActionDispatch::Request).to receive(:session) {

0 commit comments

Comments
 (0)