require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') describe ApplicationController do describe "#jelly_callback" do attr_reader :response before do @response = Struct.new(:body).new stub(@controller).render do |params| response.body = ERB.new(params[:inline]).result(@controller.send(:binding)) end end it "have the method included" do @controller.respond_to?(:jelly_callback).should be_true end context "when the request is XHR" do before do stub(request).xhr? {true} end it "responds with a json hash" do @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do "grape" end callback = JSON.parse(response.body) callback["method"].should == "on_foo" callback["arguments"].should == ["grape"] callback["bar"].should == "baz" end end context "when the request is not XHR" do before do stub(request).xhr? {false} end context "when there is a callback param" do before do @controller.params[:callback] = "Jelly.notifyObservers" end it "responds with a call to the given callback method with the json as an argument" do @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do "grape" end json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1] callback = JSON.parse(json) callback["method"].should == "on_foo" callback["arguments"].should == ["grape"] callback["bar"].should == "baz" end end context "when there is not a callback param" do 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 @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do "grape" end body = response.body body.should =~ /^