|
| 1 | +require 'gon' |
| 2 | + |
| 3 | +describe Gon::Watch do |
| 4 | + |
| 5 | + let(:controller) { ActionController::Base.new } |
| 6 | + let(:request) { ActionDispatch::Request.new({}) } |
| 7 | + |
| 8 | + before :each do |
| 9 | + controller.request = request |
| 10 | + controller.params = {} |
| 11 | + env = {} |
| 12 | + env['ORIGINAL_FULLPATH'] = '/foo' |
| 13 | + env['REQUEST_METHOD'] = 'GET' |
| 14 | + |
| 15 | + Gon::Watch.clear |
| 16 | + Gon::Request.instance_variable_set(:@request_env, env) |
| 17 | + Gon::Request.env['action_controller.instance'] = controller |
| 18 | + Gon.clear |
| 19 | + end |
| 20 | + |
| 21 | + it 'should add variables to Gon#all_variables hash' do |
| 22 | + Gon.a = 1 |
| 23 | + Gon.watch.b = 2 |
| 24 | + Gon.all_variables.should == { 'a' => 1, 'b' => 2 } |
| 25 | + end |
| 26 | + |
| 27 | + describe '#all_variables' do |
| 28 | + |
| 29 | + it 'should generate array with current request url, method type and variable names' do |
| 30 | + Gon.watch.a = 1 |
| 31 | + Gon.watch.all_variables.should == { 'a' => { 'url' => '/foo', 'method' => 'GET', 'name' => 'a' } } |
| 32 | + end |
| 33 | + |
| 34 | + end |
| 35 | + |
| 36 | + describe '#render' do |
| 37 | + |
| 38 | + it 'should render function with variables in gon namespace' do |
| 39 | + Gon.watch.a = 1 |
| 40 | + Gon.watch.render.should =~ /gon\.watch\s=/ |
| 41 | + Gon.watch.render.should =~ /gon\.watchedVariables/ |
| 42 | + end |
| 43 | + |
| 44 | + end |
| 45 | + |
| 46 | + it 'should return value of variable if called right request' do |
| 47 | + env = Gon::Request.instance_variable_get :@request_env |
| 48 | + env["HTTP_X_REQUESTED_WITH"] = "XMLHttpRequest" |
| 49 | + request = ActionDispatch::Request.new env |
| 50 | + controller.request = request |
| 51 | + params = {} |
| 52 | + params[:gon_return_variable] = true |
| 53 | + params[:gon_watched_variable] = 'a' |
| 54 | + controller.params = params |
| 55 | + Gon::Request.env['action_controller.instance'] = controller |
| 56 | + |
| 57 | + controller.should_receive('render').with(:text => '1') |
| 58 | + |
| 59 | + Gon.watch.a = 1 |
| 60 | + end |
| 61 | + |
| 62 | +end |
0 commit comments