|
| 1 | +require 'spec_helper' |
| 2 | +require 'rails/generators' |
| 3 | +require 'generators/cypress_on_rails/install_generator' |
| 4 | +require 'tmpdir' |
| 5 | +require 'fileutils' |
| 6 | + |
| 7 | +RSpec.describe CypressOnRails::InstallGenerator, type: :generator do |
| 8 | + let(:destination_root) { Dir.mktmpdir } |
| 9 | + |
| 10 | + before do |
| 11 | + # Set up a minimal Rails app structure |
| 12 | + FileUtils.mkdir_p(File.join(destination_root, 'config', 'initializers')) |
| 13 | + FileUtils.mkdir_p(File.join(destination_root, 'bin')) |
| 14 | + |
| 15 | + # Mock the generator's destination_root |
| 16 | + allow(Dir).to receive(:pwd).and_return(destination_root) |
| 17 | + |
| 18 | + # Prevent actual npm/yarn installation in tests |
| 19 | + allow_any_instance_of(CypressOnRails::InstallGenerator).to receive(:system).and_return(true) |
| 20 | + end |
| 21 | + |
| 22 | + after do |
| 23 | + FileUtils.rm_rf(destination_root) |
| 24 | + end |
| 25 | + |
| 26 | + describe 'with default options (cypress framework, e2e folder)' do |
| 27 | + let(:args) { [] } |
| 28 | + let(:options) { {} } |
| 29 | + |
| 30 | + before do |
| 31 | + run_generator(args, options) |
| 32 | + end |
| 33 | + |
| 34 | + it 'creates the initializer with correct install_folder path' do |
| 35 | + initializer_path = File.join(destination_root, 'config', 'initializers', 'cypress_on_rails.rb') |
| 36 | + expect(File).to exist(initializer_path) |
| 37 | + |
| 38 | + content = File.read(initializer_path) |
| 39 | + # Should point to e2e, not e2e/cypress |
| 40 | + expect(content).to include('c.install_folder = File.expand_path("#{__dir__}/../../e2e")') |
| 41 | + end |
| 42 | + |
| 43 | + it 'creates cypress config at install_folder root' do |
| 44 | + config_path = File.join(destination_root, 'e2e', 'cypress.config.js') |
| 45 | + expect(File).to exist(config_path) |
| 46 | + end |
| 47 | + |
| 48 | + it 'creates e2e_helper.rb at install_folder root' do |
| 49 | + helper_path = File.join(destination_root, 'e2e', 'e2e_helper.rb') |
| 50 | + expect(File).to exist(helper_path) |
| 51 | + end |
| 52 | + |
| 53 | + it 'creates app_commands directory at install_folder root' do |
| 54 | + commands_path = File.join(destination_root, 'e2e', 'app_commands') |
| 55 | + expect(File).to be_directory(commands_path) |
| 56 | + end |
| 57 | + |
| 58 | + it 'creates cypress support files in framework subdirectory' do |
| 59 | + support_path = File.join(destination_root, 'e2e', 'cypress', 'support', 'index.js') |
| 60 | + expect(File).to exist(support_path) |
| 61 | + end |
| 62 | + |
| 63 | + it 'creates cypress examples in framework subdirectory' do |
| 64 | + examples_path = File.join(destination_root, 'e2e', 'cypress', 'e2e', 'rails_examples') |
| 65 | + expect(File).to be_directory(examples_path) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe 'with playwright framework' do |
| 70 | + let(:args) { [] } |
| 71 | + let(:options) { { framework: 'playwright' } } |
| 72 | + |
| 73 | + before do |
| 74 | + run_generator(args, options) |
| 75 | + end |
| 76 | + |
| 77 | + it 'creates the initializer with correct install_folder path' do |
| 78 | + initializer_path = File.join(destination_root, 'config', 'initializers', 'cypress_on_rails.rb') |
| 79 | + expect(File).to exist(initializer_path) |
| 80 | + |
| 81 | + content = File.read(initializer_path) |
| 82 | + # Should point to e2e, not e2e/playwright |
| 83 | + expect(content).to include('c.install_folder = File.expand_path("#{__dir__}/../../e2e")') |
| 84 | + end |
| 85 | + |
| 86 | + it 'creates playwright config at install_folder root' do |
| 87 | + config_path = File.join(destination_root, 'e2e', 'playwright.config.js') |
| 88 | + expect(File).to exist(config_path) |
| 89 | + end |
| 90 | + |
| 91 | + it 'creates e2e_helper.rb at install_folder root' do |
| 92 | + helper_path = File.join(destination_root, 'e2e', 'e2e_helper.rb') |
| 93 | + expect(File).to exist(helper_path) |
| 94 | + end |
| 95 | + |
| 96 | + it 'creates app_commands directory at install_folder root' do |
| 97 | + commands_path = File.join(destination_root, 'e2e', 'app_commands') |
| 98 | + expect(File).to be_directory(commands_path) |
| 99 | + end |
| 100 | + |
| 101 | + it 'creates playwright support files in framework subdirectory' do |
| 102 | + support_path = File.join(destination_root, 'e2e', 'playwright', 'support', 'index.js') |
| 103 | + expect(File).to exist(support_path) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe 'with custom install_folder' do |
| 108 | + let(:args) { [] } |
| 109 | + let(:options) { { install_folder: 'spec/system' } } |
| 110 | + |
| 111 | + before do |
| 112 | + run_generator(args, options) |
| 113 | + end |
| 114 | + |
| 115 | + it 'creates files in the custom folder' do |
| 116 | + helper_path = File.join(destination_root, 'spec', 'system', 'e2e_helper.rb') |
| 117 | + expect(File).to exist(helper_path) |
| 118 | + |
| 119 | + commands_path = File.join(destination_root, 'spec', 'system', 'app_commands') |
| 120 | + expect(File).to be_directory(commands_path) |
| 121 | + end |
| 122 | + |
| 123 | + it 'sets the correct install_folder in the initializer' do |
| 124 | + initializer_path = File.join(destination_root, 'config', 'initializers', 'cypress_on_rails.rb') |
| 125 | + content = File.read(initializer_path) |
| 126 | + expect(content).to include('c.install_folder = File.expand_path("#{__dir__}/../../spec/system")') |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + describe 'file structure ensures middleware and framework compatibility' do |
| 131 | + let(:args) { [] } |
| 132 | + let(:options) { {} } |
| 133 | + |
| 134 | + before do |
| 135 | + run_generator(args, options) |
| 136 | + end |
| 137 | + |
| 138 | + it 'places e2e_helper.rb where middleware expects it (install_folder/e2e_helper.rb)' do |
| 139 | + # Middleware looks for #{install_folder}/e2e_helper.rb |
| 140 | + helper_path = File.join(destination_root, 'e2e', 'e2e_helper.rb') |
| 141 | + expect(File).to exist(helper_path) |
| 142 | + expect(File.read(helper_path)).to include('CypressOnRails') |
| 143 | + end |
| 144 | + |
| 145 | + it 'places app_commands where middleware expects it (install_folder/app_commands)' do |
| 146 | + # Middleware looks for #{install_folder}/app_commands/#{command}.rb |
| 147 | + commands_path = File.join(destination_root, 'e2e', 'app_commands') |
| 148 | + expect(File).to be_directory(commands_path) |
| 149 | + |
| 150 | + # Check that command files exist |
| 151 | + clean_cmd = File.join(commands_path, 'clean.rb') |
| 152 | + expect(File).to exist(clean_cmd) |
| 153 | + end |
| 154 | + |
| 155 | + it 'places cypress.config.js where cypress --project flag expects it' do |
| 156 | + # Cypress runs with --project install_folder, expects config at that level |
| 157 | + config_path = File.join(destination_root, 'e2e', 'cypress.config.js') |
| 158 | + expect(File).to exist(config_path) |
| 159 | + |
| 160 | + # Verify the config references the correct relative path for support files |
| 161 | + content = File.read(config_path) |
| 162 | + expect(content).to include('cypress/support/index.js') |
| 163 | + end |
| 164 | + |
| 165 | + it 'creates a valid directory structure' do |
| 166 | + # The expected structure: |
| 167 | + # e2e/ |
| 168 | + # cypress.config.js <- Config at root of install_folder |
| 169 | + # e2e_helper.rb <- Helper at root of install_folder |
| 170 | + # app_commands/ <- Commands at root of install_folder |
| 171 | + # cypress/ <- Framework-specific subdirectory |
| 172 | + # support/ |
| 173 | + # e2e/ |
| 174 | + |
| 175 | + expect(File).to exist(File.join(destination_root, 'e2e', 'cypress.config.js')) |
| 176 | + expect(File).to exist(File.join(destination_root, 'e2e', 'e2e_helper.rb')) |
| 177 | + expect(File).to be_directory(File.join(destination_root, 'e2e', 'app_commands')) |
| 178 | + expect(File).to be_directory(File.join(destination_root, 'e2e', 'cypress')) |
| 179 | + expect(File).to be_directory(File.join(destination_root, 'e2e', 'cypress', 'support')) |
| 180 | + expect(File).to be_directory(File.join(destination_root, 'e2e', 'cypress', 'e2e')) |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + def run_generator(args, options) |
| 185 | + generator_options = [] |
| 186 | + options.each do |key, value| |
| 187 | + generator_options << "--#{key}=#{value}" |
| 188 | + end |
| 189 | + |
| 190 | + CypressOnRails::InstallGenerator.start( |
| 191 | + args + generator_options, |
| 192 | + { |
| 193 | + destination_root: destination_root, |
| 194 | + shell: Thor::Shell::Basic.new, |
| 195 | + behavior: :invoke |
| 196 | + } |
| 197 | + ) |
| 198 | + end |
| 199 | +end |
0 commit comments