spec/lib/magicka/aggregator_spec.rb in magicka-0.5.6 vs spec/lib/magicka/aggregator_spec.rb in magicka-0.6.0

- old
+ new

@@ -9,10 +9,35 @@ let(:model) { :my_model } let(:renderer) { instance_double('renderer') } let(:template) { 'templates/forms/input' } let(:locals) { {} } + describe '.type' do + let(:type) { %w[form display other].sample } + + it 'sets aggregator type' do + expect { aggregator_class.type(type) } + .to change(aggregator_class, :type) + .from(nil).to(type.to_sym) + end + + context 'when type has not been set' do + let(:aggregator_class) do + Class.new(described_class) do + def self.name + 'Magicka::MyClass' + end + end + end + + it 'Uses class name as type' do + expect(aggregator_class.type) + .to eq(:my_class) + end + end + end + describe '.with_element' do context 'when seeting element class only' do it do expect { aggregator_class.with_element(Magicka::Input) } .to add_method(:input) @@ -201,9 +226,73 @@ aggregator.input(field, model: 'my_custom_model', **arguments) expect(renderer).to have_received(:render) end end + end + end + end + + describe '#with_model' do + let(:expected_aggregator) do + aggregator_class.new(renderer, 'my_model.inner') + end + + it do + aggregator.with_model(:inner) do |new_aggregator| + expect(new_aggregator).to eq(expected_aggregator) + end + end + end + + describe '#only' do + let(:aggregator_class) do + Class.new(described_class) do + type :included + end + end + + context 'when the type is included in the list' do + it 'executes the block' do + value = 0 + + aggregator.only(:not_included, :included, :other) { value += 1 } + expect(value).to eq(1) + end + end + + context 'when the type is not included in the list' do + it 'does not execute the block' do + value = 0 + + aggregator.only(:not_included, :other) { value += 1 } + expect(value).to be_zero + end + end + end + + describe '#except' do + let(:aggregator_class) do + Class.new(described_class) do + type :included + end + end + + context 'when the type is included in the list' do + it 'does not execute the block' do + value = 0 + + aggregator.except(:not_included, :included, :other) { value += 1 } + expect(value).to be_zero + end + end + + context 'when the type is not included in the list' do + it 'executes the block' do + value = 0 + + aggregator.except(:not_included, :other) { value += 1 } + expect(value).to eq(1) end end end end