Skip to content

Commit e3e7044

Browse files
committed
Add integration tests
1 parent aa9fcf0 commit e3e7044

31 files changed

+190
-66
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.gem
22
Gemfile.lock
3+
test/dummy/tmp
4+
test/dummy/log

.travis.yml

-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
rvm:
2-
- 1.8.7
32
- 1.9.3
43
- 2.0.0
5-
- jruby-18mode
64
- jruby-19mode
7-
- rbx-18mode
85
- rbx-19mode
96

107
gemfile:
118
- test/gemfiles/Gemfile.rails-3.1.x
129
- test/gemfiles/Gemfile.rails-3.2.x
1310
- test/gemfiles/Gemfile.rails-4.0.x
14-
15-
matrix:
16-
exclude:
17-
- rvm: 1.8.7
18-
gemfile: test/gemfiles/Gemfile.rails-4.0.x
19-
- rvm: jruby-18mode
20-
gemfile: test/gemfiles/Gemfile.rails-4.0.x
21-
- rvm: rbx-18mode
22-
gemfile: test/gemfiles/Gemfile.rails-4.0.x

pjax_rails.gemspec

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ Gem::Specification.new do |s|
1111
s.add_dependency 'jquery-rails'
1212

1313
s.add_development_dependency 'rake'
14-
s.add_development_dependency 'tzinfo'
14+
s.add_development_dependency 'rails'
15+
s.add_development_dependency 'capybara'
16+
s.add_development_dependency 'poltergeist'
1517
end
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
require 'test_helper'
22

3-
class PjaxControllerTest < ActionController::TestCase
3+
class DefaultLayoutControllerTest < ActionController::TestCase
44
test 'renders without layout' do
55
request.env['HTTP_X_PJAX'] = true
66

77
get :index
88

99
if Rails::VERSION::STRING >= '4.0.0'
10-
assert_equal 'pjax#index', response.body
10+
assert_match 'default_layout#index', response.body
1111
else
1212
# The behavior for ~> 3.0 varies from 4.0. If there is a layout for parent
1313
# controller and `layout` in parent controller is set to false it will be
1414
# rendered anyway with a warning in a log file. It should be set explicit
1515
# in child controller.
16-
assert_equal 'layouts/application pjax#index', response.body
16+
assert_match 'layouts/application default_layout#index', response.body
1717
end
1818
end
1919

2020
test 'renders with default layout' do
2121
get :index
2222

23-
assert_equal 'layouts/application pjax#index', response.body
23+
assert_match 'layouts/application default_layout#index', response.body
2424
end
2525

2626
test 'prevents pjax' do
@@ -36,20 +36,20 @@ class PjaxControllerTest < ActionController::TestCase
3636

3737
get :index, '_pjax' => true
3838

39-
assert_equal({ 'controller' => 'pjax', 'action' => 'index' }, @controller.params)
39+
assert_equal({ 'controller' => 'default_layout', 'action' => 'index' }, @controller.params)
4040
assert_equal '', request.env['QUERY_STRING']
4141
assert_nil request.env['rack.request.query_string']
4242
assert_nil request.env['rack.request.query_hash']
4343
assert_nil request.env['action_dispatch.request.query_parameters']
44-
assert_equal '/pjax', request.original_fullpath
45-
assert_equal '/pjax', request.fullpath
44+
assert_equal '/default_layout', request.original_fullpath
45+
assert_equal '/default_layout', request.fullpath
4646
end
4747

4848
test 'sets pjax url' do
4949
request.env['HTTP_X_PJAX'] = true
5050

5151
get :index
5252

53-
assert_equal 'http://test.host/pjax', response.headers['X-PJAX-URL']
53+
assert_equal 'http://test.host/default_layout', response.headers['X-PJAX-URL']
5454
end
5555
end

test/controllers/with_layout_controller_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class WithLayoutControllerTest < ActionController::TestCase
66

77
get :index
88

