1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
require "abstract_unit"
class AjaxTestCase < ActiveSupport::TestCase
include ActionView::Helpers::TagHelper
def url_for(url)
case url
when Hash
"/url/hash"
when String
url
else
raise TypeError.new("Unsupported url type (#{url.class}) for this test helper")
end
end
def assert_html(html, matches)
matches.each do |match|
assert_match(Regexp.new(Regexp.escape(match)), html)
end
end
def self.assert_callbacks_work(&blk)
define_method(:assert_callbacks_work, &blk)
[:complete, :failure, :success, :interactive, :loaded, :loading, 404].each do |callback|
test "#{callback} callback" do
markup = assert_callbacks_work(callback)
assert_html markup, %W(data-#{callback}-code="undoRequestCompleted\(request\)")
end
end
end
end
class LinkToRemoteTest < AjaxTestCase
include ActionView::Helpers::AjaxHelperCompat
def link(options = {})
link_to_remote("Delete this post", "/blog/destroy/3", options)
end
test "basic" do
assert_html link(:update => "#posts"),
%w(data-update-success="#posts")
end
test "using a url string" do
assert_html link_to_remote("Test", "/blog/update/1"),
%w(href="/blog/update/1")
end
test "using a url hash" do
link = link_to_remote("Delete this post", {:controller => :blog}, :update => "#posts")
assert_html link, %w(href="/url/hash" data-update-success="#posts")
end
test "with no update" do
assert_html link, %w(href="/blog/destroy/3" Delete\ this\ post data-remote="true")
end
test "with :html options" do
expected = %{<a data-custom="me" data-remote="true" data-update-success="#posts" href="/blog/destroy/3">Delete this post</a>}
assert_equal expected, link(:update => "#posts", :html => {"data-custom" => "me"})
end
test "with a hash for :update" do
link = link(:update => {:success => "#posts", :failure => "#error"})
assert_match(/data-update-success="#posts"/, link)
assert_match(/data-update-failure="#error"/, link)
end
test "with positional parameters" do
link = link(:position => :top, :update => "#posts")
assert_match(/data\-update\-position="top"/, link)
end
test "with an optional method" do
link = link(:method => "delete")
assert_match(/data-method="delete"/, link)
end
class LegacyLinkToRemoteTest < AjaxTestCase
include ActionView::Helpers::AjaxHelperCompat
def link(options)
link_to_remote("Delete this post", "/blog/destroy/3", options)
end
test "basic link_to_remote with :url =>" do
expected = %{<a data-remote="true" data-update-success="#posts" href="/blog/destroy/4">Delete this post</a>}
assert_equal expected,
link_to_remote("Delete this post", :url => "/blog/destroy/4", :update => "#posts")
end
assert_callbacks_work do |callback|
link(callback => "undoRequestCompleted(request)")
end
end
end
class ButtonToRemoteTest < AjaxTestCase
include ActionView::Helpers::AjaxHelperCompat
def button(options, html = {})
button_to_remote("Remote outpost", options, html)
end
class StandardTest < ButtonToRemoteTest
test "basic" do
expected = %{<input class="fine" data-remote="true" data-url="/url/hash" type="button" value="Remote outpost" />}
assert_equal expected, button({:url => "/url/hash"}, {:class => "fine"})
end
end
class LegacyButtonToRemoteTest < ButtonToRemoteTest
include ActionView::Helpers::AjaxHelperCompat
assert_callbacks_work do |callback|
button(callback => "undoRequestCompleted(request)")
end
end
end
class ObserveFieldTest < AjaxTestCase
include ActionView::Helpers::AjaxHelperCompat
def url_for(hash)
"/blog/update"
end
def protect_against_forgery?
false
end
def field(options = {})
observe_field("title", options)
end
test "basic" do
assert_html field,
%w(data-observe="true")
end
test "with a :frequency option" do
assert_html field(:frequency => 5.minutes),
%w(data-observe="true" data-frequency="300")
end
test "using a url string" do
assert_html field(:url => "/some/other/url"),
%w(data-observe="true" data-url="/some/other/url")
end
test "using a url hash" do
assert_html field(:url => {:controller => :blog, :action => :update}),
%w(data-observe="true" data-url="/blog/update")
end
# def test_observe_field
# assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
# observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
# end
#
# def test_observe_field_using_with_option
# expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(value)})})\n//]]>\n</script>)
# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id')
# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)")
# end
#
# def test_observe_field_using_json_in_with_option
# expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:{'id':value}})})\n//]]>\n</script>)
# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
# end
#
# def test_observe_field_using_function_for_callback
# assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
# observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")
# end
end
|