Permalink
Browse files

all tests pass using new jquery helpers

  • Loading branch information...
1 parent cb80a00 commit 22263c57e19131059dbccd82a5997b892b1a1126 @kristianmandrup committed Sep 5, 2012
View
@@ -8,6 +8,6 @@ task :default => :test
Rake::TestTask.new(:test) do |test|
test.libs << 'test'
- test.test_files = FileList['test/javascript_generator/*_test.rb'] # FileList['test/*_test.rb', 'test/rails/*_test.rb']
+ test.test_files = FileList['test/*_test.rb', 'test/rails/*_test.rb', 'test/javascript_generator/*_test.rb']
test.verbose = true
end
@@ -52,19 +52,20 @@ def element(selector)
"$(#{selector})"
end
- def update(id, markup); element(id) + '.html("'+escape(markup)+'");'; end
- def replace(id, markup); element(id) + '.replaceWith("'+escape(markup)+'");'; end
+ def update(*args, &block)
+ jq_helper.markup_act :html, *args, &block
+ end
+
+ def replace(*args, &block)
+ jq_helper.markup_act :replaceWith, *args, &block
+ end
def update_id(id, markup); update("##{id}", markup); end
def replace_id(id, markup); replace("##{id}", markup); end
- def update_text(id, selector, markup)
- element(id) + ".find(#{selector}).text('#{escape(markup)}');"
- end
-
def selector_for var, id, selector
raise ArgumentError, "Must not be an _apo_ selector here: #{selector}" if jq_helper.apo_match?(selector)
- "var _apo_#{var} = " + element("##{id}") + ".find('#{selector}');"
+ "var _apo_#{var} = " + element("##{id}") + ".find(\"#{selector}\");"
end
[:replace_all, :prepend_to, :append_to].each do |name|
@@ -73,7 +74,7 @@ def selector_for var, id, selector
end
end
- [:append, :prepend, :after, :before, :wrap, :wrap_inner, :wrap_all].each do |name|
+ [:update_text, :append, :prepend, :after, :before, :wrap, :wrap_inner, :wrap_all].each do |name|
define_method name do |args|
jq_helper.markup_action *args, name
end
@@ -5,7 +5,7 @@ def find_element id, selector
if id == nil || apo_selector?(selector)
return element(selector)
end
- element("##{id}") + ".find('#{selector}')"
+ element("##{id}") + ".find(\"#{selector}\")"
end
# - id, selector action
@@ -22,9 +22,18 @@ def inv_markup_action(selector, markup, action)
"$(#{escaped(markup)}).#{action}(#{selector});"
end
+ def markup_act name, *args, &block
+ id, selector = extract_args *args, &block
+ markup = block_given? ? yield : args.last
+
+ find_element(id, selector) + mk_action(name, markup)
+ end
+
+ # - id, selector, markup, action
+ # - selector, markup, action
def markup_action *args, &block
args = args.flatten
- id, selector, markup, action = extract_args(*args)
+ id, selector, markup, action = extract_args(*args, &block)
action ||= yield if block_given?
raise ArgumentError, "Must take action block or Symbol as last argument" unless action
elem_action = case action
@@ -37,12 +46,14 @@ def markup_action *args, &block
end
find_element(id, selector) + elem_action
end
-
+
+ include ::ActionView::Helpers::JavaScriptHelper
+
def escaped markup
- "'#{escape(markup)}'"
+ "\"#{escape_javascript(markup)}\""
end
- def mk_action name, markup
+ def mk_action name, markup
js_action "#{name}(#{escaped(markup)})"
end
@@ -51,11 +62,12 @@ def js_action action
end
def js_camelize str
- str.to_s.camelize.sub(/^\w/, s[0].downcase)
+ str = str.to_s
+ str.camelize.sub(/^\w/, str[0].downcase)
end
def calc_selector selector
- selector = apo_selector?(selector) ? "_apo_#{selector}" : "'#{selector}'"
+ selector = apo_selector?(selector) ? "_apo_#{selector}" : "\"#{selector}\""
end
def apo_selector? selector
@@ -74,8 +86,8 @@ def element(selector)
end
# id, selector, markup, action
- def extract_args *args
- [extract_id(*args), extract_selector(*args), extract_markup(*args), extract_action(*args)]
+ def extract_args *args, &block
+ [extract_id(*args), extract_selector(*args), extract_markup(*args, &block), extract_action(*args, &block)]
end
def extract_id *args
@@ -88,14 +100,12 @@ def extract_selector *args
args.size == 4 ? args[1] : args[0]
end
- def extract_markup(*args)
- args = args.flatten
- args.size == 4 ? args[2] : args.last
+ def extract_markup(*args, &block)
+ block_given? ? args.last : args[-2]
end
- def extract_action(*args)
- args = args.flatten
- args.size == 4 ? args.last : nil
+ def extract_action(*args, &block)
+ args.last unless block_given?
end
extend self
@@ -9,29 +9,51 @@ class JQueryHelperTest < Test::Unit::TestCase
context '#find_element' do
context 'args: id, selector' do
should "generate full action" do
- assert_equal "$('#my_widget').find('.item')", @helper.find_element('my_widget', '.item')
+ assert_equal "$(\"#my_widget\").find(\".item\")", @helper.find_element('my_widget', '.item')
end
end
context 'args: selector' do
should "generate selector only" do
- assert_equal "$('.item')", @helper.find_element(nil, '.item')
+ assert_equal "$(\".item\")", @helper.find_element(nil, '.item')
end
end
end
context '#jq_action' do
context 'args: id, selector, action' do
should "generate full action" do
- assert_equal "$('#my_widget').find('.item').empty();", @helper.jq_action('my_widget', '.item', 'empty()')
+ assert_equal "$(\"#my_widget\").find(\".item\").empty();", @helper.jq_action('my_widget', '.item', 'empty()')
end
end
context 'args: selector, action' do
should "generate selector only" do
- assert_equal "$('.item').empty();", @helper.jq_action('.item', 'empty()')
+ assert_equal "$(\".item\").empty();", @helper.jq_action('.item', 'empty()')
end
end
end
+
+ context '#inv_markup_action' do
+ context 'args: selector, markup, action' do
+ should "generate full action" do
+ assert_equal "$(\"<b>hello > world<\\/b>\").replaceAll(\"#my_widget\");", @helper.inv_markup_action('#my_widget', '<b>hello > world</b>', :replace_all)
+ end
+ end
+ end
+
+ context '#markup_action' do
+ context 'args: id, selector, markup, action' do
+ should "generate full action" do
+ assert_equal "$(\"#my_widget\").find(\".item\").append(\"<b>hello > world<\\/b>\");", @helper.markup_action('my_widget', '.item', '<b>hello > world</b>', :append)
+ end
+ end
+
+ context 'args: selector, markup, action' do
+ should "generate selector action" do
+ assert_equal "$(\".item\").append(\"<b>hello > world<\\/b>\");", @helper.markup_action('.item', '<b>hello > world</b>', :append)
+ end
+ end
+ end
end
end
@@ -64,6 +64,10 @@ class JavascriptGeneratorTest < Test::Unit::TestCase
setup do
@gen = Apotomo::JavascriptGenerator.new(:jquery)
end
+
+ should 'create _apo_xyz javascript variable for jQuery element' do
+ assert_equal "var _apo_xyz = $(\"#my_id\").find(\".item\");", @gen.selector_for(:xyz, 'my_id', '.item')
+ end
should "respond to jquery" do
assert_respond_to @gen, :jquery
@@ -72,14 +76,32 @@ class JavascriptGeneratorTest < Test::Unit::TestCase
should "respond to replace" do
assert_equal "$(\"#drinks\").replaceWith(\"EMPTY!\");", @gen.replace("#drinks", 'EMPTY!')
end
+
+ should "escape" do
+ assert_equal "<li id=\\\"beer\\\"><\\/li>", @gen.escape('<li id="beer"></li>')
+ end
+
+ should "respond to replace and escape" do
+ assert_equal "$(\"#drinks\").replaceWith(\"<li id=\\\"beer\\\"><\\/li>\");", @gen.replace("#drinks", '<li id="beer"></li>')
+ end
+
+ should "respond to replace with block" do
+ txt = @gen.replace("#drinks") { '<li id="beer"></li>'}
+ assert_equal "$(\"#drinks\").replaceWith(\"<li id=\\\"beer\\\"><\\/li>\");", txt
+ end
should "respond to replace_id" do
assert_equal "$(\"#drinks\").replaceWith(\"EMPTY!\");", @gen.replace_id("drinks", 'EMPTY!')
end
- should "respond to update" do
+ should "respond to update and escape" do
assert_equal "$(\"#drinks\").html(\"<li id=\\\"beer\\\"><\\/li>\");", @gen.update("#drinks", '<li id="beer"></li>')
end
+
+ should "respond to update with block" do
+ txt = @gen.update("#drinks") { '<li id="beer"></li>'}
+ assert_equal "$(\"#drinks\").html(\"<li id=\\\"beer\\\"><\\/li>\");", txt
+ end
should "respond to update_id" do
assert_equal "$(\"#drinks\").html(\"EMPTY!\");", @gen.update_id("drinks", 'EMPTY!')

0 comments on commit 22263c5

Please sign in to comment.