Skip to content

Commit 58d680e

Browse files
Add tests for react_on_rails_attribution_comment to verify Pro and open source license comments
1 parent 5555b37 commit 58d680e

File tree

2 files changed

+119
-13
lines changed

2 files changed

+119
-13
lines changed

react_on_rails_pro/spec/react_on_rails_pro/utils_spec.rb

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "spec_helper"
44

5+
# rubocop:disable Metrics/ModuleLength
56
module ReactOnRailsPro
67
RSpec.describe Utils do
78
describe "cache helpers .bundle_hash and .bundle_file_name" do
@@ -21,9 +22,7 @@ module ReactOnRailsPro
2122

2223
before do
2324
allow(ReactOnRails.configuration)
24-
.to receive(:server_bundle_js_file).and_return(nil)
25-
allow(ReactOnRails.configuration)
26-
.to receive(:rsc_bundle_js_file).and_return(nil)
25+
.to receive_messages(server_bundle_js_file: nil, rsc_bundle_js_file: nil)
2726
allow(Shakapacker).to receive_message_chain("manifest.lookup!")
2827
.with("client-bundle.js")
2928
.and_return("/webpack/production/client-bundle-0123456789abcdef.js")
@@ -42,9 +41,8 @@ module ReactOnRailsPro
4241
allow(ReactOnRails::Utils).to receive(:server_bundle_js_file_path)
4342
.and_return(server_bundle_js_file_path)
4443
allow(ReactOnRails.configuration)
45-
.to receive(:server_bundle_js_file).and_return("webpack-bundle.js")
46-
allow(ReactOnRails.configuration)
47-
.to receive(:rsc_bundle_js_file).and_return("rsc-webpack-bundle.js")
44+
.to receive_messages(server_bundle_js_file: "webpack-bundle.js",
45+
rsc_bundle_js_file: "rsc-webpack-bundle.js")
4846
allow(File).to receive(:mtime).with(server_bundle_js_file_path).and_return(123)
4947

5048
result = described_class.bundle_hash
@@ -80,14 +78,13 @@ module ReactOnRailsPro
8078
rsc_bundle_js_file_path = File.expand_path("./public/#{rsc_bundle_js_file}")
8179
allow(Shakapacker).to receive_message_chain("manifest.lookup!")
8280
.and_return(rsc_bundle_js_file)
83-
allow(ReactOnRails::Utils).to receive(:server_bundle_js_file_path)
84-
.and_return(rsc_bundle_js_file_path.gsub("rsc-", ""))
85-
allow(ReactOnRails::Utils).to receive(:rsc_bundle_js_file_path)
86-
.and_return(rsc_bundle_js_file_path)
87-
allow(ReactOnRails.configuration)
88-
.to receive(:server_bundle_js_file).and_return("webpack-bundle.js")
81+
allow(ReactOnRails::Utils).to receive_messages(
82+
server_bundle_js_file_path: rsc_bundle_js_file_path.gsub("rsc-", ""),
83+
rsc_bundle_js_file_path: rsc_bundle_js_file_path
84+
)
8985
allow(ReactOnRails.configuration)
90-
.to receive(:rsc_bundle_js_file).and_return("rsc-webpack-bundle.js")
86+
.to receive_messages(server_bundle_js_file: "webpack-bundle.js",
87+
rsc_bundle_js_file: "rsc-webpack-bundle.js")
9188
allow(Digest::MD5).to receive(:file)
9289
.with(rsc_bundle_js_file_path)
9390
.and_return("barfoobarfoo")
@@ -221,5 +218,73 @@ module ReactOnRailsPro
221218

