|
| 1 | +require 'gon' |
| 2 | + |
| 3 | +describe Gon::Global do |
| 4 | + |
| 5 | + before(:each) do |
| 6 | + Gon::Global.clear |
| 7 | + end |
| 8 | + |
| 9 | + describe '#all_variables' do |
| 10 | + |
| 11 | + it 'returns all variables in hash' do |
| 12 | + Gon.global.a = 1 |
| 13 | + Gon.global.b = 2 |
| 14 | + Gon.global.c = Gon.global.a + Gon.global.b |
| 15 | + Gon.global.c.should == 3 |
| 16 | + Gon.global.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3} |
| 17 | + end |
| 18 | + |
| 19 | + it 'supports all data types' do |
| 20 | + Gon.global.clear |
| 21 | + Gon.global.int = 1 |
| 22 | + Gon.global.float = 1.1 |
| 23 | + Gon.global.string = 'string' |
| 24 | + Gon.global.array = [ 1, 'string' ] |
| 25 | + Gon.global.hash_var = { :a => 1, :b => '2'} |
| 26 | + Gon.global.hash_w_array = { :a => [ 2, 3 ] } |
| 27 | + Gon.global.klass = Hash |
| 28 | + end |
| 29 | + |
| 30 | + end |
| 31 | + |
| 32 | + describe '#include_gon' do |
| 33 | + |
| 34 | + before(:each) do |
| 35 | + Gon.clear |
| 36 | + Gon.global.clear |
| 37 | + ActionView::Base. |
| 38 | + instance_methods. |
| 39 | + map(&:to_s). |
| 40 | + include?('include_gon').should == true |
| 41 | + @base = ActionView::Base.new |
| 42 | + @base.request = request |
| 43 | + end |
| 44 | + |
| 45 | + it 'outputs correct js with an integer' do |
| 46 | + Gon.global.int = 1 |
| 47 | + @base.include_gon.should == "<script>window.gon = {};" + |
| 48 | + "gon.global={\"int\":1};" + |
| 49 | + "</script>" |
| 50 | + end |
| 51 | + |
| 52 | + it 'outputs correct js with an integer and integer in Gon' do |
| 53 | + Gon::Request. |
| 54 | + instance_variable_set(:@request_id, request.object_id) |
| 55 | + Gon.int = 1 |
| 56 | + Gon.global.int = 1 |
| 57 | + @base.include_gon.should == "<script>window.gon = {};" + |
| 58 | + "gon.int=1;" + |
| 59 | + "gon.global={\"int\":1};" + |
| 60 | + "</script>" |
| 61 | + end |
| 62 | + |
| 63 | + it 'outputs correct js with a string' do |
| 64 | + Gon.global.str = %q(a'b"c) |
| 65 | + @base.include_gon.should == "<script>window.gon = {};" + |
| 66 | + "gon.global={\"str\":\"a'b\\\"c\"};" + |
| 67 | + "</script>" |
| 68 | + end |
| 69 | + |
| 70 | + it 'outputs correct js with a script string' do |
| 71 | + Gon.global.str = %q(</script><script>alert('!')</script>) |
| 72 | + @base.include_gon.should == "<script>window.gon = {};" + |
| 73 | + "gon.global={\"str\":\"<\\/script><script>alert('!')<\\/script>\"};" + |
| 74 | + "</script>" |
| 75 | + end |
| 76 | + |
| 77 | + end |
| 78 | + |
| 79 | + it 'returns exception if try to set public method as variable' do |
| 80 | + Gon.global.clear |
| 81 | + lambda { Gon.global.all_variables = 123 }.should raise_error |
| 82 | + lambda { Gon.global.rabl = 123 }.should raise_error |
| 83 | + end |
| 84 | + |
| 85 | + it 'works fine with rabl and jbuilder' do |
| 86 | + pending |
| 87 | + end |
| 88 | + |
| 89 | + def request |
| 90 | + @request ||= double 'request', :env => {} |
| 91 | + end |
| 92 | + |
| 93 | +end |
0 commit comments