Skip to content

Commit 43a284c

Browse files
add unit tests for rails_context_if_not_already_rendered
1 parent 383fda1 commit 43a284c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

spec/dummy/spec/helpers/react_on_rails_helper_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class PlainReactOnRailsHelper
99
include ReactOnRailsHelper
10+
include ActionView::Helpers::TagHelper
1011
end
1112

1213
# rubocop:disable Metrics/BlockLength
@@ -365,5 +366,30 @@ class PlainReactOnRailsHelper
365366
expect { ob.send(:rails_context, server_side: false) }.not_to raise_error
366367
end
367368
end
369+
370+
describe "#rails_context_if_not_already_rendered", :focus do
371+
let(:helper) { PlainReactOnRailsHelper.new }
372+
373+
before do
374+
allow(helper).to receive(:rails_context).and_return({ some: "context" })
375+
end
376+
377+
it "returns a script tag with rails context when not already rendered" do
378+
result = helper.send(:rails_context_if_not_already_rendered)
379+
expect(result).to include('<script type="application/json" id="js-react-on-rails-context">')
380+
expect(result).to include('"some":"context"')
381+
end
382+
383+
it "returns an empty string when already rendered" do
384+
helper.instance_variable_set(:@rendered_rails_context, true)
385+
result = helper.send(:rails_context_if_not_already_rendered)
386+
expect(result).to eq("")
387+
end
388+
389+
it "calls rails_context with server_side: false" do
390+
helper.send(:rails_context_if_not_already_rendered)
391+
expect(helper).to have_received(:rails_context).with(server_side: false)
392+
end
393+
end
368394
end
369395
# rubocop:enable Metrics/BlockLength

0 commit comments

Comments
 (0)