spec/jelly/jelly_helper_spec.rb in honkster-jelly-0.13.1 vs spec/jelly/jelly_helper_spec.rb in honkster-jelly-0.13.2

- old
+ new

@@ -1,27 +1,31 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') describe JellyHelper, :type => :helper do - def jelly_ops(html) + after do + helper.jelly_clear_ops() + end + + def parse_jelly_ops(html) JSON.parse(Regexp.new('Jelly\.run\.apply\(Jelly, (.*)\);').match(html)[1]) end describe "#spread_jelly" do before do stub_controller = mock! do |controller| - controller.controller_path {'my_fun_controller'} - controller.action_name {'super_good_action'} + controller.controller_path { 'my_fun_controller' } + controller.action_name { 'super_good_action' } end - stub(helper).controller {stub_controller} + stub(helper).controller { stub_controller } end it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do output = helper.spread_jelly output.should include('<script type="text/javascript">') doc = Nokogiri::HTML(output) - ops = jelly_ops(doc.at("script").inner_html) + ops = parse_jelly_ops(doc.at("script").inner_html) ops.should include(["attach", "Jelly.Location"]) ops.should include(["attach", "Jelly.Page", 'MyFunController', 'super_good_action']) end end @@ -44,42 +48,146 @@ end end end describe "#jelly_attach" do - before do - def helper.form_authenticity_token - "12345" + context "when not given a block" do + it "fails to add multiple calls to Jelly.attach for the same component" do + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') + helper.jelly_ops.should == [ + ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'], + ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'], + ] end - end - after do - helper.jelly_clear_ops() + it "adds an attach op to jelly_ops" do + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') + helper.jelly_ops.should( + include( + ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'] + ) + ) + + html = helper.spread_jelly + doc = Nokogiri::HTML(html) + document_ready_tag = doc.at("script") + document_ready_part = document_ready_tag.inner_html.split("\n")[2] + arguments = parse_jelly_ops(document_ready_part) + arguments.should include(["attach", "MyComponent", 'arg1', 'arg2', 'arg3']) + end end - it "fails to add multiple calls to Jelly.attach for the same component" do - helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') - helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') - helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') - helper.instance_variable_get(:@jelly_ops).should == [ - ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'], - ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'], - ] + context "when given a block" do + it "fails to add multiple calls to Jelly.attach for the same component" do + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_ops.should == [ + ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'], + ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'], + ] + end + + it "passes the attach op into the block so it can be added to jelly_ops" do + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') + helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_ops.should( + include( + ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'] + ) + ) + + html = helper.spread_jelly + doc = Nokogiri::HTML(html) + document_ready_tag = doc.at("script") + document_ready_part = document_ready_tag.inner_html.split("\n")[2] + arguments = parse_jelly_ops(document_ready_part) + helper.jelly_ops[0..1].should == [ + ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'], + ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'], + ] + end end - it "adds a call to Jelly.attach" do - helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') - helper.instance_variable_get(:@jelly_ops).should include( - ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'] - ) + end - html = helper.spread_jelly - doc = Nokogiri::HTML(html) - document_ready_tag = doc.at("script") - document_ready_part = document_ready_tag.inner_html.split("\n")[2] - arguments = jelly_ops(document_ready_part) - arguments.should include(["attach", "MyComponent", 'arg1', 'arg2', 'arg3']) + describe "#jelly_notify" do + context "when not given a block" do + it "fails to add multiple calls to Jelly.attach for the same component" do + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5') + helper.jelly_ops.should == [ + ["notify", "myMessage", 'arg1', 'arg2', 'arg3'], + ["notify", "myMessage", 'arg1', 'arg2', 'arg5'], + ] + end + + it "adds an attach op to jelly_ops" do + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') + helper.jelly_ops.should( + include( + ["notify", "myMessage", 'arg1', 'arg2', 'arg3'] + ) + ) + + html = helper.spread_jelly + doc = Nokogiri::HTML(html) + document_ready_tag = doc.at("script") + document_ready_part = document_ready_tag.inner_html.split("\n")[2] + arguments = parse_jelly_ops(document_ready_part) + arguments.should include(["notify", "myMessage", 'arg1', 'arg2', 'arg3']) + end end + context "when given a block" do + it "fails to add multiple calls to Jelly.attach for the same component" do + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_ops.should == [ + ["notify", "myMessage", 'arg1', 'arg2', 'arg5'], + ["notify", "myMessage", 'arg1', 'arg2', 'arg3'], + ] + end + + it "passes the attach op into the block so it can be added to jelly_ops" do + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') + helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5') do |op| + helper.jelly_ops.unshift op + end + helper.jelly_ops.should( + include( + ["notify", "myMessage", 'arg1', 'arg2', 'arg3'] + ) + ) + + html = helper.spread_jelly + doc = Nokogiri::HTML(html) + document_ready_tag = doc.at("script") + document_ready_part = document_ready_tag.inner_html.split("\n")[2] + arguments = parse_jelly_ops(document_ready_part) + helper.jelly_ops[0..1].should == [ + ["notify", "myMessage", 'arg1', 'arg2', 'arg5'], + ["notify", "myMessage", 'arg1', 'arg2', 'arg3'], + ] + end + end end end \ No newline at end of file