Sha256: e364779138559eeee915887da17f50b0696b0e0fa6ac0c6e2ffa6471b8bd9086

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

RSpec.describe Dry::View::Scope do
  subject(:scope) { described_class.new(renderer: renderer, context: context, locals: locals) }

  let(:locals) { {} }
  let(:context) { double(:context) }
  let(:renderer) { spy(:renderer) }

  describe '#render' do
    it 'renders a partial with itself as the scope' do
      scope.render(:info)
      expect(renderer).to have_received(:partial).with(:info, scope)
    end

    it 'renders a partial with provided locals' do
      scope_with_locals = described_class.new(renderer: renderer, context: context, locals: {foo: 'bar'})

      scope.render(:info, foo: 'bar')
      expect(renderer).to have_received(:partial).with(:info, scope_with_locals)
    end
  end

  describe '#method_missing' do
    context 'matching locals' do
      let(:locals) { {greeting: 'hello from locals'} }
      let(:context) { double('context', greeting: 'hello from context') }

      it 'returns a matching value from the locals, in favour of a matching method on the context' do
        expect(scope.greeting).to eq 'hello from locals'
      end
    end

    context 'matching context' do
      let(:context) { double('context', greeting: 'hello from context') }

      it 'calls the matching method on the context' do
        expect(scope.greeting).to eq 'hello from context'
      end

      it 'forwards all arguments to the method' do
        blk = -> { }
        scope.greeting 'args', &blk

        expect(context).to have_received(:greeting).with('args', &blk)
      end
    end

    describe 'no matches' do
      it 'raises an error' do
        expect { scope.greeting }.to raise_error(NoMethodError)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-view-0.5.3 spec/unit/scope_spec.rb
dry-view-0.5.2 spec/unit/scope_spec.rb
dry-view-0.5.1 spec/unit/scope_spec.rb
dry-view-0.5.0 spec/unit/scope_spec.rb
dry-view-0.4.0 spec/unit/scope_spec.rb