From c1c25286168c99a78860dd5ea1f1917d972cb2cb Mon Sep 17 00:00:00 2001 From: kbparagua Date: Sat, 15 Feb 2014 17:32:32 +0800 Subject: [PATCH] Basic spec and param fix --- lib/paloma/action_controller_extension.rb | 2 +- .../app/assets/javascripts/application.js | 18 +- test_app/app/controllers/main_controller.rb | 46 +++- test_app/config/routes.rb | 4 +- .../spec/controllers/main_controller_spec.rb | 5 +- test_app/spec/integration/main_spec.rb | 254 +++++++++++------- 6 files changed, 221 insertions(+), 108 deletions(-) diff --git a/lib/paloma/action_controller_extension.rb b/lib/paloma/action_controller_extension.rb index 5b152fa..e68660e 100644 --- a/lib/paloma/action_controller_extension.rb +++ b/lib/paloma/action_controller_extension.rb @@ -89,7 +89,7 @@ module Paloma # :param_1 => 1, :param_2 => 2 elsif path_or_options.is_a? Hash - self.paloma.params.merge! path_or_options[:params] || {} + self.paloma.params.merge! path_or_options || {} end end diff --git a/test_app/app/assets/javascripts/application.js b/test_app/app/assets/javascripts/application.js index a99ed2a..e24b9af 100644 --- a/test_app/app/assets/javascripts/application.js +++ b/test_app/app/assets/javascripts/application.js @@ -16,21 +16,23 @@ //= require_tree . -// Will be manipulated by Paloma controllers. -window.called = []; - - // // // Controllers // // -var Main = Paloma.controller('Main'); -Main.prototype.index = function(){ - window.called.push('Main#index'); -}; +var Main = Paloma.controller('Main'); +Main.prototype.index = function(){}; +Main.prototype.otherAction = function(){}; +Main.prototype.prevent = function(){}; +Main.prototype.basic_params = function(){}; + + +var OtherMain = Paloma.controller('OtherMain'); +OtherMain.prototype.show = function(){}; +OtherMain.prototype.otherAction = function(){}; diff --git a/test_app/app/controllers/main_controller.rb b/test_app/app/controllers/main_controller.rb index 130a9f9..051729f 100644 --- a/test_app/app/controllers/main_controller.rb +++ b/test_app/app/controllers/main_controller.rb @@ -1,14 +1,52 @@ class MainController < ApplicationController - js :params => {:y => 300}, :only => :index - - + # Default behavior def index - js 'MainShit', :params => {:z => 2} render :inline => 'Main#index', :layout => 'application' end + # Override controller + def show + js 'OtherMain' + render :inline => 'Main#show', :layout => 'application' + end + + + # Override action + def new + js :otherAction + render :inline => 'Main#new', :layout => 'application' + end + + + # Override controller/action + def edit + js 'OtherMain#otherAction' + render :inline => 'Main#edit', :layout => 'application' + end + + + # Stop paloma from execution + def prevent + js false + render :inline => 'Main#prevent', :layout => 'application' + end + + + def basic_params + js :x => 1, :y => 2 + render :inline => 'Main#basic_params', :layout => 'application' + end + + + + + + # + # Non-HTML response + # + def json_response render :json => {:x => 1} end diff --git a/test_app/config/routes.rb b/test_app/config/routes.rb index 1a3b08c..a0511cb 100644 --- a/test_app/config/routes.rb +++ b/test_app/config/routes.rb @@ -5,7 +5,9 @@ TestApp::Application.routes.draw do root :to => 'main#index' resources :main, :controller => 'Main' do - member do + collection do + get :prevent + get :basic_params get :json_response get :js_response get :xml_response diff --git a/test_app/spec/controllers/main_controller_spec.rb b/test_app/spec/controllers/main_controller_spec.rb index 9fce7e4..19bfc21 100644 --- a/test_app/spec/controllers/main_controller_spec.rb +++ b/test_app/spec/controllers/main_controller_spec.rb @@ -4,9 +4,10 @@ require 'spec_helper' describe MainController do context 'default behavior' do - it 'test' do + it 'executes the same controller/action pair' do get :index - raise MainController.instance + x = page.evaluate_script 'Paloma.engine.lastRequest' + raise x.inspect end end diff --git a/test_app/spec/integration/main_spec.rb b/test_app/spec/integration/main_spec.rb index 6ccc4d6..af00aee 100644 --- a/test_app/spec/integration/main_spec.rb +++ b/test_app/spec/integration/main_spec.rb @@ -3,101 +3,171 @@ require 'spec_helper' feature 'executing Paloma controller', :js => true do - describe 'after rendering' do + # + # + # Basic + # + # - it 'executes the corresponding Paloma controller action' do - visit foo_path(1) - called = page.evaluate_script 'window.called' + context 'default behavior' do + it 'executes the same controller/action' do + visit main_index_path + request = page.evaluate_script 'Paloma.engine.lastRequest' - expect(called).to include 'MyFoo#show' - end - - - context 'coming from a redirect' do - before { visit foo_index_path } - - it 'executes next the Paloma action of the last Rails action' do - last = page.evaluate_script 'window.called.pop()' - expect(last).to eq 'Main#index' - end - end - - - context 'when js params is passed' do - it 'passes the parameters to Paloma controller action' do - visit foo_path(1) - parameter = page.evaluate_script 'window.parameter' - - expect(parameter).to eq 'Parameter From Paloma' - end - end - - - context 'from namespaced controller' do - it 'executes the corresponding Paloma controller action' do - visit admin_bar_path(1) - called = page.evaluate_script 'window.called.pop()' - - expect(called).to eq 'Admin/Bar#show' - end - end - - - context 'when js(false) is triggered' do - it 'does not append paloma hook' do - visit edit_foo_path(1) - - page.should_not have_selector '.js-paloma-hook' - end - end - - - context 'when requests from a controller with multiple name' do - it 'executes the corresponding Paloma action' do - visit multiple_names_path - - called = page.evaluate_script 'window.called.pop()' - - expect(called).to eq 'MultipleNames#index' - end - end - - - # - # [TODO] - # Create proper test for the following: - # - - context 'when rendering json' do - it 'does not execute Paloma' do - visit json_response_main_path 1 - page.should_not have_selector '.js-paloma-hook' - end - end - - - context 'when rendering js' do - it 'does not execute Paloma' do - visit js_response_main_path 1 - page.should_not have_selector '.js-paloma-hook' - end - end - - - context 'when rendering xml' do - it 'does not execute Paloma' do - visit xml_response_main_path 1 - page.should_not have_selector '.js-paloma-hook' - end - end - - - context 'when rendering a file' do - it 'does not execute Paloma' do - visit file_response_main_path 1 - page.should_not have_selector '.js-paloma-hook' - end + expect(request).to eq({ + 'controller' => 'Main', + 'action' => 'index', + 'params' => {}}) end end + + context 'override default controller' do + it 'executes the specified controller' do + visit main_path(1) + request = page.evaluate_script 'Paloma.engine.lastRequest' + + expect(request).to eq({ + 'controller' => 'OtherMain', + 'action' => 'show', + 'params' => {}}) + end + end + + + context 'override default action' do + it 'executes the specified action' do + visit new_main_path + request = page.evaluate_script 'Paloma.engine.lastRequest' + + expect(request).to eq({ + 'controller' => 'Main', + 'action' => 'otherAction', + 'params' => {}}) + end + end + + + context 'override default controller/action' do + it 'executes the specified controller/action' do + visit edit_main_path(1) + request = page.evaluate_script 'Paloma.engine.lastRequest' + + expect(request).to eq({ + 'controller' => 'OtherMain', + 'action' => 'otherAction', + 'params' => {}}) + end + end + + + context 'parameter passed' do + it 'passes the parameter' do + visit basic_params_main_index_path + params = page.evaluate_script 'Paloma.engine.lastRequest.params' + + expect(params).to eq({'x' => 1, 'y' => 2}) + end + end + + + + + + # + # + # Prevent Paloma + # + # + + shared_examples 'no paloma' do + it 'does not add paloma hook' do + expect(page.has_selector?('.js-paloma-hook')).to be_false + end + end + + + context 'js(false)' do + before { visit prevent_main_index_path } + + include_examples 'no paloma' + + it 'prevents execution of Paloma controller' do + request = page.evaluate_script 'Paloma.engine.lastRequest' + expect(request).to be_nil + end + end + + context 'json response' do + before { visit json_response_main_index_path } + include_examples 'no paloma' + end + + context 'js response' do + before { visit js_response_main_index_path } + include_examples 'no paloma' + end + + context 'xml response' do + before { visit xml_response_main_index_path } + include_examples 'no paloma' + end + + context 'file response' do + before { visit file_response_main_index_path } + include_examples 'no paloma' + end + + + # context 'coming from a redirect' do + # before { visit foo_index_path } + + # it 'executes next the Paloma action of the last Rails action' do + # last = page.evaluate_script 'window.called.pop()' + # expect(last).to eq 'Main#index' + # end + # end + + + # context 'when js params is passed' do + # it 'passes the parameters to Paloma controller action' do + # visit foo_path(1) + # parameter = page.evaluate_script 'window.parameter' + + # expect(parameter).to eq 'Parameter From Paloma' + # end + # end + + + # context 'from namespaced controller' do + # it 'executes the corresponding Paloma controller action' do + # visit admin_bar_path(1) + # called = page.evaluate_script 'window.called.pop()' + + # expect(called).to eq 'Admin/Bar#show' + # end + # end + + + # context 'when js(false) is triggered' do + # it 'does not append paloma hook' do + # visit edit_foo_path(1) + + # page.should_not have_selector '.js-paloma-hook' + # end + # end + + + # context 'when requests from a controller with multiple name' do + # it 'executes the corresponding Paloma action' do + # visit multiple_names_path + + # called = page.evaluate_script 'window.called.pop()' + + # expect(called).to eq 'MultipleNames#index' + # end + # end + + + end