|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "spec_helper" |
| 4 | + |
| 5 | +module ReactOnRails |
| 6 | + RSpec.describe Engine do |
| 7 | + describe ".skip_version_validation?" do |
| 8 | + let(:package_json_path) { "/fake/path/package.json" } |
| 9 | + |
| 10 | + before do |
| 11 | + allow(VersionChecker::NodePackageVersion).to receive(:package_json_path) |
| 12 | + .and_return(package_json_path) |
| 13 | + allow(Rails.logger).to receive(:debug) |
| 14 | + end |
| 15 | + |
| 16 | + context "when package.json doesn't exist" do |
| 17 | + before do |
| 18 | + allow(File).to receive(:exist?).with(package_json_path).and_return(false) |
| 19 | + end |
| 20 | + |
| 21 | + it "returns true" do |
| 22 | + expect(described_class.skip_version_validation?).to be true |
| 23 | + end |
| 24 | + |
| 25 | + it "logs debug message about missing package.json" do |
| 26 | + described_class.skip_version_validation? |
| 27 | + expect(Rails.logger).to have_received(:debug) |
| 28 | + .with("[React on Rails] Skipping validation - package.json not found") |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context "when package.json exists" do |
| 33 | + before do |
| 34 | + allow(File).to receive(:exist?).with(package_json_path).and_return(true) |
| 35 | + end |
| 36 | + |
| 37 | + context "when running a generator" do |
| 38 | + before do |
| 39 | + stub_const("ARGV", ["generate", "react_on_rails:install"]) |
| 40 | + end |
| 41 | + |
| 42 | + it "returns true" do |
| 43 | + expect(described_class.skip_version_validation?).to be true |
| 44 | + end |
| 45 | + |
| 46 | + it "logs debug message about generator runtime" do |
| 47 | + described_class.skip_version_validation? |
| 48 | + expect(Rails.logger).to have_received(:debug) |
| 49 | + .with("[React on Rails] Skipping validation during generator runtime") |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + context "when running a generator with short form" do |
| 54 | + before do |
| 55 | + stub_const("ARGV", ["g", "react_on_rails:install"]) |
| 56 | + end |
| 57 | + |
| 58 | + it "returns true" do |
| 59 | + expect(described_class.skip_version_validation?).to be true |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "when ARGV is empty" do |
| 64 | + before do |
| 65 | + stub_const("ARGV", []) |
| 66 | + end |
| 67 | + |
| 68 | + it "returns false" do |
| 69 | + expect(described_class.skip_version_validation?).to be false |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context "when running other commands" do |
| 74 | + %w[server console runner].each do |command| |
| 75 | + context "when running '#{command}'" do |
| 76 | + before do |
| 77 | + stub_const("ARGV", [command]) |
| 78 | + end |
| 79 | + |
| 80 | + it "returns false" do |
| 81 | + expect(described_class.skip_version_validation?).to be false |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe ".running_generator?" do |
| 90 | + context "when ARGV is empty" do |
| 91 | + before do |
| 92 | + stub_const("ARGV", []) |
| 93 | + end |
| 94 | + |
| 95 | + it "returns false" do |
| 96 | + expect(described_class.running_generator?).to be false |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context "when ARGV.first is 'generate'" do |
| 101 | + before do |
| 102 | + stub_const("ARGV", %w[generate model User]) |
| 103 | + end |
| 104 | + |
| 105 | + it "returns true" do |
| 106 | + expect(described_class.running_generator?).to be true |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context "when ARGV.first is 'g'" do |
| 111 | + before do |
| 112 | + stub_const("ARGV", %w[g controller Users]) |
| 113 | + end |
| 114 | + |
| 115 | + it "returns true" do |
| 116 | + expect(described_class.running_generator?).to be true |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "when ARGV.first is another command" do |
| 121 | + before do |
| 122 | + stub_const("ARGV", ["server"]) |
| 123 | + end |
| 124 | + |
| 125 | + it "returns false" do |
| 126 | + expect(described_class.running_generator?).to be false |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + describe ".package_json_missing?" do |
| 132 | + let(:package_json_path) { "/fake/path/package.json" } |
| 133 | + |
| 134 | + before do |
| 135 | + allow(VersionChecker::NodePackageVersion).to receive(:package_json_path) |
| 136 | + .and_return(package_json_path) |
| 137 | + end |
| 138 | + |
| 139 | + context "when package.json exists" do |
| 140 | + before do |
| 141 | + allow(File).to receive(:exist?).with(package_json_path).and_return(true) |
| 142 | + end |
| 143 | + |
| 144 | + it "returns false" do |
| 145 | + expect(described_class.package_json_missing?).to be false |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + context "when package.json doesn't exist" do |
| 150 | + before do |
| 151 | + allow(File).to receive(:exist?).with(package_json_path).and_return(false) |
| 152 | + end |
| 153 | + |
| 154 | + it "returns true" do |
| 155 | + expect(described_class.package_json_missing?).to be true |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments