class JavaScriptGeneratorTest

Public Instance Methods

_evaluate_assigns_and_ivars() click to toggle source
# File test/template/prototype_helper_test.rb, line 142
def _evaluate_assigns_and_ivars() end
setup() click to toggle source
# File test/template/prototype_helper_test.rb, line 132
def setup
  super
  @generator = create_generator
  ActiveSupport.escape_html_entities_in_json  = true
end
teardown() click to toggle source
# File test/template/prototype_helper_test.rb, line 138
def teardown
  ActiveSupport.escape_html_entities_in_json  = false
end
test_alert() click to toggle source
# File test/template/prototype_helper_test.rb, line 193
def test_alert
  assert_equal 'alert("hello");', @generator.alert('hello')
end
test_call_with_block() click to toggle source
# File test/template/prototype_helper_test.rb, line 456
def test_call_with_block
  @generator.call(:before)
  @generator.call(:my_method) do |p|
    p[:one].show
    p[:two].hide
  end
  @generator.call(:in_between)
  @generator.call(:my_method_with_arguments, true, "hello") do |p|
    p[:three].visual_effect(:highlight)
  end
  assert_equal "before();\nmy_method(function() { $(\"one\").show();\n$(\"two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"three\").visualEffect(\"highlight\"); });", @generator.to_s
end
test_class_proxy() click to toggle source
# File test/template/prototype_helper_test.rb, line 451
def test_class_proxy
  @generator.form.focus('my_field')
  assert_equal "Form.focus(\"my_field\");", @generator.to_s
end
test_class_proxy_call_with_block() click to toggle source
# File test/template/prototype_helper_test.rb, line 469
def test_class_proxy_call_with_block
  @generator.my_object.my_method do |p|
    p[:one].show
    p[:two].hide
  end
  assert_equal "MyObject.myMethod(function() { $(\"one\").show();\n$(\"two\").hide(); });", @generator.to_s
end
test_collection_first_and_last() click to toggle source
# File test/template/prototype_helper_test.rb, line 303
  def test_collection_first_and_last
    @generator.select('p.welcome b').first.hide()
    @generator.select('p.welcome b').last.show()
    assert_equal "$$("p.welcome b").first().hide();
$$("p.welcome b").last().show();
".strip, @generator.to_s
  end
test_collection_proxy_on_collect() click to toggle source
# File test/template/prototype_helper_test.rb, line 329
  def test_collection_proxy_on_collect
    @generator.select('p').collect('a') { |para| para.show }
    @generator.select('p').collect { |para| para.hide }
    assert_equal "var a = $$("p").collect(function(value, index) {
return value.show();
});
$$("p").collect(function(value, index) {
return value.hide();
});
".strip, @generator.to_s
    @generator = create_generator
  end
test_collection_proxy_with_each() click to toggle source
# File test/template/prototype_helper_test.rb, line 312
  def test_collection_proxy_with_each
    @generator.select('p.welcome b').each do |value|
      value.remove_class_name 'selected'
    end
    @generator.select('p.welcome b').each do |value, index|
      @generator.visual_effect :highlight, value
    end
    assert_equal "$$("p.welcome b").each(function(value, index) {
value.removeClassName("selected");
});
$$("p.welcome b").each(function(value, index) {
new Effect.Highlight(value,{});
});
".strip, @generator.to_s
  end
test_collection_proxy_with_each_slice() click to toggle source
# File test/template/prototype_helper_test.rb, line 423
  def test_collection_proxy_with_each_slice
    @generator.select('p').each_slice('a', 3)
    @generator.select('p').each_slice('a', 3) do |group, index|
      group.reverse
    end

    assert_equal "var a = $$("p").eachSlice(3);
var a = $$("p").eachSlice(3, function(value, index) {
return value.reverse();
});
".strip, @generator.to_s
  end
test_collection_proxy_with_find_all() click to toggle source
# File test/template/prototype_helper_test.rb, line 402
  def test_collection_proxy_with_find_all
    @generator.select('p').find_all 'a' do |value, index|
      @generator << '(value.className == "welcome")'
    end

    assert_equal "var a = $$("p").findAll(function(value, index) {
return (value.className == "welcome");
});
".strip, @generator.to_s
  end
test_collection_proxy_with_grep() click to toggle source
# File test/template/prototype_helper_test.rb, line 343
  def test_collection_proxy_with_grep
    @generator.select('p').grep 'a', /^a/ do |value|
      @generator << '(value.className == "welcome")'
    end
    @generator.select('p').grep 'b', /b$/ do |value, index|
      @generator.call 'alert', value
      @generator << '(value.className == "welcome")'
    end

    assert_equal "var a = $$("p").grep(/^a/, function(value, index) {
return (value.className == "welcome");
});
var b = $$("p").grep(/b$/, function(value, index) {
alert(value);
return (value.className == "welcome");
});
".strip, @generator.to_s
  end
test_collection_proxy_with_in_groups_of() click to toggle source
# File test/template/prototype_helper_test.rb, line 414
  def test_collection_proxy_with_in_groups_of
    @generator.select('p').in_groups_of('a', 3)
    @generator.select('p').in_groups_of('a', 3, 'x')
    assert_equal "var a = $$("p").inGroupsOf(3);
var a = $$("p").inGroupsOf(3, "x");
".strip, @generator.to_s
  end
test_collection_proxy_with_inject() click to toggle source
# File test/template/prototype_helper_test.rb, line 363
  def test_collection_proxy_with_inject
    @generator.select('p').inject 'a', [] do |memo, value|
      @generator << '(value.className == "welcome")'
    end
    @generator.select('p').inject 'b', nil do |memo, value, index|
      @generator.call 'alert', memo
      @generator << '(value.className == "welcome")'
    end

    assert_equal "var a = $$("p").inject([], function(memo, value, index) {
return (value.className == "welcome");
});
var b = $$("p").inject(null, function(memo, value, index) {
alert(memo);
return (value.className == "welcome");
});
".strip, @generator.to_s
  end
test_collection_proxy_with_pluck() click to toggle source
# File test/template/prototype_helper_test.rb, line 383
def test_collection_proxy_with_pluck
  @generator.select('p').pluck('a', 'className')
  assert_equal %Q(var a = $$("p").pluck("className");), @generator.to_s
end
test_collection_proxy_with_zip() click to toggle source
# File test/template/prototype_helper_test.rb, line 388
  def test_collection_proxy_with_zip
    ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('a', [4, 5, 6], [7, 8, 9])
    ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('b', [4, 5, 6], [7, 8, 9]) do |array|
      @generator.call 'array.reverse'
    end

    assert_equal "var a = [1, 2, 3].zip([4,5,6], [7,8,9]);
var b = [1, 2, 3].zip([4,5,6], [7,8,9], function(array) {
return array.reverse();
});
".strip, @generator.to_s
  end
test_debug_rjs() click to toggle source
# File test/template/prototype_helper_test.rb, line 437
def test_debug_rjs
  ActionView::Base.debug_rjs = true
  @generator['welcome'].replace_html 'Welcome'
  assert_equal "try {\n$(\"welcome\").update(\"Welcome\");\n} catch (e) { alert('RJS error:\\n\\n' + e.toString()); alert('$(\\\"welcome\\\").update(\\\"Welcome\\\");'); throw e }", @generator.to_s
ensure
  ActionView::Base.debug_rjs = false
end
test_delay() click to toggle source
# File test/template/prototype_helper_test.rb, line 209
def test_delay
  @generator.delay(20) do
    @generator.hide('foo')
  end

  assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
end
test_draggable() click to toggle source
# File test/template/prototype_helper_test.rb, line 291
def test_draggable
  assert_equal %Q(new Draggable("blah", {});),
    @generator.draggable('blah')
