spec/gon/basic_spec.rb in gon-6.0.1 vs spec/gon/basic_spec.rb in gon-6.1.0

- old
+ new

@@ -1,7 +1,5 @@ -require 'spec_helper' - describe Gon do before(:each) do Gon.clear end @@ -95,37 +93,30 @@ @base.request = request end it 'outputs correct js with an integer' do Gon.int = 1 - expect(@base.include_gon).to eq('<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - 'gon.int=1;' + - "\n//]]>\n" + - '</script>') + expect(@base.include_gon).to eq(wrap_script( + 'window.gon={};' + + 'gon.int=1;')) end it 'outputs correct js with a string' do Gon.str = %q(a'b"c) - expect(@base.include_gon).to eq('<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - %q(gon.str="a'b\"c";) + - "\n//]]>\n" + - '</script>') + expect(@base.include_gon).to eq(wrap_script( + 'window.gon={};' + + %q(gon.str="a'b\"c";)) + ) end it 'outputs correct js with a script string' do Gon.str = %q(</script><script>alert('!')</script>) escaped_str = "\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e" - expect(@base.include_gon).to eq('<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - %Q(gon.str="#{escaped_str}";) + - "\n//]]>\n" + - '</script>') + expect(@base.include_gon).to eq(wrap_script( + 'window.gon={};' + + %Q(gon.str="#{escaped_str}";)) + ) end it 'outputs correct js with an integer and type' do Gon.int = 1 expect(@base.include_gon(type: true)).to eq('<script type="text/javascript">' + @@ -136,53 +127,37 @@ '</script>') end it 'outputs correct js with an integer, camel-case and namespace' do Gon.int_cased = 1 - expect(@base.include_gon(camel_case: true, namespace: 'camel_cased')).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.camel_cased={};' + - 'camel_cased.intCased=1;' + - "\n//]]>\n" + - '</script>' + expect(@base.include_gon(camel_case: true, namespace: 'camel_cased')).to eq( + wrap_script('window.camel_cased={};' + + 'camel_cased.intCased=1;') ) end it 'outputs correct js with camel_depth = :recursive' do Gon.test_hash = { test_depth_one: { test_depth_two: 1 } } - expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - 'gon.testHash={"testDepthOne":{"testDepthTwo":1}};' + - "\n//]]>\n" + - '</script>' + expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq( + wrap_script('window.gon={};' + + 'gon.testHash={"testDepthOne":{"testDepthTwo":1}};') ) end it 'outputs correct js with camel_depth = 2' do Gon.test_hash = { test_depth_one: { test_depth_two: 1 } } - expect(@base.include_gon(camel_case: true, camel_depth: 2)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - 'gon.testHash={"testDepthOne":{"test_depth_two":1}};' + - "\n//]]>\n" + - '</script>' + expect(@base.include_gon(camel_case: true, camel_depth: 2)).to eq( + wrap_script('window.gon={};' + + 'gon.testHash={"testDepthOne":{"test_depth_two":1}};') ) end it 'outputs correct js for an array with camel_depth = :recursive' do Gon.test_hash = { test_depth_one: [{ test_depth_two: 1 }, { test_depth_two: 2 }] } expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};' + - 'gon.testHash={"testDepthOne":[{"testDepthTwo":1},{"testDepthTwo":2}]};' + - "\n//]]>\n" + - '</script>' + wrap_script('window.gon={};' + + 'gon.testHash={"testDepthOne":[{"testDepthTwo":1},{"testDepthTwo":2}]};') ) end it 'outputs correct js with an integer and without tag' do Gon.int = 1 @@ -215,48 +190,29 @@ ) end it 'outputs correct js without cdata, without type, gon init and an integer' do Gon.int = 1 - expect(@base.include_gon(cdata: false, type: false)).to eq( \ - '<script>' + + expect(@base.include_gon(cdata: false, type: false)).to eq( + wrap_script( "\n" + 'window.gon={};' + 'gon.int=1;' + - "\n" + - '</script>' + "\n", false) ) end it 'outputs correct js with type text/javascript' do - expect(@base.include_gon(need_type: true, init: true)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};'\ - "\n//]]>\n" + - '</script>' - ) + expect(@base.include_gon(need_type: true, init: true)).to eq(wrap_script('window.gon={};')) end it 'outputs correct js with namespace check' do - expect(@base.include_gon(namespace_check: true)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon=window.gon||{};'\ - "\n//]]>\n" + - '</script>' - ) + expect(@base.include_gon(namespace_check: true)).to eq(wrap_script('window.gon=window.gon||{};')) end it 'outputs correct js without namespace check' do - expect(@base.include_gon(namespace_check: false)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};'\ - "\n//]]>\n" + - '</script>' - ) + expect(@base.include_gon(namespace_check: false)).to eq(wrap_script('window.gon={};')) end context "without a current_gon instance" do before(:each) do @@ -271,22 +227,15 @@ it 'outputs correct js' do expect(@base.include_gon).to eq("") end it 'outputs correct js with init' do - expect(@base.include_gon(init: true)).to eq( \ - '<script>' + - "\n//<![CDATA[\n" + - 'window.gon={};'\ - "\n//]]>\n" + - '</script>' - ) + expect(@base.include_gon(init: true)).to eq(wrap_script('window.gon={};')) end end - end describe '#include_gon_amd' do before(:each) do @@ -334,19 +283,11 @@ describe '#check_for_rabl_and_jbuilder' do let(:controller) { ActionController::Base.new } - it 'should be able to handle ruby 1.8.7 style constants array (strings)' do - constants_as_strings = Gon.constants.map(&:to_s) - allow(Gon).to receive(:constants) { constants_as_strings } - expect { Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller }.not_to raise_error - expect { Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller }.not_to raise_error - end - - it 'should be able to handle ruby 1.9+ style constants array (symbols)' do - constants_as_symbols = Gon.constants.map(&:to_sym) - allow(Gon).to receive(:constants) { constants_as_symbols } + it 'should be able to handle constants array (symbols)' do + allow(Gon).to receive(:constants) { Gon.constants } expect { Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller }.not_to raise_error expect { Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller }.not_to raise_error end end