require 'html/spec_helper'
describe "Flash" do
with_environment :development
class MockFlashContext < Crystal::MockTemplateContext
include Crystal::FlashHelper, Crystal::ControllerUrlHelper, Crystal::ViewUrlHelper
def url_for *args;
end
end
class FlashTestRenderCaller < Crystal::Processor
def call
context = MockFlashContext.new
block = workspace.check_flash.request
block.call context if block
workspace.request_done = true
next_processor.call
end
end
class FlashTestHelper < Crystal::Processor
def call
block = workspace.check_flash.before_request
block.call workspace if block
workspace.before_request_done = true
next_processor.call
block = workspace.check_flash.after_request
block.call workspace if block
workspace.after_request_done = true
end
end
before :all do
crystal.after :environment do
crystal.conveyors.web do |web|
web.use Crystal::Processors::PrepareParams
web.use FlashTestHelper
web.use Crystal::Processors::PrepareFlash
web.use FlashTestRenderCaller
end
end
end
def check_flash opt
workspace = nil
result = Crystal::HTTPAdapter.mock_call({}, :check_flash => opt.to_openobject) do |&block|
block.call
workspace = crystal[:workspace]
end
workspace.before_request_done.should be_true
workspace.request_done.should be_true
workspace.after_request_done.should be_true
workspace
end
it "flash should be extracted from session if there's any" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'html'
workspace.request.session['flash'] = {'info' => 'Ok'}.to_json
},
:request => lambda{|context|
context.flash.info.should == "Ok"
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should be_nil
}
)
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'html'
},
:request => lambda{|context|
context.flash.info.should be_nil
}
)
end
it "flash should be saved for next request in session" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'html'
},
:request => lambda{|context|
context.flash.info = "Ok"
context.flash.info.should be_nil
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should == {'info' => 'Ok'}.to_json
}
)
end
it "flash with redirect should be saved for next request in session" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'html'
},
:request => lambda{|context|
context.flash.info = "Ok"
context.redirect_to '/'
context.flash.info.should be_nil
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should == {'info' => 'Ok'}.to_json
}
)
end
it "AJAX (:js format) flash should be displayed in the same request" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'js'
},
:request => lambda{|context|
context.flash.info = "Ok"
context.flash.info.should == "Ok"
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should be_nil
}
)
end
it "flash with AJAX redirect (:js format) should be saved for next request in session" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'js'
},
:request => lambda{|context|
context.flash.info = "Ok"
context.flash.info.should == "Ok"
context.redirect_to '/'
context.flash.info.should be_nil
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should == {'info' => 'Ok'}.to_json
}
)
end
it "multiple messages with non-AJAX request" do
check_flash(
:before_request => lambda{|workspace|
workspace.request.session['flash'] = {'info' => 'Ok'}.to_json
},
:request => lambda{|context|
context.flash.error = "Error"
context.flash.error.should be_nil
context.flash.info.should == 'Ok'
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should == {'error' => 'Error'}.to_json
}
)
end
it "multiple messages with AJAX request" do
check_flash(
:before_request => lambda{|workspace|
workspace.params.format = 'js'
workspace.request.session['flash'] = {'info' => 'Ok'}.to_json
},
:request => lambda{|context|
context.flash.error = "Error"
context.flash.error.should == "Error"
context.flash.info.should == 'Ok'
},
:after_request => lambda{|workspace|
workspace.request.session['flash'].should be_nil
}
)
end
end