Skip to content

Commit

Permalink
update rspecs for new bootstrap related logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yorzi committed Nov 30, 2015
1 parent 6142e1a commit 3691a49
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 46 deletions.
7 changes: 0 additions & 7 deletions lib/generators/react_on_rails/base_generator.rb
Expand Up @@ -179,13 +179,6 @@ def force_application_scss_naming_if_necessary
new_name = File.join(destination_root, "#{base_path}application.scss")
File.rename(bad_name, new_name)
end

# if there still is not application.scss, just create one
def create_application_scss_if_necessary
path = File.join(destination_root, "app/assets/stylesheets/application.scss")
return if File.exist?(path)
File.open(path, "w") { |f| f.puts "// Created by React on Rails gem\n\n" }
end
end
end
end
7 changes: 7 additions & 0 deletions lib/generators/react_on_rails/bootstrap_generator.rb
Expand Up @@ -17,6 +17,13 @@ def copy_bootstrap_files
client/bootstrap-sass.config.js).each { |file| copy_file(base_path + file, file) }
end

# if there still is not application.scss, just create one
def create_application_scss_if_necessary
path = File.join(destination_root, "app/assets/stylesheets/application.scss")
return if File.exist?(path)
File.open(path, "w") { |f| f.puts "// Created by React on Rails gem\n\n" }
end

def prepend_to_application_scss
data = <<-DATA.strip_heredoc
// DO NOT REQUIRE TREE! It will interfere with load order!
Expand Down
8 changes: 6 additions & 2 deletions lib/generators/react_on_rails/install_generator.rb
Expand Up @@ -44,6 +44,12 @@ class InstallGenerator < Rails::Generators::Base
def run_generators # rubocop:disable Metrics/CyclomaticComplexity
return unless installation_prerequisites_met?
warn_if_nvm_is_not_installed
invoke_generators
end

private

def invoke_generators # rubocop:disable Metrics/CyclomaticComplexity
invoke "react_on_rails:base"
invoke "react_on_rails:react_no_redux" unless options.redux?
invoke "react_on_rails:react_with_redux" if options.redux?
Expand All @@ -53,8 +59,6 @@ def run_generators # rubocop:disable Metrics/CyclomaticComplexity
invoke "react_on_rails:bootstrap" if options.bootstrap?
end

private

# NOTE: other requirements for existing files such as .gitignore or application.
# js(.coffee) are not checked by this method, but instead produce warning messages
# and allow the build to continue
Expand Down
53 changes: 41 additions & 12 deletions spec/react_on_rails/generators/install_generator_spec.rb
Expand Up @@ -9,7 +9,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -19,7 +19,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:disabled"
end

Expand All @@ -29,7 +29,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:disabled"
end

Expand All @@ -39,7 +39,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
include_examples "ruby_linters"
end
Expand All @@ -50,7 +50,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
include_examples "ruby_linters"
end
Expand All @@ -61,7 +61,7 @@
include_examples "base_generator:server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -71,7 +71,7 @@
include_examples "base_generator:server_rendering"
include_examples "no_redux_generator:base"
include_examples "no_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -81,7 +81,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -91,7 +91,7 @@
include_examples "base_generator:no_server_rendering"
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:no_server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -101,7 +101,7 @@
include_examples "base_generator:server_rendering"
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -111,7 +111,7 @@
include_examples "base_generator:server_rendering"
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -121,7 +121,7 @@
include_examples "base_generator:server_rendering"
include_examples "react_with_redux_generator:base"
include_examples "react_with_redux_generator:server_rendering"
include_examples "bootstrap"
include_examples "bootstrap:enabled"
include_examples "js_linters:enabled"
end

Expand All @@ -142,6 +142,35 @@
include_examples "heroku_deployment"
end

context "--bootstrap" do
before(:all) { run_generator_test_with_args(%w(--bootstrap)) }
include_examples "base_generator:base", application_js: true
include_examples "bootstrap:enabled"
end

context "--bootstrap" do
before(:all) { run_generator_test_with_args(%w(--bootstrap), application_js: false) }
include_examples "base_generator:base", application_js: false
include_examples "bootstrap:enabled"
end

context "-B" do
before(:all) { run_generator_test_with_args(%w(-B)) }
include_examples "bootstrap:enabled"
end

context "--no-bootstrap" do
before(:all) { run_generator_test_with_args(%w(--no-bootstrap)) }
include_examples "base_generator:base", application_js: true
include_examples "bootstrap:disabled"
end

context "--no-bootstrap" do
before(:all) { run_generator_test_with_args(%w(--no-bootstrap), application_js: false) }
include_examples "base_generator:base", application_js: false
include_examples "bootstrap:disabled"
end

context "without existing application.js or application.js.coffee file" do
before(:all) { run_generator_test_with_args([], application_js: false) }
include_examples "base_generator:base", application_js: false
Expand Down
Expand Up @@ -39,9 +39,6 @@
//= require generated/vendor-bundle
//= require generated/app-bundle
// bootstrap-sprockets depends on generated/vendor-bundle for jQuery.
//= require bootstrap-sprockets
MATCH
assert_file("app/assets/javascripts/application.js") do |contents|
assert_match(match, contents)
Expand Down Expand Up @@ -85,6 +82,21 @@
assert_match("gem 'therubyracer', platforms: :ruby", contents)
end
end

