Skip to content

Commit 765cde1

Browse files
Add tests for Pro and open source attribution comments in rendered output
1 parent 58d680e commit 765cde1

File tree

2 files changed

+173
-1
lines changed

2 files changed

+173
-1
lines changed

react_on_rails_pro/spec/dummy/spec/helpers/react_on_rails_pro_helper_spec.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def render_to_string(*args); end
1313
def response; end
1414
end
1515

16-
describe ReactOnRailsProHelper, type: :helper do
16+
describe ReactOnRailsProHelper do
1717
# In order to test the pro helper, we need to load the methods from the regular helper.
1818
# I couldn't see any easier way to do this.
1919
include ReactOnRails::Helper
@@ -695,4 +695,45 @@ def render_cached_random_value(cache_key)
695695
end
696696
end
697697
end
698+
699+
describe "attribution comment in stream_react_component" do
700+
include StreamingTestHelpers
701+
702+
let(:component_name) { "TestComponent" }
703+
let(:props) { { test: "data" } }
704+
let(:component_options) { { prerender: true, id: "#{component_name}-react-component-0" } }
705+
let(:chunks) do
706+
[
707+
{ html: "<div>Test Content</div>", consoleReplayScript: "" }
708+
]
709+
end
710+
711+
before do
712+
@rorp_rendering_fibers = []
713+
ReactOnRailsPro::Request.instance_variable_set(:@connection, nil)
714+
original_httpx_plugin = HTTPX.method(:plugin)
715+
allow(HTTPX).to receive(:plugin) do |*args|
716+
original_httpx_plugin.call(:mock_stream).plugin(*args)
717+
end
718+
clear_stream_mocks
719+
720+
mock_streaming_response(%r{http://localhost:3800/bundles/[a-f0-9]{32}-test/render/[a-f0-9]{32}}, 200,
721+
count: 1) do |yielder|
722+
chunks.each do |chunk|
723+
yielder.call("#{chunk.to_json}\n")
724+
end
725+
end
726+
end
727+
728+
it "includes the Pro attribution comment in the rendered output" do
729+
result = stream_react_component(component_name, props: props, **component_options)
730+
expect(result).to include("<!-- Powered by React on Rails Pro (c) ShakaCode")
731+
end
732+
733+
it "includes the attribution comment only once" do
734+
result = stream_react_component(component_name, props: props, **component_options)
735+
comment_count = result.scan("<!-- Powered by React on Rails Pro").length
736+
expect(comment_count).to eq(1)
737+
end
738+
end
698739
end

spec/dummy/spec/helpers/react_on_rails_helper_spec.rb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,5 +674,136 @@ def self.pro_attribution_comment; end
674674
end
675675
end
676676
end
677+
678+
describe "attribution comment inclusion in rendered output" do
679+
let(:props) { { name: "Test" } }
680+
681+
before do
682+
allow(SecureRandom).to receive(:uuid).and_return(0)
683+
end
684+
685+
describe "#react_component" do
686+
context "when React on Rails Pro is installed" do
687+
before do
688+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true)
689+
allow(ReactOnRailsPro::Utils).to receive(:pro_attribution_comment)
690+
.and_return("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
691+
end
692+
693+
it "includes the Pro attribution comment in the rendered output" do
694+
result = react_component("App", props: props)
695+
expect(result).to include("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
696+
end
697+
698+
it "includes the attribution comment only once" do
699+
result = react_component("App", props: props)
700+
comment_count = result.scan("<!-- Powered by React on Rails Pro").length
701+
expect(comment_count).to eq(1)
702+
end
703+
end
704+
705+
context "when React on Rails Pro is NOT installed" do
706+
before do
707+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
708+
end
709+
710+
it "includes the open source attribution comment in the rendered output" do
711+
result = react_component("App", props: props)
712+
expect(result).to include("<!-- Powered by React on Rails (c) ShakaCode | Open Source -->")
713+
end
714+
715+
it "includes the attribution comment only once" do
716+
result = react_component("App", props: props)
717+
comment_count = result.scan("<!-- Powered by React on Rails").length
718+
expect(comment_count).to eq(1)
719+
end
720+
end
721+
end
722+
723+
describe "#redux_store" do
724+
context "when React on Rails Pro is installed" do
725+
before do
726+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true)
727+
allow(ReactOnRailsPro::Utils).to receive(:pro_attribution_comment)
728+
.and_return("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
729+
end
730+
731+
it "includes the Pro attribution comment in the rendered output" do
732+
result = redux_store("TestStore", props: props)
733+
expect(result).to include("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
734+
end
735+
736+
it "includes the attribution comment only once" do
737+
result = redux_store("TestStore", props: props)
738+
comment_count = result.scan("<!-- Powered by React on Rails Pro").length
739+
expect(comment_count).to eq(1)
740+
end
741+
end
742+
743+
context "when React on Rails Pro is NOT installed" do
744+
before do
745+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
746+
end
747+
748+
it "includes the open source attribution comment in the rendered output" do
749+
result = redux_store("TestStore", props: props)
750+
expect(result).to include("<!-- Powered by React on Rails (c) ShakaCode | Open Source -->")
751+
end
752+
753+
it "includes the attribution comment only once" do
754+
result = redux_store("TestStore", props: props)
755+
comment_count = result.scan("<!-- Powered by React on Rails").length
756+
expect(comment_count).to eq(1)
757+
end
758+
end
759+
end
760+
761+
describe "#react_component_hash" do
762+
before do
763+
allow(ReactOnRails::ServerRenderingPool).to receive(:server_render_js_with_console_logging).and_return(
764+
"html" => { "componentHtml" => "<div>Test</div>", "title" => "Test Title" },
765+
"consoleReplayScript" => ""
766+
)
767+
allow(ReactOnRails::ServerRenderingJsCode).to receive(:js_code_renderer)
768+
.and_return(ReactOnRails::ServerRenderingJsCode)
769+
end
770+
771+
context "when React on Rails Pro is installed" do
772+
before do
773+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true)
774+
allow(ReactOnRailsPro::Utils).to receive(:pro_attribution_comment)
775+
.and_return("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
776+
end
777+
778+
it "includes the Pro attribution comment in the componentHtml" do
779+
result = react_component_hash("App", props: props, prerender: true)
780+
expect(result["componentHtml"]).to include("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
781+
end
782+
783+
it "includes the attribution comment only once" do
784+
result = react_component_hash("App", props: props, prerender: true)
785+
comment_count = result["componentHtml"].scan("<!-- Powered by React on Rails Pro").length
786+
expect(comment_count).to eq(1)
787+
end
788+
end
789+
790+
context "when React on Rails Pro is NOT installed" do
791+
before do
792+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
793+
end
794+
795+
it "includes the open source attribution comment in the componentHtml" do
796+
result = react_component_hash("App", props: props, prerender: true)
797+
expect(result["componentHtml"]).to include("<!-- Powered by React on Rails (c) ShakaCode | Open Source -->")
798+
end
799+
800+
it "includes the attribution comment only once" do
801+
result = react_component_hash("App", props: props, prerender: true)
802+
comment_count = result["componentHtml"].scan("<!-- Powered by React on Rails").length
803+
expect(comment_count).to eq(1)
804+
end
805+
end
806+
end
807+
end
677808
end
678809
# rubocop:enable Metrics/BlockLength

0 commit comments

Comments
 (0)