222219
it { expect(printable_cache_key).to eq("1_2_3_4_5") }
223220
end
221+
222+
describe ".pro_attribution_comment" do
223+
before do
224+
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))
225+
end
226+
227+
context "when license is valid and not in grace period" do
228+
before do
229+
allow(ReactOnRailsPro::LicenseValidator).to receive_messages(grace_days_remaining: nil, evaluation?: false)
230+
end
231+
232+
it "returns the standard licensed attribution comment" do
233+
result = described_class.pro_attribution_comment
234+
expect(result).to eq("<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->")
235+
end
236+
end
237+
238+
context "when license is in grace period" do
239+
before do
240+
allow(ReactOnRailsPro::LicenseValidator).to receive(:grace_days_remaining).and_return(15)
241+
end
242+
243+
it "returns attribution comment with grace period information" do
244+
result = described_class.pro_attribution_comment
245+
expected = "<!-- Powered by React on Rails Pro (c) ShakaCode | " \
246+
"Licensed (Expired - Grace Period: 15 days remaining) -->"
247+
expect(result).to eq(expected)
248+
end
249+
end
250+
251+
context "when license is in grace period with 1 day remaining" do
252+
before do
253+
allow(ReactOnRailsPro::LicenseValidator).to receive(:grace_days_remaining).and_return(1)
254+
end
255+
256+
it "returns attribution comment with singular day" do
257+
result = described_class.pro_attribution_comment
258+
expected = "<!-- Powered by React on Rails Pro (c) ShakaCode | " \
259+
"Licensed (Expired - Grace Period: 1 days remaining) -->"
260+
expect(result).to eq(expected)
261+
end
262+
end
263+
264+
context "when using evaluation license" do
265+
before do
266+
allow(ReactOnRailsPro::LicenseValidator).to receive_messages(grace_days_remaining: nil, evaluation?: true)
267+
end
268+
269+
it "returns evaluation license attribution comment" do
270+
result = described_class.pro_attribution_comment
271+
expect(result).to eq("<!-- Powered by React on Rails Pro (c) ShakaCode | Evaluation License -->")
272+
end
273+
end
274+
275+
context "when grace_days_remaining returns 0" do
276+
before do
277+
allow(ReactOnRailsPro::LicenseValidator).to receive(:grace_days_remaining).and_return(0)
278+
end
279+
280+
it "returns attribution comment with grace period information" do
281+
result = described_class.pro_attribution_comment
282+
expected = "<!-- Powered by React on Rails Pro (c) ShakaCode | " \
283+
"Licensed (Expired - Grace Period: 0 days remaining) -->"
284+
expect(result).to eq(expected)
285+
end
286+
end
287+
end
224288
end
225289
end
290+
# rubocop:enable Metrics/ModuleLength

spec/dummy/spec/helpers/react_on_rails_helper_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,5 +633,46 @@ def helper.append_javascript_pack_tag(name, **options)
633633
expect(helper).to have_received(:rails_context).with(server_side: false)
634634
end
635635
end
636+
637+
describe "#react_on_rails_attribution_comment" do
638+
let(:helper) { PlainReactOnRailsHelper.new }
639+
640+
context "when React on Rails Pro is installed" do
641+
let(:pro_comment) { "<!-- Powered by React on Rails Pro (c) ShakaCode | Licensed -->" }
642+
643+
before do
644+
pro_module = Module.new
645+
utils_module = Module.new do
646+
def self.pro_attribution_comment; end
647+
end
648+
stub_const("ReactOnRailsPro", pro_module)
649+
stub_const("ReactOnRailsPro::Utils", utils_module)
650+
651+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true)
652+
allow(utils_module).to receive(:pro_attribution_comment).and_return(pro_comment)
653+
end
654+
655+
it "returns the Pro attribution comment" do
656+
result = helper.send(:react_on_rails_attribution_comment)
657+
expect(result).to eq(pro_comment)
658+
end
659+
660+
it "calls ReactOnRailsPro::Utils.pro_attribution_comment" do
661+
helper.send(:react_on_rails_attribution_comment)
662+
expect(ReactOnRailsPro::Utils).to have_received(:pro_attribution_comment)
663+
end
664+
end
665+
666+
context "when React on Rails Pro is NOT installed" do
667+
before do
668+
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
669+
end
670+
671+
it "returns the open source attribution comment" do
672+
result = helper.send(:react_on_rails_attribution_comment)
673+
expect(result).to eq("<!-- Powered by React on Rails (c) ShakaCode | Open Source -->")
674+
end
675+
end
676+
end
636677
end
637678
# rubocop:enable Metrics/BlockLength

0 commit comments

Comments
 (0)