it "appends path configurations to assets.rb" do
expected = <<-EXPECTED.strip_heredoc
# Add client/assets/ folders to asset pipeline's search path.
# If you do not want to move existing images and fonts from your Rails app
# you could also consider creating symlinks there that point to the original
# rails directories. In that case, you would not add these paths here.
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "stylesheets")
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "images")
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "fonts")
Rails.application.config.assets.precompile += %w( generated/server-bundle.js )
EXPECTED
assert_file("config/initializers/assets.rb") { |contents| assert_match(expected, contents) }
end
end

shared_examples "base_generator:no_server_rendering" do
Expand Down
@@ -1,33 +1,25 @@
shared_examples "bootstrap" do
it "appends path configurations to assets.rb" do
expected = <<-EXPECTED.strip_heredoc
# Add client/assets/ folders to asset pipeline's search path.
# If you do not want to move existing images and fonts from your Rails app
# you could also consider creating symlinks there that point to the original
# rails directories. In that case, you would not add these paths here.
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "stylesheets")
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "images")
Rails.application.config.assets.paths << Rails.root.join("client", "assets", "fonts")
Rails.application.config.assets.precompile += %w( generated/server-bundle.js )
EXPECTED
assert_file("config/initializers/assets.rb") { |contents| assert_match(expected, contents) }
end

shared_examples "bootstrap:enabled" do
it "removes incompatible requires in application.scss" do
assert_file("app/assets/stylesheets/application.scss") do |contents|
refute_match("*= require_tree .", contents)
refute_match("*= require_self", contents)
end
end

it "appends bootstrap related contents in application.scss" do
assert_file("app/assets/stylesheets/application.scss") do |contents|
assert_match(bootstrap_related_css_contents, contents)
end
end

it "appends bootstrap related js contents in application.js" do
assert_file("app/assets/javascripts/application.js") do |contents|
assert_match(bootstrap_related_js_contents, contents)
end
end

it "copies bootstrap files" do
%w(app/assets/stylesheets/_bootstrap-custom.scss
app/assets/stylesheets/application.scss
client/assets/stylesheets/_post-bootstrap.scss
client/assets/stylesheets/_pre-bootstrap.scss
client/assets/stylesheets/_react-on-rails-sass-helper.scss
client/bootstrap-sass.config.js).each { |file| assert_file(file) }
bootstrap_related_files.each { |file| assert_file(file) }
end

it "adds bootstrap_sprockets to the Gemfile" do
Expand All @@ -36,3 +28,70 @@
end
end
end

shared_examples "bootstrap:disabled" do
it "doesn't copy bootstrap files" do
bootstrap_related_files.each { |file| assert_no_file(file) }
end

it "doesn't add bootstrap_sprockets to the Gemfile" do
assert_file("Gemfile") do |contents|
refute_match(/gem 'bootstrap-sass'/, contents)
end
end

it "doesn't append bootstrap related contents in application.scss" do
assert_file("app/assets/stylesheets/application.scss") do |contents|
refute_match(bootstrap_related_css_contents, contents)
end
end

it "doesn't append bootstrap related js contents in application.js" do
assert_file("app/assets/javascripts/application.js") do |contents|
refute_match(bootstrap_related_js_contents, contents)
end
end
end

def bootstrap_related_files
%w(app/assets/stylesheets/_bootstrap-custom.scss
client/assets/stylesheets/_post-bootstrap.scss
client/assets/stylesheets/_pre-bootstrap.scss
client/assets/stylesheets/_react-on-rails-sass-helper.scss
client/bootstrap-sass.config.js)
end

def bootstrap_related_css_contents
<<-DATA.strip_heredoc
// DO NOT REQUIRE TREE! It will interfere with load order!
// Account for differences between Rails and Webpack Sass code.
$rails: true;
// Included from bootstrap-sprockets gem and loaded in app/assets/javascripts/application.rb
@import 'bootstrap-sprockets';
// Customizations - needs to be imported after bootstrap-sprocket but before bootstrap-custom!
// The _pre-bootstrap.scss file is located under
// client/assets/stylesheets, which has been added to the Rails asset
// pipeline search path. See config/application.rb.
@import 'pre-bootstrap';
// These scss files are located under client/assets/stylesheets
// (which has been added to the Rails asset pipeline search path in config/application.rb).
@import 'bootstrap-custom';
// This must come after all the boostrap styles are loaded so that these styles can override those.
@import 'post-bootstrap';
DATA
end

def bootstrap_related_js_contents
<<-DATA.strip_heredoc
// bootstrap-sprockets depends on generated/vendor-bundle for jQuery.
//= require bootstrap-sprockets
DATA
end

0 comments on commit 3691a49

Please sign in to comment.