end
test_drop_receiving() click to toggle source
# File test/template/prototype_helper_test.rb, line 296
def test_drop_receiving
  assert_equal %Q(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
    @generator.drop_receiving('blah', :url => { :action => "order" })
  assert_equal %Q(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
    @generator.drop_receiving('blah', :url => { :action => "order" }, :type => :synchronous)
end
test_element_access() click to toggle source
# File test/template/prototype_helper_test.rb, line 231
def test_element_access
  assert_equal %Q($("hello");), @generator['hello']
end
test_element_access_on_records() click to toggle source
# File test/template/prototype_helper_test.rb, line 235
def test_element_access_on_records
  assert_equal %Q($("bunny_5");),   @generator[Bunny.new(:id => 5)]
  assert_equal %Q($("new_bunny");), @generator[Bunny.new]
end
test_element_proxy_assignment() click to toggle source
# File test/template/prototype_helper_test.rb, line 255
def test_element_proxy_assignment
  @generator['hello'].width = 400
  assert_equal %Q($("hello").width = 400;), @generator.to_s
end
test_element_proxy_one_deep() click to toggle source
# File test/template/prototype_helper_test.rb, line 240
def test_element_proxy_one_deep
  @generator['hello'].hide
  assert_equal %Q($("hello").hide();), @generator.to_s
end
test_element_proxy_two_deep() click to toggle source
# File test/template/prototype_helper_test.rb, line 260
def test_element_proxy_two_deep
  @generator['hello'].hide("first").clean_whitespace
  assert_equal %Q($("hello").hide("first").cleanWhitespace();), @generator.to_s
end
test_element_proxy_variable_access() click to toggle source
# File test/template/prototype_helper_test.rb, line 245
def test_element_proxy_variable_access
  @generator['hello']['style']
  assert_equal %Q($("hello").style;), @generator.to_s
end
test_element_proxy_variable_access_with_assignment() click to toggle source
# File test/template/prototype_helper_test.rb, line 250
def test_element_proxy_variable_access_with_assignment
  @generator['hello']['style']['color'] = 'red'
  assert_equal %Q($("hello").style.color = "red";), @generator.to_s
end
test_hide() click to toggle source
# File test/template/prototype_helper_test.rb, line 179
def test_hide
  assert_equal 'Element.hide("foo");',
    @generator.hide('foo')
  assert_equal '["foo","bar","baz"].each(Element.hide);',
    @generator.hide('foo', 'bar', 'baz')
end
test_insert_html_with_string() click to toggle source
# File test/template/prototype_helper_test.rb, line 144
def test_insert_html_with_string
  assert_equal 'Element.insert("element", { top: "\u003Cp\u003EThis is a test\u003C/p\u003E" });',
    @generator.insert_html(:top, 'element', '<p>This is a test</p>')
  assert_equal 'Element.insert("element", { bottom: "\u003Cp\u003EThis is a test\u003C/p\u003E" });',
    @generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
  assert_equal 'Element.insert("element", { before: "\u003Cp\u003EThis is a test\u003C/p\u003E" });',
    @generator.insert_html(:before, 'element', '<p>This is a test</p>')
  assert_equal 'Element.insert("element", { after: "\u003Cp\u003EThis is a test\u003C/p\u003E" });',
    @generator.insert_html(:after, 'element', '<p>This is a test</p>')
end
test_literal() click to toggle source
# File test/template/prototype_helper_test.rb, line 445
def test_literal
  literal = @generator.literal("function() {}")
  assert_equal "function() {}", ActiveSupport::JSON.encode(literal)
  assert_equal "", @generator.to_s
end
test_redirect_to() click to toggle source
# File test/template/prototype_helper_test.rb, line 197
def test_redirect_to
  assert_equal 'window.location.href = "http://www.example.com/welcome";',
    @generator.redirect_to(:action => 'welcome')
  assert_equal 'window.location.href = "http://www.example.com/welcome?a=b&c=d";',
    @generator.redirect_to("http://www.example.com/welcome?a=b&c=d")
end
test_reload() click to toggle source
# File test/template/prototype_helper_test.rb, line 204
def test_reload
  assert_equal 'window.location.reload();',
    @generator.reload
end
test_remove() click to toggle source
# File test/template/prototype_helper_test.rb, line 165
def test_remove
  assert_equal 'Element.remove("foo");',
    @generator.remove('foo')
  assert_equal '["foo","bar","baz"].each(Element.remove);',
    @generator.remove('foo', 'bar', 'baz')
end
test_replace_element_with_string() click to toggle source
# File test/template/prototype_helper_test.rb, line 160
def test_replace_element_with_string
  assert_equal 'Element.replace("element", "\u003Cdiv id=\"element\"\u003E\u003Cp\u003EThis is a test\u003C/p\u003E\u003C/div\u003E");',
    @generator.replace('element', '<div id="element"><p>This is a test</p></div>')
end
test_replace_html_with_string() click to toggle source
# File test/template/prototype_helper_test.rb, line 155
def test_replace_html_with_string
  assert_equal 'Element.update("element", "\u003Cp\u003EThis is a test\u003C/p\u003E");',
    @generator.replace_html('element', '<p>This is a test</p>')
end
test_select_access() click to toggle source
# File test/template/prototype_helper_test.rb, line 265
def test_select_access
  assert_equal %Q($$("div.hello");), @generator.select('div.hello')
end
test_select_proxy_one_deep() click to toggle source
# File test/template/prototype_helper_test.rb, line 269
def test_select_proxy_one_deep
  @generator.select('p.welcome b').first.hide
  assert_equal %Q($$("p.welcome b").first().hide();), @generator.to_s
end
test_show() click to toggle source
# File test/template/prototype_helper_test.rb, line 172
def test_show
  assert_equal 'Element.show("foo");',
    @generator.show('foo')
  assert_equal '["foo","bar","baz"].each(Element.show);',
    @generator.show('foo', 'bar', 'baz')
end
test_sortable() click to toggle source
# File test/template/prototype_helper_test.rb, line 284
def test_sortable
  assert_equal %Q(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
    @generator.sortable('blah', :url => { :action => "order" })
  assert_equal %Q(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
    @generator.sortable('blah', :url => { :action => "order" }, :type => :synchronous)
end
test_to_s() click to toggle source
# File test/template/prototype_helper_test.rb, line 217
  def test_to_s
    @generator.insert_html(:top, 'element', '<p>This is a test</p>')
    @generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
    @generator.remove('foo', 'bar')
    @generator.replace_html('baz', '<p>This is a test</p>')

    assert_equal "Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
Element.insert("element", { bottom: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
["foo","bar"].each(Element.remove);
Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
".chomp, @generator.to_s
  end
test_toggle() click to toggle source
# File test/template/prototype_helper_test.rb, line 186
def test_toggle
  assert_equal 'Element.toggle("foo");',
    @generator.toggle('foo')
  assert_equal '["foo","bar","baz"].each(Element.toggle);',
    @generator.toggle('foo', 'bar', 'baz')
end
test_visual_effect() click to toggle source
# File test/template/prototype_helper_test.rb, line 274
def test_visual_effect
  assert_equal %Q(new Effect.Puff("blah",{});),
    @generator.visual_effect(:puff,'blah')
end
test_visual_effect_toggle() click to toggle source
# File test/template/prototype_helper_test.rb, line 279
def test_visual_effect_toggle
  assert_equal %Q(Effect.toggle("blah",'appear',{});),
    @generator.visual_effect(:toggle_appear,'blah')
end