Skip to content

Commit

Permalink
add unit tests for rails_context_if_not_already_rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Aug 19, 2024
1 parent eaebb79 commit c8861ad
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions spec/dummy/spec/helpers/react_on_rails_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class PlainReactOnRailsHelper
include ReactOnRailsHelper
include ActionView::Helpers::TagHelper
end

# rubocop:disable Metrics/BlockLength
Expand Down Expand Up @@ -345,5 +346,30 @@ class PlainReactOnRailsHelper
expect { ob.send(:rails_context, server_side: false) }.not_to raise_error
end
end

describe "#rails_context_if_not_already_rendered", :focus do
let(:helper) { PlainReactOnRailsHelper.new }

before do
allow(helper).to receive(:rails_context).and_return({ some: "context" })
end

it "returns a script tag with rails context when not already rendered" do
result = helper.send(:rails_context_if_not_already_rendered)
expect(result).to include('<script type="application/json" id="js-react-on-rails-context">')
expect(result).to include('"some":"context"')
end

it "returns an empty string when already rendered" do
helper.instance_variable_set(:@rendered_rails_context, true)
result = helper.send(:rails_context_if_not_already_rendered)
expect(result).to eq("")
end

it "calls rails_context with server_side: false" do
helper.send(:rails_context_if_not_already_rendered)
expect(helper).to have_received(:rails_context).with(server_side: false)
end
end
end
# rubocop:enable Metrics/BlockLength

0 comments on commit c8861ad

Please sign in to comment.