Sha256: e8db314e074cd0341a652c184b597c0b5d9b33788b23ddc44fbd145fe9aa4def

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

require 'spec_helper'
require 'compo'

# Mock implementation of a ParentTracker
class MockParentTracker
  include Compo::ParentTracker

  def initialize(parent, id_function)
    @parent = parent
    @id_function = id_function
  end
end

describe MockParentTracker do
  subject { MockParentTracker.new(parent, id_function) }
  let(:parent) { double(:parent) }
  let(:id_function) { double(:id_function) }

  describe '#parent' do
    it 'returns the current parent' do
      expect(subject.parent).to eq(parent)
    end
  end

  describe '#id' do
    it 'calls the current ID function and returns its result' do
      allow(id_function).to receive(:call).and_return(:id)
      expect(id_function).to receive(:call).once.with(no_args)

      expect(subject.id).to eq(:id)
    end
  end

  describe '#update_parent' do
    let(:new_parent) { double(:new_parent) }
    let(:new_id_function) { double(:new_id_function) }

    it 'sets the parent to the new value' do
      subject.update_parent(new_parent, new_id_function)
      expect(subject.parent).to eq(new_parent)
    end

    it 'sets the ID function to the new ID function' do
      allow(new_id_function).to receive(:call).and_return(:new_id)
      expect(new_id_function).to receive(:call).once

      subject.update_parent(new_parent, new_id_function)
      expect(subject.id).to eq(:new_id)
    end
  end

  describe '#remove_parent' do
    it 'sets the parent to nil' do
      subject.remove_parent
      expect(subject.parent).to be_nil
    end

    it 'sets the ID function to one returning nil' do
      subject.remove_parent
      expect(subject.id).to be_nil
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
compo-0.1.5 spec/parent_tracker_spec.rb
compo-0.1.4 spec/parent_tracker_spec.rb
compo-0.1.3 spec/parent_tracker_spec.rb
compo-0.1.2 spec/parent_tracker_spec.rb
compo-0.1.1 spec/parent_tracker_spec.rb
compo-0.1.0 spec/parent_tracker_spec.rb