require 'spec_helper'

describe Supplier do
  let(:supplier){ build :supplier }

  describe '#add_section_on_create through create' do
    it 'calls add_section_on_create only for create' do
      supplier = build :supplier
      expect( supplier ).to receive(:add_section_on_create).once
      supplier.save
      supplier.name = "OtherSupplierName7"
      expect( supplier ).not_to receive(:add_section_on_create)
      supplier.save
    end

    it "creates a default section" do
      supplier = create :supplier
      supplier.sections.size.should be 1
    end
  end

  # property open
  describe :open do
    it 'should be false by default' do
      supplier.open.should == false
    end

    it 'should not be open? by default' do
      supplier.open?.should == false
    end

    it 'should be closed? by default' do
      supplier.closed?.should == true
    end

    describe :mark_as_open! do
      before :each do
        supplier.mark_as_open!
      end
      it 'should be persisted in the database' do
        supplier.reload
        supplier.open.should == true
      end

      it 'should be open?' do
        supplier.open?.should == true
      end

      it 'should not be closed?' do
        supplier.closed?.should == false
      end

    end

    describe :mark_as_closed! do
      before :each do
        supplier.mark_as_open!
        supplier.mark_as_closed!
      end

      it 'should be persisted in the database' do
        supplier.reload
        supplier.open.should == false
      end

      it 'should be open?' do
        supplier.open?.should == false
      end

      it 'should not be closed?' do
        supplier.closed?.should == true
      end
    end

  end

  describe '#decrement_orders_placed_count!' do
    it 'decreases orders_placed' do
      Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
      supplier.decrement_orders_placed_count!.should == 8
    end
  end

  describe '#employee_settings' do
    let(:employee){ create :employee }
    it 'is initialized by default' do
      supplier.employee_settings.should be_a SupplierEmployeesSettings
    end

    it 'returns null object without employee' do
      supplier.employee_settings.for_employee(nil).should_not be_present
    end
    it 'returns employee_settings on for employee' do
      supplier.add_employee employee
      settings = supplier.employee_settings.for_employee(employee)
      settings.should be_present
      settings.manager?.should be false
    end

    it 'indicates a manager when specified as such and is persisted' do
      supplier.add_manager employee
      settings = supplier.employee_settings.for_employee(employee)
      settings.manager?.should be true
      reloaded_supplier = Supplier.find(supplier.id)
      settings = reloaded_supplier.settings_for(employee)
      settings.manager?.should be true
    end

    it "Adds another manager (second appears to work different as the first)" do
      supplier.add_manager employee
      new_employee = create :employee, email: 'new-employee@example.com'
      supplier.add_manager new_employee
      reloaded_supplier = Supplier.find(supplier.id)
      settings = reloaded_supplier.settings_for(new_employee)
      settings.manager?.should be true
    end

  end

  describe '#add_employee' do
    it 'resets the settings to the supplier settings' do
      employee = create :employee
      expect( employee.settings ).not_to be_present
      expect( employee.settings.supplier ).not_to be_present
      supplier.add_employee employee
      new_supplier_referenced = (employee.settings.supplier == supplier)
      new_supplier_referenced.should be true
    end

  end
end
