require 'spec_helper'

describe Calentry do

  let(:rubrik) {FactoryGirl.create(:rubrik)}
  let(:user) {FactoryGirl.create(:user)}
  let(:calendar) {FactoryGirl.create(:calendar,updated_at: 1.hour.ago)}
  let(:neuigkeit) {FactoryGirl.create(:neuigkeit, rubrik_id: rubrik.id, author_id: user.id, updated_at: 1.hour.ago)}
  subject(:calentry)   {FactoryGirl.build(:calentry, calendar_id: calendar.id, object_type: "Neuigkeit",object_id: neuigkeit.id)}

  it "is valid with full data" do
    expect(calentry).to be_valid
  end

  [:ende,:start,:typ].each do |attr|
    it "is not valid without #{attr}" do 
      e= FactoryGirl.build(:calentry, calendar_id: calendar.id, object_type: "Neuigkeit",object_id: neuigkeit.id,  attr => nil)
      expect(e).not_to be_valid 
      expect(e).to have_at_least(1).errors_on(attr)
    end
 end
  it "updates updated_at of neuigekit" do
    expect {calentry.dauer+=1.hour;calentry.save}.to change{calentry.object.updated_at}
  end
  it "updates updated_at of calendar" do
    expect {calentry.dauer+=1.hour;calentry.save}.to change{calentry.calendar.updated_at}
  end 
  it "changes ende with dauer" do
    expect {calentry.dauer=7}.to change(calentry,:ende).to(calentry.start+7.hours)
  end
  it "changes dauer with ende" do 
    expect {calentry.ende=calentry.start+7.hours}.to change(calentry,:dauer).to(7)
  end
  it "sets is upcomming" do
    calentry.start=1.hour.from_now
    calentry.dauer=2
    expect(calentry.is_upcomming?).to be true    
    expect(calentry.is_past?).to be false
    expect(calentry.is_ongoing?).to be false

    calentry.start=1.hour.ago
    calentry.dauer=2

    expect(calentry.is_upcomming?).to be false
    expect(calentry.is_past?).to be false
    expect(calentry.is_ongoing?).to be true

    calentry.start=4.hour.ago
    calentry.dauer=2

    expect(calentry.is_upcomming?).to be false
    expect(calentry.is_past?).to be true
    expect(calentry.is_ongoing?).to be false

  end

end
