Skip to content

Commit be7c56b

Browse files
committed
Update conditional sharing specs
1 parent 2309b58 commit be7c56b

File tree

4 files changed

+97
-31
lines changed

4 files changed

+97
-31
lines changed

lib/inertia_rails/action_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def match?(controller)
1818
#{@conditional_key.inspect} option.
1919
MSG
2020

21-
raise ActionNotFound.new(message, controller, missing_action)
21+
raise AbstractController::ActionNotFound.new(message, controller, missing_action)
2222
end
2323

2424
@actions.include?(controller.action_name)

spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ class InertiaConditionalSharingController < ApplicationController
88
end
99

1010
inertia_share only: :edit do
11-
{only_block_prop: 1}
11+
{edit_only_only_block_prop: 1}
1212
end
1313

1414
inertia_share except: [:show, :index] do
15-
{except_block_prop: 1}
15+
{edit_only_except_block_prop: 1}
1616
end
1717

1818
inertia_share if: -> { is_edit? } do
19-
{if_proc_prop: 1}
19+
{edit_only_if_proc_prop: 1}
2020
end
2121

2222
inertia_share unless: -> { !is_edit? } do
23-
{unless_proc_prop: 1}
23+
{edit_only_unless_proc_prop: 1}
2424
end
2525

26-
inertia_share({only_prop: 1}, only: :edit)
26+
inertia_share({edit_only_only_prop: 1}, only: :edit)
2727

28-
inertia_share({if_prop: 1}, if: [:is_edit?, -> { true }])
28+
inertia_share({edit_only_if_prop: 1}, if: [:is_edit?, -> { true }])
2929

30-
inertia_share({unless_prop: 1}, unless: :not_edit?)
30+
inertia_share({edit_only_unless_prop: 1}, unless: :not_edit?)
3131

32-
inertia_share({only_if_prop: 1}, only: :edit, if: -> { true })
32+
inertia_share({edit_only_only_if_prop: 1}, only: :edit, if: -> { true })
3333

34-
inertia_share({except_if_prop: 1}, except: [:index, :show], if: -> { true })
34+
inertia_share({edit_only_except_if_prop: 1}, except: [:index, :show], if: -> { true })
3535

3636
def index
3737
render inertia: 'EmptyTestComponent', props: {

spec/inertia/action_filter_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# spec/lib/inertia_rails/action_filter_spec.rb
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe InertiaRails::ActionFilter do
6+
let(:controller) do
7+
instance_double(
8+
'ActionController::Base',
9+
action_name: 'current_action',
10+
class: instance_double('Class', name: 'TestController')
11+
).tap do |stub|
12+
allow(stub).to receive(:available_action?).and_return(true)
13+
allow(stub).to receive(:available_action?).with('nonexistent').and_return(false)
14+
end
15+
end
16+
17+
describe '#match?' do
18+
context 'when action exists' do
19+
it 'returns true if action matches' do
20+
filter = described_class.new(:only, 'current_action')
21+
expect(filter.match?(controller)).to be true
22+
end
23+
24+
it 'returns false if action does not match' do
25+
filter = described_class.new(:only, 'other_action')
26+
expect(filter.match?(controller)).to be false
27+
end
28+
29+
it 'handles multiple actions' do
30+
filter = described_class.new(:only, %w[current_action other actions])
31+
expect(filter.match?(controller)).to be true
32+
end
33+
34+
it 'handles symbol actions' do
35+
filter = described_class.new(:only, :current_action)
36+
expect(filter.match?(controller)).to be true
37+
end
38+
end
39+
40+
context 'when action does not exist' do
41+
it 'raises ActionNotFound with appropriate message' do
42+
filter = described_class.new(:only, :nonexistent)
43+
expected_message = <<~MSG
44+
The nonexistent action could not be found for the :inertia_share
45+
callback on TestController, but it is listed in the controller's
46+
:only option.
47+
MSG
48+
49+
expect {
50+
filter.match?(controller)
51+
}.to raise_error(AbstractController::ActionNotFound, expected_message)
52+
end
53+
end
54+
end
55+
end

spec/inertia/conditional_sharing_spec.rb

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,43 @@
22
# but it can be done by referencing the action name in an inertia_share block.
33
RSpec.describe "conditionally shared data in a controller", type: :request do
44
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
6-
get conditional_share_show_path, headers: {'X-Inertia' => true}
7-
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
8-
normal_shared_prop: 1,
5+
let(:edit_only_props) do
6+
{
7+
edit_only_only_block_prop: 1,
8+
edit_only_except_block_prop: 1,
9+
edit_only_if_proc_prop: 1,
10+
edit_only_unless_proc_prop: 1,
11+
edit_only_only_prop: 1,
12+
edit_only_if_prop: 1,
13+
edit_only_unless_prop: 1,
14+
edit_only_only_if_prop: 1,
15+
edit_only_except_if_prop: 1,
16+
edit_only_prop: 1,
17+
}
18+
end
19+
20+
let(:show_only_props) do
21+
{
922
show_only_prop: 1,
1023
conditionally_shared_show_prop: 1,
11-
})
24+
}
25+
end
26+
27+
let(:index_only_props) do
28+
{
29+
index_only_prop: 1,
30+
}
31+
end
32+
33+
it "does not leak the data between requests" do
34+
get conditional_share_show_path, headers: {'X-Inertia' => true}
35+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq(show_only_props.merge(normal_shared_prop: 1))
1236

1337
get conditional_share_index_path, headers: {'X-Inertia' => true}
14-
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).not_to include({
15-
conditionally_shared_show_prop: 1,
16-
})
38+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq(index_only_props.merge(normal_shared_prop: 1))
39+
1740
get conditional_share_edit_path, headers: { 'X-Inertia' => true }
18-
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq({
19-
normal_shared_prop: 1,
20-
only_block_prop: 1,
21-
except_block_prop: 1,
22-
if_proc_prop: 1,
23-
unless_proc_prop: 1,
24-
only_prop: 1,
25-
if_prop: 1,
26-
unless_prop: 1,
27-
only_if_prop: 1,
28-
except_if_prop: 1,
29-
edit_only_prop: 1,
30-
})
41+
expect(JSON.parse(response.body)['props'].deep_symbolize_keys).to eq(edit_only_props.merge(normal_shared_prop: 1))
3142
end
3243
end
3344

0 commit comments

Comments
 (0)