jelly 0.6.5 → 0.8.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2
+
3
+ describe Jelly::Common do
4
+ attr_reader :fixture
5
+ before do
6
+ @fixture = Class.new do
7
+ include Jelly::Common
8
+ end.new
9
+ end
10
+
11
+ describe "#jelly_callback_hash" do
12
+ it "creates a hash with a method and arguments" do
13
+ fixture.jelly_callback_hash("my_method", 1, 2, 3).should == {
14
+ "method" => "my_method",
15
+ "arguments" => [1, 2, 3]
16
+ }
17
+ end
18
+ end
19
+
20
+ describe "#jelly_callback_attach_hash" do
21
+ context "when passed attachments" do
22
+ it "creates a hash with the attach param being set to the given attachments" do
23
+ attachments = [
24
+ fixture.jelly_attachment_hash("Foo", 1, 2),
25
+ fixture.jelly_attachment_hash("Bar", 3),
26
+ ]
27
+ fixture.jelly_callback_attach_hash(attachments).should == {
28
+ "attach" => attachments
29
+ }
30
+ end
31
+ end
32
+
33
+ context "when not passed attachments" do
34
+ it "creates a hash with the attach param being set to #jelly_attachments" do
35
+ attachments = [
36
+ fixture.jelly_attachment_hash("Foo", 1, 2),
37
+ fixture.jelly_attachment_hash("Bar", 3),
38
+ ]
39
+ stub(fixture).jelly_attachments {attachments}
40
+ fixture.jelly_callback_attach_hash.should == {
41
+ "attach" => attachments
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,327 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2
+
3
+ describe ApplicationController, :type => :controller do
4
+
5
+ describe "#jelly_callback" do
6
+ attr_reader :template
7
+ before do
8
+ stub(@controller).render do |params|
9
+ @template = ActionView::Base.new(@controller.class.view_paths, {}, @controller)
10
+ template.send(:_evaluate_assigns_and_ivars)
11
+ response.body = ERB.new(params[:inline]).result(template.send(:binding))
12
+ end
13
+ end
14
+
15
+ it "have the method included" do
16
+ @controller.respond_to?(:jelly_callback).should be_true
17
+ end
18
+
19
+ describe "Arguments block" do
20
+ describe "self" do
21
+ it "runs with the binding of the ERB template" do
22
+ self_in_block = nil
23
+ @controller.send(:jelly_callback, 'foo', :format => :json) do
24
+ self_in_block = self
25
+ 12345
26
+ end
27
+ self_in_block.should == template
28
+ end
29
+ end
30
+
31
+ context "when an Array is returned from the block" do
32
+ it "sets the arguments to be an Array around the Hash" do
33
+ @controller.send(:jelly_callback, 'foo', :format => :json) do
34
+ ["foo", "bar"]
35
+ end
36
+ callback = JSON.parse(response.body)
37
+ callback["method"].should == "on_foo"
38
+ callback["arguments"].should == ["foo", "bar"]
39
+ end
40
+ end
41
+
42
+ context "when a non-array is returned in the block" do
43
+ context "when the argument is a Hash" do
44
+ it "sets the arguments to be an Array around the Hash" do
45
+ @controller.send(:jelly_callback, 'foo', :format => :json) do
46
+ {"foo" => "bar"}
47
+ end
48
+ callback = JSON.parse(response.body)
49
+ callback["method"].should == "on_foo"
50
+ callback["arguments"].should == [{"foo" => "bar"}]
51
+ end
52
+ end
53
+
54
+ context "when the argument is a String" do
55
+ it "sets the arguments to be an Array around the argument" do
56
+ @controller.send(:jelly_callback, 'foo', :format => :json) do
57
+ "foobar"
58
+ end
59
+ callback = JSON.parse(response.body)
60
+ callback["method"].should == "on_foo"
61
+ callback["arguments"].should == ["foobar"]
62
+ end
63
+ end
64
+
65
+ context "when the argument is a Number" do
66
+ it "sets the arguments to be an Array around the argument" do
67
+ @controller.send(:jelly_callback, 'foo', :format => :json) do
68
+ 12345
69
+ end
70
+ callback = JSON.parse(response.body)
71
+ callback["method"].should == "on_foo"
72
+ callback["arguments"].should == [12345]
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ context "when given a format" do
79
+ describe "json" do
80
+ it "responds with a json hash, even if the request is not xhr" do
81
+ stub(request).xhr? {false}
82
+
83
+ @controller.send(:jelly_callback, 'foo', {'format' => :json, 'bar' => 'baz'}) do
84
+ "grape"
85
+ end
86
+ callback = JSON.parse(response.body)
87
+ callback["method"].should == "on_foo"
88
+ callback["arguments"].should == ["grape"]
89
+ callback["bar"].should == "baz"
90
+ end
91
+ end
92
+
93
+ describe "jsonp" do
94
+ it "responds with a jsonp callback based on the callback param" do
95
+ @controller.params[:callback] = "Jelly.notifyObservers"
96
+
97
+ @controller.send(:jelly_callback, 'foo', {'format' => :jsonp, 'bar' => 'baz'}) do
98
+ "grape"
99
+ end
100
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
101
+ callback = JSON.parse(json)
102
+ callback["method"].should == "on_foo"
103
+ callback["arguments"].should == ["grape"]
104
+ callback["bar"].should == "baz"
105
+ end
106
+ end
107
+
108
+ describe "iframe" do
109
+ it "responds with a the json in a textarea tag" do
110
+ @controller.send(:jelly_callback, 'foo', {'format' => :iframe, 'bar' => 'baz'}) do
111
+ "grape"
112
+ end
113
+ body = response.body
114
+ body.should =~ /^<textarea>/
115
+ body.should =~ /<\/textarea>$/
116
+ doc = Nokogiri::HTML(body)
117
+
118
+ callback = JSON.parse(doc.at("textarea").inner_html)
119
+ callback["method"].should == "on_foo"
120
+ callback["arguments"].should == ["grape"]
121
+ callback["bar"].should == "baz"
122
+ end
123
+ end
124
+ end
125
+
126
+ context "when there is a callback param" do
127
+ before do
128
+ @controller.params[:callback] = "Jelly.notifyObservers"
129
+ end
130
+
131
+ context "when the request is XHR" do
132
+ before do
133
+ stub(request).xhr? {true}
134
+ end
135
+
136
+ it "responds with a call to the given callback method with the json as an argument" do
137
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
138
+ "grape"
139
+ end
140
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
141
+ callback = JSON.parse(json)
142
+ callback["method"].should == "on_foo"
143
+ callback["arguments"].should == ["grape"]
144
+ callback["bar"].should == "baz"
145
+ end
146
+ end
147
+
148
+ context "when the request is not XHR" do
149
+ before do
150
+ stub(request).xhr? {false}
151
+ end
152
+
153
+ it "responds with a call to the given callback method with the json as an argument" do
154
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
155
+ "grape"
156
+ end
157
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
158
+ callback = JSON.parse(json)
159
+ callback["method"].should == "on_foo"
160
+ callback["arguments"].should == ["grape"]
161
+ callback["bar"].should == "baz"
162
+ end
163
+ end
164
+ end
165
+
166
+ context "when there is not a callback param" do
167
+ context "when the request is XHR" do
168
+ before do
169
+ stub(request).xhr? {true}
170
+ end
171
+
172
+ it "responds with a json hash" do
173
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
174
+ "grape"
175
+ end
176
+ callback = JSON.parse(response.body)
177
+ callback["method"].should == "on_foo"
178
+ callback["arguments"].should == ["grape"]
179
+ callback["bar"].should == "baz"
180
+ end
181
+
182
+ end
183
+
184
+ context "when the request is not XHR" do
185
+ before do
186
+ stub(request).xhr? {false}
187
+ end
188
+
189
+ context "when there is not a callback param" do
190
+ it "wraps the json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
191
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
192
+ "grape"
193
+ end
194
+ body = response.body
195
+ body.should =~ /^<textarea>/
196
+ body.should =~ /<\/textarea>$/
197
+ doc = Nokogiri::HTML(body)
198
+
199
+ callback = JSON.parse(doc.at("textarea").inner_html)
200
+ callback["method"].should == "on_foo"
201
+ callback["arguments"].should == ["grape"]
202
+ callback["bar"].should == "baz"
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ describe "#raw_jelly_callback" do
210
+ attr_reader :response
211
+ before do
212
+ @response = Struct.new(:body).new
213
+ stub(@controller).render do |params|
214
+ response.body = ERB.new(params[:inline]).result(@controller.send(:binding))
215
+ end
216
+ end
217
+
218
+ it "have the method included" do
219
+ @controller.respond_to?(:raw_jelly_callback).should be_true
220
+ end
221
+
222
+ context "when given a format" do
223
+ describe "json" do
224
+ it "responds with a json hash, even if the request is not xhr" do
225
+ stub(request).xhr? {false}
226
+
227
+ @controller.send(:raw_jelly_callback, :format => :json) do
228
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
229
+ end
230
+ callback = JSON.parse(response.body)
231
+ callback["method"].should == "foo"
232
+ callback["arguments"].should == ["grape"]
233
+ callback["bar"].should == "baz"
234
+ end
235
+ end
236
+
237
+ describe "jsonp" do
238
+ it "responds with a jsonp callback based on the callback param" do
239
+ @controller.params[:callback] = "Jelly.notifyObservers"
240
+
241
+ @controller.send(:raw_jelly_callback, :format => :jsonp) do
242
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
243
+ end
244
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
245
+ callback = JSON.parse(json)
246
+ callback["method"].should == "foo"
247
+ callback["arguments"].should == ["grape"]
248
+ callback["bar"].should == "baz"
249
+ end
250
+ end
251
+
252
+ describe "iframe" do
253
+ it "responds with a the json in a textarea tag" do
254
+ @controller.send(:raw_jelly_callback, :format => :iframe) do
255
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
256
+ end
257
+ body = response.body
258
+ body.should =~ /^<textarea>/
259
+ body.should =~ /<\/textarea>$/
260
+ doc = Nokogiri::HTML(body)
261
+
262
+ callback = JSON.parse(doc.at("textarea").inner_html)
263
+ callback["method"].should == "foo"
264
+ callback["arguments"].should == ["grape"]
265
+ callback["bar"].should == "baz"
266
+ end
267
+ end
268
+ end
269
+
270
+ context "when the request is XHR" do
271
+ before do
272
+ stub(request).xhr? {true}
273
+ end
274
+
275
+ it "responds with a json hash" do
276
+ @controller.send(:raw_jelly_callback) do
277
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
278
+ end
279
+ callback = JSON.parse(response.body)
280
+ callback["method"].should == "foo"
281
+ callback["arguments"].should == ["grape"]
282
+ callback["bar"].should == "baz"
283
+ end
284
+
285
+ end
286
+
287
+ context "when the request is not XHR" do
288
+ before do
289
+ stub(request).xhr? {false}
290
+ end
291
+
292
+ context "when there is a callback param" do
293
+ before do
294
+ @controller.params[:callback] = "Jelly.notifyObservers"
295
+ end
296
+
297
+ it "responds with a call to the given callback method with the json as an argument" do
298
+ @controller.send(:raw_jelly_callback) do
299
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
300
+ end
301
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
302
+ callback = JSON.parse(json)
303
+ callback["method"].should == "foo"
304
+ callback["arguments"].should == ["grape"]
305
+ callback["bar"].should == "baz"
306
+ end
307
+ end
308
+
309
+ context "when there is not a callback param" do
310
+ it "wraps the json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
311
+ @controller.send(:raw_jelly_callback) do
312
+ jelly_callback_hash("foo", "grape").merge('bar' => 'baz')
313
+ end
314
+ body = response.body
315
+ body.should =~ /^<textarea>/
316
+ body.should =~ /<\/textarea>$/
317
+ doc = Nokogiri::HTML(body)
318
+
319
+ callback = JSON.parse(doc.at("textarea").inner_html)
320
+ callback["method"].should == "foo"
321
+ callback["arguments"].should == ["grape"]
322
+ callback["bar"].should == "baz"
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
@@ -1,19 +1,28 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2
2
 
3
- describe JellyHelper do
3
+ describe JellyHelper, :type => :helper do
4
+
5
+ def jelly_attach_arguments(html)
6
+ JSON.parse(Regexp.new('Jelly\.attach\.apply\(Jelly, (.*)\);').match(html)[1])
7
+ end
4
8
 
5
9
  describe "#spread_jelly" do
6
10
  before do
7
- stub_controller = mock(Object, :controller_path => 'my_fun_controller', :action_name => 'super_good_action')
8
- helper.should_receive(:controller).any_number_of_times.and_return(stub_controller)
9
- helper.should_receive(:form_authenticity_token).and_return('areallysecuretoken')
11
+ stub_controller = mock! do |controller|
12
+ controller.controller_path {'my_fun_controller'}
13
+ controller.action_name {'super_good_action'}
14
+ end
15
+ stub(helper).controller {stub_controller}
16
+ mock(helper).form_authenticity_token {'areallysecuretoken'}
10
17
  end
11
18
 
12
19
  it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do
13
20
  output = helper.spread_jelly
14
21
  output.should include('<script type="text/javascript">')
15
- output.should include("Jelly.attach(Jelly.Location, #{[].to_json});")
16
- output.should include("Jelly.attach(Jelly.Page, #{['MyFunController', 'super_good_action'].to_json});")
22
+ doc = Nokogiri::HTML(output)
23
+ argument = jelly_attach_arguments(doc.css("script")[1].inner_html)
24
+ argument.should include({'component' => "Jelly.Location", 'arguments' => []})
25
+ argument.should include({'component' => "Jelly.Page", 'arguments' => ['MyFunController', 'super_good_action']})
17
26
  end
18
27
  end
19
28
 
@@ -38,10 +47,13 @@ describe JellyHelper do
38
47
  end
39
48
 
40
49
  describe "#attach_javascript_component" do
50
+ before do
51
+ def helper.form_authenticity_token
52
+ "12345"
53
+ end
54
+ end
41
55
 
42
56
  after do
43
- #need to clear this since it's saving state between tests
44
- assigns[:content_for_javascript] = ""
45
57
  helper.clear_jelly_attached()
46
58
  end
47
59
 
@@ -49,19 +61,26 @@ describe JellyHelper do
49
61
  helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
50
62
  helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
51
63
  helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg5')
52
- assigns[:content_for_javascript].should == 'Jelly.attach(MyComponent, ["arg1","arg2","arg3"]);Jelly.attach(MyComponent, ["arg1","arg2","arg5"]);'
64
+ assigns[:jelly_attachments].should == [
65
+ {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']},
66
+ {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg5']},
67
+ ]
53
68
  end
54
69
 
55
- it "adds a call to Jelly.attach in the javascript content" do
70
+ it "adds a call to Jelly.attach in an $(document).ready block" do
56
71
  helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
57
72
  expected_args = ['arg1','arg2','arg3'].to_json
58
- assigns[:content_for_javascript].should == "Jelly.attach(MyComponent, #{expected_args});"
59
- end
73
+ assigns[:jelly_attachments].should == [
74
+ {'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']}
75
+ ]
60
76
 
61
- it "adds a call to Jelly.attach in the javascript_on_ready content" do
62
- helper.attach_javascript_component_on_ready("MyComponent", 'arg1', 'arg2', 'arg3')
63
- expected_args = ['arg1','arg2','arg3'].to_json
64
- assigns[:content_for_javascript_on_ready].should == "Jelly.attach(MyComponent, #{expected_args});"
77
+ html = helper.spread_jelly
78
+ doc = Nokogiri::HTML(html)
79
+ document_ready_tag = doc.css("script")[1]
80
+ document_ready_tag.inner_html.should include("$(document).ready(function() {")
81
+ document_ready_part = document_ready_tag.inner_html.split("\n")[3]
82
+ arguments = jelly_attach_arguments(document_ready_part)
83
+ arguments.should include({'component' => "MyComponent", 'arguments' => ['arg1', 'arg2', 'arg3']})
65
84
  end
66
85
 
67
86
  end