Class: ReactOnRails::Generators::DevTestsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorHelper
Defined in:
lib/generators/react_on_rails/dev_tests_generator.rb

Instance Method Summary collapse

Methods included from GeneratorHelper

#add_documentation_reference, #add_npm_dependencies, #component_extension, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #destination_config_path, #detect_react_version, #empty_directory_with_keep_file, #gem_in_lockfile?, #keep_file, #package_json, #print_generator_messages, #pro_gem_installed?, #resolve_server_client_or_both_path, #setup_file_error, #shakapacker_version_9_or_higher?, #symlink_dest_file_to_dest_file, #use_pro?, #use_rsc?, #using_rspack?, #using_swc?

Instance Method Details

#add_react_on_rails_as_file_dependencyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/react_on_rails/dev_tests_generator.rb', line 58

def add_react_on_rails_as_file_dependency
  # Add react-on-rails as a file dependency pointing to the local package
  # This allows testing with the local npm package without needing yalc
  package_json = File.join(destination_root, "package.json")
  contents = JSON.parse(File.read(package_json))
  contents["dependencies"] ||= {}

  # Calculate relative path from the generated example to the npm package
  # Generated examples are in gen-examples/examples/<name>/
  # The npm package is in packages/react-on-rails/
  contents["dependencies"]["react-on-rails"] = "file:../../../packages/react-on-rails"

  File.open(package_json, "w+") { |f| f.puts JSON.pretty_generate(contents) }
end


40
41
42
43
44
45
# File 'lib/generators/react_on_rails/dev_tests_generator.rb', line 40

def add_test_related_gems_to_gemfile
  gem("rspec-rails", group: :test)
  # NOTE: chromedriver-helper was deprecated in 2019. Modern selenium-webdriver (4.x)
  # and GitHub Actions have built-in driver management, so no driver helper is needed.
  gem("coveralls", require: false)
end

#copy_rspec_filesObject



26
27
28
29
30
31
32
# File 'lib/generators/react_on_rails/dev_tests_generator.rb', line 26

def copy_rspec_files
  %w[.eslintrc
     spec/spec_helper.rb
     spec/rails_helper.rb
     spec/simplecov_helper.rb
     .rspec].each { |file| copy_file(file) }
end

#copy_testsObject



34
35
36
37
38
# File 'lib/generators/react_on_rails/dev_tests_generator.rb', line 34

def copy_tests
  files = %w[spec/system/hello_world_spec.rb]
  files << "spec/system/hello_server_spec.rb" if options.rsc
  files.each { |file| copy_file(file) }
end

#replace_prerender_if_server_renderingObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/react_on_rails/dev_tests_generator.rb', line 47

def replace_prerender_if_server_rendering
  return unless options.example_server_rendering

  hello_world_index = File.join(destination_root, "app", "views", "hello_world", "index.html.erb")
  hello_world_contents = File.read(hello_world_index)
  new_hello_world_contents = hello_world_contents.gsub("prerender: false",
                                                       "prerender: true")

  File.open(hello_world_index, "w+") { |f| f.puts new_hello_world_contents }
end