Skip to content

Commit 3659ced

Browse files
committed
Add RSpec tests for Rails 5.1
1 parent 6d2354f commit 3659ced

7 files changed

Lines changed: 215 additions & 3 deletions

File tree

Gemfile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
source "http://rubygems.org"
2-
# Specify your gem's dependencies in active_admin_sidebar.gemspec
1+
source 'https://rubygems.org'
32

3+
# Specify your gem's dependencies in activeadmin_scoped_collection_actions.gemspec
44
gemspec
5+
6+
group :test do
7+
gem 'sprockets-rails', '3.2.0'
8+
gem 'rails', '5.1.1'
9+
gem 'turbolinks'
10+
gem 'rspec-rails'
11+
gem 'activeadmin', '1.0.0'
12+
gem 'sass-rails'
13+
gem 'sqlite3'
14+
gem 'launchy'
15+
gem 'database_cleaner'
16+
gem 'capybara'
17+
gem 'selenium-webdriver'
18+
gem 'poltergeist'
19+
end

Rakefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
require "bundler/gem_tasks"
1+
require "bundler"
2+
require 'rake'
3+
Bundler.setup
4+
Bundler::GemHelper.install_tasks
5+
6+
# Import all our rake tasks
7+
FileList['tasks/**/*.rake'].each { |task| import task }

spec/sidebars_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'spec_helper'
2+
3+
describe 'authors index', type: :feature, js: true do
4+
5+
before do
6+
Author.create!(name: 'John', last_name: 'Doe')
7+
Author.create!(name: 'Jane', last_name: 'Roe')
8+
add_author_resource
9+
add_post_resource
10+
end
11+
12+
context 'left-sidebar with default settings' do
13+
before do
14+
visit '/admin/authors'
15+
end
16+
17+
it 'has left-sidebar with colapse-button' do
18+
expect(page).to have_css('#filters_sidebar_section')
19+
expect(page).to have_css('#filters_sidebar_section .collapse_btn.icono-caret-left')
20+
expect(page).to have_css('#active_admin_content.with_sidebar.left_sidebar.collapsible_sidebar')
21+
end
22+
23+
context 'when click on Collapse' do
24+
before do
25+
page.find('#filters_sidebar_section .collapse_btn').click
26+
end
27+
28+
it "sidebar is hidden, and save it's state after going to another page" do
29+
expect(page).to have_css('#sidebar', visible: :hidden)
30+
31+
# Posts page is configured as: "before_action :skip_sidebar!"
32+
visit '/admin/posts'
33+
# sidebar does not exists at all
34+
expect(page).to have_css('#page_title', text: 'Posts')
35+
expect(page).not_to have_css('#sidebar', visible: :all)
36+
37+
visit '/admin/authors'
38+
# sidebar is hidden
39+
expect(page).to have_css('#page_title', text: 'Authors')
40+
expect(page).to have_css('#sidebar', visible: :hidden)
41+
42+
page.find('.uncollapse_btn').click
43+
44+
# sidebar is visible
45+
expect(page).to have_css('#sidebar', visible: :visible)
46+
expect(page).to have_css('.collapse_btn')
47+
expect(page).not_to have_css('.uncollapse_btn')
48+
end
49+
end
50+
51+
end
52+
53+
end

