spec/lib/arstotzka_spec.rb in arstotzka-1.3.2 vs spec/lib/arstotzka_spec.rb in arstotzka-1.4.0
- old
+ new
@@ -198,6 +198,169 @@
it 'retrieves attribute from base json' do
expect(value).to eq(json['user']['name'])
end
end
+
+ context 'when changing configuration of case after class declaration' do
+ let(:json) { { the_value: 'snake', theValue: 'lower_camel', TheValue: 'upper_camel' } }
+
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value
+ end
+ end
+
+ after { described_class.reset_config }
+
+ context 'when changing case to snake' do
+ it 'changes the way the value is fetched' do
+ expect { described_class.configure { |c| c.case :snake } }
+ .to change(dummy, :the_value)
+ .from('lower_camel').to('snake')
+ end
+ end
+
+ context 'when changing case to upper_camel' do
+ it 'changes the way the value is fetched' do
+ expect { described_class.configure { |c| c.case :upper_camel } }
+ .to change(dummy, :the_value)
+ .from('lower_camel').to('upper_camel')
+ end
+ end
+
+ context 'when expose was set to cache' do
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value, cached: true
+ end
+ end
+
+ it 'does not change the way the value is fetched' do
+ expect { described_class.configure { |c| c.case :snake } }
+ .not_to change(dummy, :the_value)
+ end
+ end
+
+ context 'when arstotka was set to cache' do
+ before { described_class.configure { cached true } }
+
+ it 'does not change the way the value is fetched' do
+ expect { described_class.configure { |c| c.case :snake } }
+ .not_to change(dummy, :the_value)
+ end
+ end
+
+ context 'when expose defined the case' do
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value, case: :upper_camel
+ end
+ end
+
+ it 'does not change the way the value is fetched' do
+ expect { described_class.configure { |c| c.case :snake } }
+ .not_to change(dummy, :the_value)
+ end
+ end
+ end
+
+ context 'when changing configuration of cached after class declaration' do
+ let(:json) { { the_value: 'old value' } }
+
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value, case: :snake
+ end
+ end
+
+ after { described_class.reset_config }
+
+ context 'when cached is defined as true after in the config' do
+ context 'when first method call is after value change' do
+ let(:block) do
+ proc do
+ described_class.configure { cached true }
+ json[:the_value] = :symbol
+ end
+ end
+
+ it 'caches after cache change' do
+ expect(&block).to change(dummy, :the_value)
+ .from('old value').to(:symbol)
+ end
+ end
+
+ context 'when first method call is before value change' do
+ let(:block) do
+ proc do
+ described_class.configure { cached true }
+ dummy.the_value
+ json[:the_value] = :symbol
+ end
+ end
+
+ it 'caches the original value' do
+ expect(&block).not_to change(dummy, :the_value)
+ end
+ end
+ end
+
+ context 'when cached is defined as false after in the config' do
+ before { described_class.configure { cached true } }
+
+ let(:block) do
+ proc do
+ described_class.configure { cached false }
+ json[:the_value] = :symbol
+ end
+ end
+
+ it 'changes value' do
+ expect(&block).to change(dummy, :the_value)
+ .from('old value').to(:symbol)
+ end
+ end
+
+ context 'when cached was defined in expose' do
+ context 'when cached was set as true' do
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value, case: :snake, cached: true
+ end
+ end
+
+ let(:block) do
+ proc do
+ described_class.configure { cached false }
+ json[:the_value] = :symbol
+ end
+ end
+
+ it 'does not change cached state' do
+ expect(&block).not_to change(dummy, :the_value)
+ end
+ end
+
+ context 'when cached was set as false' do
+ let(:dummy_class) do
+ Class.new(Arstotzka::Dummy) do
+ expose :the_value, case: :snake, cached: false
+ end
+ end
+
+ let(:block) do
+ proc do
+ described_class.configure { cached true }
+ dummy.the_value
+ json[:the_value] = :symbol
+ end
+ end
+
+ it 'does not change cached state' do
+ expect(&block).to change(dummy, :the_value)
+ .from('old value').to(:symbol)
+ end
+ end
+ end
+ end
end