Skip to content

Commit

Permalink
Tests for CtG helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Guidi committed Apr 6, 2009
1 parent 07bcd35 commit af269d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/helpers/click_to_globalize_helper.rb
@@ -1,9 +1,9 @@
module ClickToGlobalizeHelper
@@click_partial = 'shared/_click_to_globalize'
@@click_partial = File.expand_path(File.dirname(__FILE__) + "/../views/shared/_click_to_globalize.html.erb")

# Render +app/views/shared/_click_to_globalize.html.erb+.
def click_to_globalize
render @@click_partial if controller.globalize?
render :file => @@click_partial if globalize?
end

# Get form_authenticity_token if the application is protected from forgery.
Expand Down
5 changes: 4 additions & 1 deletion lib/click_to_globalize.rb
Expand Up @@ -13,7 +13,7 @@ def self.included(recipient)

module InstanceMethods
protected
def render_with_observe(options = nil, extra_options = {}, &block)
def render_with_observe(options = nil, extra_options = {}, &block)
locale_observer = Click::Observer::LocaleObserver.new
@template.add_observer locale_observer
render_without_observe(options, extra_options, &block)
Expand Down Expand Up @@ -77,6 +77,9 @@ def update(key, result) #:nodoc:
module Controller
def self.included(recipient)
recipient.send :include, InstanceMethods
recipient.class_eval do
helper_method :globalize?
end
end

module InstanceMethods
Expand Down
11 changes: 9 additions & 2 deletions test/test_helper.rb
Expand Up @@ -15,14 +15,21 @@ def locale_observer
end

class ActionView::TestCase
# HACK all this methods are placeholders, dunno why they aren't included by default
private
# HACK this is a placehoder, dunno why it isn't included by default
def protect_against_forgery?
false
end

# HACK this is a placehoder, dunno why it isn't included by default
def form_authenticity_token
"hack"
end

def render(options = nil, extra_options = {}, &block)
ActionView::Base.new.render options, extra_options, &block
end

def globalize?
ApplicationController.new.globalize?
end
end
17 changes: 16 additions & 1 deletion test/unit/helpers/click_to_globalize_helper_test.rb
@@ -1,6 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')

class ClickToGlobalizeHelperTest < ActionView::TestCase
class ClickToGlobalizeHelperTest < ActionView::TestCase
setup :stubs_action_view_methods

test "authenticity_token with protection from forgery active" do
stubs(:protect_against_forgery?).returns true
assert_equal form_authenticity_token, authenticity_token
Expand All @@ -9,4 +11,17 @@ class ClickToGlobalizeHelperTest < ActionView::TestCase
test "authenticity_token with protection from forgery inactive" do
assert_equal "", authenticity_token
end

test "click_to_globalize" do
expected = %(<script type=\"text/javascript\" src=\"/javascripts/click_to_globalize.js\"></script>\n<link href=\"/stylesheets/click_to_globalize.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" />\n<div id=\"click_to_globalize\"><ul><li><a href=\"/locales/en/change\" title=\"en\" onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m);f.submit();return false;\">en</a></li></ul></div>\n<script language=\"javascript\" type=\"text/javascript\" charset=\"utf-8\">\n// <![CDATA[\n var ctg = new ClickToGlobalize('hack', 'hackhack');\n// ]]>\n</script>\n)
assert_dom_equal expected, click_to_globalize
end

private
def stubs_action_view_methods
html = %(<ul><li><a href=\"/locales/en/change\" title=\"en\" onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m);f.submit();return false;\">en</a></li></ul>)
ActionView::Base.any_instance.stubs(:locales_menu).returns html
ActionView::Base.any_instance.stubs(:authenticity_token).returns "hack"
ActionView::Base.any_instance.stubs(:request_forgery_protection_token).returns "hackhack"
end
end

0 comments on commit af269d1

Please sign in to comment.