Skip to content
This repository
Browse code

Added assert_data_element_json test helper for data element helpers

  • Loading branch information...
commit 0955f5791555127d2ea3ab1c8a2450bca5a4236a 1 parent 6fa8f81
Bob Remeika foobarfighter authored stefanpenner committed
10 actionpack/lib/action_view/helpers/ajax_helper.rb
@@ -48,10 +48,12 @@ def observe_field(name, options = {})
48 48 end
49 49 end
50 50
51   - if options[:with] && (options[:with] !~ /[\{=(.]/)
52   - options[:with] = "'#{options[:with]}=' + encodeURIComponent(value)"
53   - else
54   - options[:with] ||= 'value' unless options[:function]
  51 + if options[:with]
  52 + if options[:with] !~ /[\{=(.]/
  53 + options[:with] = "'#{options[:with]}=' + encodeURIComponent(value)"
  54 + else
  55 + options[:with] ||= 'value' unless options[:function]
  56 + end
55 57 end
56 58
57 59 if options[:function]
44 actionpack/test/javascript/ajax_test.rb
@@ -10,6 +10,20 @@ def assert_html(html, matches)
10 10 end
11 11 end
12 12
  13 + def extract_json_from_data_element(data_element)
  14 + root = HTML::Document.new(data_element).root
  15 + script = root.find(:tag => "script")
  16 + cdata = script.children.detect {|child| child.to_s =~ /<!\[CDATA\[/ }
  17 + js = cdata.content.split("\n").map {|line| line.gsub(Regexp.new("//.*"), "")}.join("\n").strip!
  18 +
  19 + ActiveSupport::JSON.decode(js)
  20 + end
  21 +
  22 + def assert_data_element_json(actual, expected)
  23 + json = extract_json_from_data_element(actual)
  24 + assert_equal expected, json
  25 + end
  26 +
13 27 def self.assert_callbacks_work(&blk)
14 28 define_method(:assert_callbacks_work, &blk)
15 29
@@ -112,8 +126,6 @@ class LegacyButtonToRemoteTest < ButtonToRemoteTest
112 126 end
113 127 end
114 128
115   -# TODO: We need a better way to test JSON being returned from data only helpers - BR
116   -# TODO: We might also need a lower level data only helper method??? - BR
117 129 class ObserveFieldTest < AjaxTestCase
118 130 def protect_against_forgery?
119 131 false
@@ -129,18 +141,18 @@ def field(options = {})
129 141 end
130 142
131 143 test "using a url string" do
132   - assert_html field(:url => "/some/other/url"),
133   - %w("url":"/some/other/url")
  144 + assert_data_element_json field(:url => "/some/other/url"),
  145 + "url" => "/some/other/url", "name" => "title"
134 146 end
135 147
136 148 test "using a url hash" do
137   - assert_html field(:url => {:controller => :blog, :action => :update}),
138   - %w("url":"/url/hash")
  149 + assert_data_element_json field(:url => {:controller => :blog, :action => :update}),
  150 + "url" => "/url/hash", "name" => "title"
139 151 end
140 152
141 153 test "using a :frequency option" do
142   - assert_html field(:frequency => 5.minutes),
143   - %w("frequency":300)
  154 + assert_data_element_json field(:url => { :controller => :blog }, :frequency => 5.minutes),
  155 + "url" => "/url/hash", "name" => "title", "frequency" => 300
144 156 end
145 157
146 158 test "using a :frequency option of 0" do
@@ -155,19 +167,19 @@ def field(options = {})
155 167
156 168 # TODO: Consider using JSON instead of strings. Is using 'value' as a magical reference to the value of the observed field weird? (Rails2 does this) - BR
157 169 test "using a :with option" do
158   - assert_html field(:with => "foo"),
159   - %w("with":"'foo=' + encodeURIComponent(value)")
160   - assert_html field(:with => "'foo=' + encodeURIComponent(value)"),
161   - %w("with":"'foo=' + encodeURIComponent(value)")
  170 + assert_data_element_json field(:with => "foo"),
  171 + "name" => "title", "with" => "'foo=' + encodeURIComponent(value)"
  172 + assert_data_element_json field(:with => "'foo=' + encodeURIComponent(value)"),
  173 + "name" => "title", "with" => "'foo=' + encodeURIComponent(value)"
162 174 end
163 175
164 176 test "using json in a :with option" do
165   - assert_html field(:with => "{'id':value}"),
166   - %w("with":"{'id':value}")
  177 + assert_data_element_json field(:with => "{'id':value}"),
  178 + "name" => "title", "with" => "{'id':value}"
167 179 end
168 180
169 181 test "using :function for callback" do
170   - assert_html field(:function => "alert('Element changed')"),
171   - %w("function":"function(element, value) {alert('Element changed')}")
  182 + assert_data_element_json field(:function => "alert('Element changed')"),
  183 + "name" => "title", "function" => "function(element, value) {alert('Element changed')}"
172 184 end
173 185 end

0 comments on commit 0955f57

Please sign in to comment.
Something went wrong with that request. Please try again.