9-
assert_equal 'layouts/with_layout with_layout#index', response.body
9+
assert_match 'layouts/with_layout with_layout#index', response.body
1010
end
1111

1212
test 'renders with layout for regular request' do
1313
get :index
1414

15-
assert_equal 'layouts/with_layout with_layout#index', response.body
15+
assert_match 'layouts/with_layout with_layout#index', response.body
1616
end
1717
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//= require jquery
2+
//= require jquery_ujs
3+
//= require jquery.pjax
4+
//= require_tree .
5+
6+
$(function() {
7+
$(document).pjax('a:not([data-skip-pjax])', '[data-pjax-container]');
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationController < ActionController::Base
2+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CapybaraController < ApplicationController
2+
def prevents_pjax
3+
prevent_pjax!
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DefaultLayoutController < ApplicationController
2+
def prevent_pjax
3+
prevent_pjax!
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class WithLayoutController < ApplicationController
2+
layout 'with_layout'
3+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<table>
2+
<tbody>
3+
<tr>
4+
<td>Html content</td>
5+
</tr>
6+
</tbody>
7+
</table>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>Will not be touched</div>
2+
3+
<div data-pjax-container>
4+
<%= content_tag :h3, 'Pjax container' %>
5+
<%= link_to 'plainText', '/capybara/plain_text' %>
6+
<%= link_to 'htmlContent', '/capybara/html_content' %>
7+
<%= link_to 'fullReload', '/capybara/plain_text', 'data-skip-pjax' => true %>
8+
<%= link_to 'preventsPjax', '/capybara/prevents_pjax' %>
9+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Plain text
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevents pjax
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_layout#index
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_layout#prevent_pjax
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dummy</title>
5+
<%= javascript_include_tag 'application' %>
6+
</head>
7+
<body>
8+
9+
layouts/application <%= yield %>
10+
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dummy</title>
5+
<%= javascript_include_tag 'application' %>
6+
</head>
7+
<body>
8+
9+
layouts/with_layout <%= yield %>
10+
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
with_layout#index

test/dummy/config.ru

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require ::File.expand_path('../config/environment', __FILE__)
2+
3+
run Rails.application

test/dummy/config/application.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require File.expand_path('../boot', __FILE__)
2+
3+
require 'rails'
4+
require 'action_controller/railtie'
5+
require 'sprockets/railtie'
6+
7+
require 'jquery-rails'
8+
require 'pjax_rails'
9+
10+
Bundler.require(:default, Rails.env)
11+
12+
module Dummy
13+
class Application < Rails::Application
14+
config.secret_token = 'a966a729a228e5d3edf00997e7b7eab7'
15+
config.assets.enabled = true
16+
end
17+
end

test/dummy/config/boot.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
2+
3+
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

test/dummy/config/environment.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Load the Rails application.
2+
require File.expand_path('../application', __FILE__)
3+
4+
# Initialize the Rails application.
5+
Dummy::Application.initialize!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Dummy::Application.configure do
2+
config.cache_classes = false
3+
4+
config.eager_load = false
5+
6+
config.consider_all_requests_local = true
7+
config.action_controller.perform_caching = false
8+
9+
config.active_support.deprecation = :log
10+
11+
config.assets.debug = true
12+
end
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Dummy::Application.configure do
2+
config.cache_classes = true
3+
4+
config.eager_load = false
5+
6+
config.serve_static_assets = true
7+
config.static_cache_control = "public, max-age=3600"
8+
9+
config.consider_all_requests_local = true
10+
config.action_controller.perform_caching = false
11+
12+
config.action_dispatch.show_exceptions = false
13+
14+
config.action_controller.allow_forgery_protection = false
15+
16+
config.active_support.deprecation = :notify
17+
end

test/dummy/config/routes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Dummy::Application.routes.draw do
2+
get '/:controller(/:action(/:id))'
3+
end

test/features/pjax_test.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'test_helper'
2+
3+
class PjaxTest < ActiveSupport::IntegrationCase
4+
test 'loads plain text' do
5+
visit '/capybara'
6+
click_on 'plainText'
7+
assert page.has_content?('Will not be touched')
8+
assert page.has_no_content?('Pjax container')
9+
assert page.has_content?('Plain text')
10+
end
11+
12+
test 'loads html' do
13+
visit '/capybara'
14+
click_on 'htmlContent'
15+
assert page.has_content?('Will not be touched')
16+
assert page.has_no_content?('Pjax container')
17+
assert_equal page.find(:xpath, '//table/tbody/tr/td').text, 'Html content'
18+
end
19+
20+
test 'fully reloads page' do
21+
visit '/capybara'
22+
click_on 'fullReload'
23+
assert page.has_no_content?('Will not be touched')
24+
assert page.has_content?('layouts/application Plain text')
25+
end
26+
27+
test 'prevents pjax' do
28+
visit '/capybara'
29+
click_on 'preventsPjax'
30+
assert page.has_no_content?('Will not be touched')
31+
assert page.has_content?('layouts/application Prevents pjax')
32+
end
33+
end

test/gemfiles/Gemfile.rails-3.1.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source 'https://rubygems.org'
22
gemspec :path => './../..'
33

4-
gem 'railties', '~> 3.1.0'
4+
gem 'rails', '~> 3.1.0'

test/gemfiles/Gemfile.rails-3.2.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source 'https://rubygems.org'
22
gemspec :path => './../..'
33

4-
gem 'railties', '~> 3.2.0'
4+
gem 'rails', '~> 3.2.0'

test/gemfiles/Gemfile.rails-4.0.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source 'https://rubygems.org'
22
gemspec :path => './../..'
33

4-
gem 'railties', '~> 4.0.0'
4+
gem 'rails', '~> 4.0.0'

test/test_helper.rb

+11-40
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,17 @@
1+
ENV['RAILS_ENV'] ||= 'test'
2+
13
require 'test/unit'
2-
require 'rails'
3-
require 'action_controller'
4-
require 'pjax_rails'
4+
require 'dummy/config/environment'
55
require 'rails/test_help'
6+
require 'capybara/poltergeist'
67

7-
# Ruby 1.8 doesn't call self.inherited inside Class.new
8-
# Config and routes are unreacheable inside the block.
9-
app = Class.new(Rails::Application)
10-
11-
app.configure do
12-
config.active_support.deprecation = :notify
13-
config.secret_token = 'a966a729a228e5d3edf00997e7b7eab7'
14-
config.eager_load = false
15-
16-
routes {
17-
get '/:controller(/:action(/:id))'
18-
}
19-
20-
routes.finalize!
21-
end
22-
23-
app.initialize!
24-
25-
require 'action_view/testing/resolvers'
8+
Capybara.app = Rails.application
9+
Capybara.default_driver = :poltergeist
10+
class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
11+
include Capybara::DSL
2612

27-
class ApplicationController < ActionController::Base
28-
include Rails.application.routes.url_helpers
29-
end
30-
31-
class PjaxController < ApplicationController
32-
append_view_path ActionView::FixtureResolver.new('layouts/application.html.erb' => 'layouts/application <%= yield %>')
33-
append_view_path ActionView::FixtureResolver.new('pjax/index.html.erb' => 'pjax#index')
34-
append_view_path ActionView::FixtureResolver.new('pjax/prevent_pjax.html.erb' => 'pjax#prevent_pjax')
35-
36-
def prevent_pjax
37-
prevent_pjax!
13+
def teardown
14+
Capybara.reset_sessions!
15+
Capybara.use_default_driver
3816
end
3917
end
40-
41-
class WithLayoutController < ApplicationController
42-
layout 'with_layout'
43-
44-
append_view_path ActionView::FixtureResolver.new('layouts/with_layout.html.erb' => 'layouts/with_layout <%= yield %>')
45-
append_view_path ActionView::FixtureResolver.new('with_layout/index.html.erb' => 'with_layout#index')
46-
end

0 commit comments

Comments
 (0)