spec/gon/basic_spec.rb in gon-5.2.0 vs spec/gon/basic_spec.rb in gon-5.2.1

- old
+ new

@@ -1,10 +1,11 @@ require 'spec_helper' describe Gon do before(:each) do + Gon.clear end describe '#all_variables' do it 'returns all variables in hash' do @@ -14,60 +15,80 @@ expect(Gon.c).to eq(3) expect(Gon.all_variables).to eq({ 'a' => 1, 'b' => 2, 'c' => 3 }) end it 'supports all data types' do - Gon.clear Gon.int = 1 Gon.float = 1.1 Gon.string = 'string' Gon.array = [1, 'string'] Gon.hash_var = { :a => 1, :b => '2' } Gon.hash_w_array = { :a => [2, 3] } Gon.klass = Hash end it 'can be filled with dynamic named variables' do - Gon.clear - check = {} 3.times do |i| Gon.set_variable("variable#{i}", i) check["variable#{i}"] = i end expect(Gon.all_variables).to eq(check) end it 'can set and get variable with dynamic name' do - Gon.clear var_name = "variable#{rand}" Gon.set_variable(var_name, 1) expect(Gon.get_variable(var_name)).to eq(1) end it 'can be support new push syntax' do - Gon.clear - Gon.push({ :int => 1, :string => 'string' }) expect(Gon.all_variables).to eq({ 'int' => 1, 'string' => 'string' }) end it 'push with wrong object' do expect { - Gon.clear Gon.push(String.new('string object')) }.to raise_error('Object must have each_pair method') end + describe "#merge_variable" do + it 'deep merges the same key' do + Gon.merge_variable(:foo, { bar: { tar: 12 }, car: 23 }) + Gon.merge_variable(:foo, { bar: { dar: 21 }, car: 12 }) + expect(Gon.get_variable(:foo)).to eq(bar: { tar: 12, dar: 21 }, car: 12) + end + + it 'merges on push with a flag' do + Gon.push(foo: { bar: 1 }) + Gon.push({ foo: { tar: 1 } }, :merge) + expect(Gon.get_variable("foo")).to eq(bar: 1, tar: 1) + end + + context 'overrides key' do + specify "the previous value wasn't hash" do + Gon.merge_variable(:foo, 2) + Gon.merge_variable(:foo, { a: 1 }) + expect(Gon.get_variable(:foo)).to eq(a: 1) + end + + specify "the new value isn't a hash" do + Gon.merge_variable(:foo, { a: 1 }) + Gon.merge_variable(:foo, 2) + expect(Gon.get_variable(:foo)).to eq(2) + end + end + end + end describe '#include_gon' do before(:each) do - Gon.clear Gon::Request. instance_variable_set(:@request_id, request.object_id) expect(ActionView::Base. instance_methods. map(&:to_s). @@ -259,11 +280,10 @@ end describe '#include_gon_amd' do before(:each) do - Gon.clear Gon::Request. instance_variable_set(:@request_id, request.object_id) @base = ActionView::Base.new @base.request = request end @@ -284,27 +304,26 @@ end it 'outputs correct js with an integer' do Gon.int = 1 - expect(@base.include_gon_amd).to eq( wrap_script( + expect(@base.include_gon_amd).to eq( wrap_script( 'define(\'gon\',[],function(){'+ 'var gon={};gon[\'int\']=1;return gon;'+ '});') ) end it 'outputs correct module name when given a namespace' do - expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script( + expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script( 'define(\'data\',[],function(){'+ 'var gon={};return gon;'+ '});') ) end end it 'returns exception if try to set public method as variable' do - Gon.clear expect { Gon.all_variables = 123 }.to raise_error expect { Gon.rabl = 123 }.to raise_error end describe '#check_for_rabl_and_jbuilder' do