spec/spec_helper.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
$LOAD_PATH.unshift(File.dirname(__FILE__))
2+
$LOAD_PATH << File.expand_path('../support', __FILE__)
3+
4+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
5+
require "bundler"
6+
Bundler.setup
7+
8+
ENV['RAILS_ENV'] = 'test'
9+
# Ensure the Active Admin load path is happy
10+
require 'rails'
11+
ENV['RAILS'] = Rails.version
12+
ENV['RAILS_ROOT'] = File.expand_path("../rails/rails-#{ENV['RAILS']}", __FILE__)
13+
# Create the test app if it doesn't exists
14+
unless File.exists?(ENV['RAILS_ROOT'])
15+
system 'rake setup'
16+
end
17+
18+
require 'active_model'
19+
# require ActiveRecord to ensure that Ransack loads correctly
20+
require 'active_record'
21+
require 'active_admin'
22+
ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
23+
require ENV['RAILS_ROOT'] + '/config/environment.rb'
24+
# Disabling authentication in specs so that we don't have to worry about
25+
# it allover the place
26+
ActiveAdmin.application.authentication_method = false
27+
ActiveAdmin.application.current_user_method = false
28+
29+
require 'rspec/rails'
30+
require 'support/admin'
31+
require 'capybara/rails'
32+
require 'capybara/rspec'
33+
require 'capybara/poltergeist'
34+
35+
36+
RSpec.configure do |config|
37+
config.use_transactional_fixtures = false
38+
39+
config.before(:suite) do
40+
DatabaseCleaner.strategy = :truncation
41+
DatabaseCleaner.clean_with(:truncation)
42+
end
43+
config.before(:each) do
44+
DatabaseCleaner.strategy = :truncation
45+
DatabaseCleaner.start
46+
end
47+
config.after(:each) do
48+
DatabaseCleaner.clean
49+
end
50+
51+
end
52+
53+
# RSpec.configure do |config|
54+
# config.before(:each, js: true) do
55+
# page.driver.browser.manage.window.maximize if page.driver.browser.respond_to?(:manage)
56+
# end
57+
# end
58+
# Capybara.javascript_driver = :selenium
59+
60+
Capybara.register_driver :poltergeist do |app|
61+
Capybara::Poltergeist::Driver.new(app, {
62+
js_errors: true,
63+
timeout: 80,
64+
debug: true,
65+
:phantomjs_options => ['--debug=no', '--load-images=no']
66+
})
67+
end
68+
Capybara.javascript_driver = :poltergeist

spec/support/admin.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def add_author_resource(options = {}, &block)
2+
3+
ActiveAdmin.register Author do
4+
config.filters = true
5+
end
6+
7+
Rails.application.reload_routes!
8+
9+
end
10+
11+
12+
def add_post_resource(options = {}, &block)
13+
14+
ActiveAdmin.register Post do
15+
config.filters = true
16+
before_action :skip_sidebar!
17+
end
18+
19+
Rails.application.reload_routes!
20+
21+
end

spec/support/rails_template.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Rails template to build the sample app for specs
2+
3+
generate :model, 'author name:string{10}:uniq last_name:string birthday:date'
4+
generate :model, 'post title:string:uniq body:text author:references'
5+
6+
#Add validation
7+
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n", after: "Base\n"
8+
inject_into_file "app/models/post.rb", " validates_presence_of :author\n", after: ":author\n"
9+
10+
# Configure default_url_options in test environment
11+
inject_into_file "config/environments/test.rb", " config.action_mailer.default_url_options = { :host => 'example.com' }\n", after: "config.cache_classes = true\n"
12+
13+
# Add our local Active Admin to the load path
14+
inject_into_file "config/environment.rb",
15+
"\n$LOAD_PATH.unshift('#{File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib'))}')\nrequire \"active_admin\"\n",
16+
after: "require File.expand_path('../application', __FILE__)"
17+
18+
run "rm Gemfile"
19+
20+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
21+
22+
generate :'active_admin:install --skip-users'
23+
generate :'formtastic:install'
24+
25+
# Initialize plugin
26+
inject_into_file "config/initializers/active_admin.rb",
27+
" config.before_action do\n left_sidebar!(collapsed: true) if respond_to?(:left_sidebar!)\n end\n\n",
28+
after: "ActiveAdmin.setup do |config|\n"
29+
30+
inject_into_file "app/assets/stylesheets/active_admin.scss",
31+
"@import \"active_admin_sidebar\";\n",
32+
after: "@import \"active_admin/base\";\n"
33+
34+
inject_into_file "app/assets/javascripts/active_admin.js.coffee",
35+
"#= require active_admin_sidebar\n",
36+
after: "#= require active_admin/base\n"
37+
38+
run "rm -r test"
39+
run "rm -r spec"
40+
41+
route "root :to => 'admin/dashboard#index'"
42+
43+
rake "db:migrate"

tasks/test.rake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
desc "Creates a test rails app for the specs to run against"
2+
task :setup do
3+
require 'rails/version'
4+
system("mkdir spec/rails") unless File.exists?("spec/rails")
5+
system "bundle exec rails new spec/rails/rails-#{Rails::VERSION::STRING} -m spec/support/rails_template.rb --skip-spring"
6+
end

0 commit comments

Comments
 (0)