Permalink
Browse files

Add controller's tests

  • Loading branch information...
1 parent 90f9571 commit e9f3c1cdfb9c33a557b47a832a4a3065661828f6 @route route committed Aug 13, 2013
View
@@ -1 +1,2 @@
-*.gem
+*.gem
+Gemfile.lock
View
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
View
@@ -1,3 +1,12 @@
+require 'rake/testtask'
+
+task :default => :test
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
desc 'Update jquery-pjax to last version'
task :update do
require 'open-uri'
View
@@ -2,7 +2,7 @@
module PjaxRails
class Engine < ::Rails::Engine
- initializer "pjax_rails.add_controller" do
+ initializer 'pjax_rails.add_controller' do
ActiveSupport.on_load :action_controller do
ActionController::Base.send :include, Pjax
end
View
@@ -5,7 +5,10 @@ Gem::Specification.new do |s|
s.email = 'david@loudthinking.com'
s.summary = 'PJAX integration for Rails 3.1+'
+ s.files = Dir['lib/**/*.rb', 'lib/**/*.js', 'vendor/**/*.js']
+
+ s.add_dependency 'railties', '>= 3.1', '< 5.0'
s.add_dependency 'jquery-rails'
- s.files = Dir["lib/**/*.rb", "lib/**/*.js", "vendor/**/*.js"]
+ s.add_development_dependency 'rake'
end
@@ -0,0 +1,47 @@
+require 'test_helper'
+
+class PjaxControllerTest < ActionController::TestCase
+ test 'renders without layout' do
+ request.env['HTTP_X_PJAX'] = true
+
+ get :index
+
+ assert_equal 'pjax#index', response.body
+ end
+
+ test 'renders with default layout' do
+ get :index
+
+ assert_equal 'layouts/application pjax#index', response.body
+ end
+
+ test 'prevents pjax' do
+ request.env['HTTP_X_PJAX'] = true
+
+ get :prevent_pjax
+
+ assert_equal 406, response.status
+ end
+
+ test 'strips pjax params' do
+ request.env['HTTP_X_PJAX'] = true
+
+ get :index, '_pjax' => true
+
+ assert_equal({ 'controller' => 'pjax', 'action' => 'index' }, @controller.params)
+ assert_equal '', request.env['QUERY_STRING']
+ assert_nil request.env['rack.request.query_string']
+ assert_nil request.env['rack.request.query_hash']
+ assert_nil request.env['action_dispatch.request.query_parameters']
+ assert_equal '/pjax', request.original_fullpath
+ assert_equal '/pjax', request.fullpath
+ end
+
+ test 'sets pjax url' do
+ request.env['HTTP_X_PJAX'] = true
+
+ get :index
+
+ assert_equal 'http://test.host/pjax', response.headers['X-PJAX-URL']
+ end
+end
@@ -0,0 +1,17 @@
+require 'test_helper'
+
+class WithLayoutControllerTest < ActionController::TestCase
+ test 'renders with layout for pjax request' do
+ request.env['HTTP_X_PJAX'] = true
+
+ get :index
+
+ assert_equal 'layouts/with_layout with_layout#index', response.body
+ end
+
+ test 'renders with layout for regular request' do
+ get :index
+
+ assert_equal 'layouts/with_layout with_layout#index', response.body
+ end
+end
View
@@ -0,0 +1,42 @@
+require 'test/unit'
+require 'rails'
+require 'action_controller'
+require 'pjax_rails'
+require 'rails/test_help'
+
+Class.new(Rails::Application) do |app|
+ app.configure do
+ config.active_support.deprecation = :notify
+ config.secret_token = 'a966a729a228e5d3edf00997e7b7eab7'
+ config.eager_load = false
+
+ routes {
+ get '/:controller(/:action(/:id))'
+ }
+ end
+
+ app.initialize!
+end
+
+require 'action_view/testing/resolvers'
+
+class ApplicationController < ActionController::Base
+ include Rails.application.routes.url_helpers
+end
+
+class PjaxController < ApplicationController
+ append_view_path ActionView::FixtureResolver.new('layouts/application.html.erb' => 'layouts/application <%= yield %>')
+ append_view_path ActionView::FixtureResolver.new('pjax/index.html.erb' => 'pjax#index')
+ append_view_path ActionView::FixtureResolver.new('pjax/prevent_pjax.html.erb' => 'pjax#prevent_pjax')
+
+ def prevent_pjax
+ prevent_pjax!
+ end
+end
+
+class WithLayoutController < ApplicationController
+ layout 'with_layout'
+
+ append_view_path ActionView::FixtureResolver.new('layouts/with_layout.html.erb' => 'layouts/with_layout <%= yield %>')
+ append_view_path ActionView::FixtureResolver.new('with_layout/index.html.erb' => 'with_layout#index')
+end

0 comments on commit e9f3c1c

Please sign in to comment.