milestone_spec.rb 3.51 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: milestones
#
5 6 7
#  id          :integer          not null, primary key
#  title       :string(255)      not null
#  project_id  :integer          not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
8 9
#  description :text
#  due_date    :date
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10 11
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12
#  state       :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
#  iid         :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15
#

16 17 18
require 'spec_helper'

describe Milestone do
19
  describe "Associations" do
20 21
    it { is_expected.to belong_to(:project) }
    it { is_expected.to have_many(:issues) }
22 23
  end

24 25 26
  describe "Mass assignment" do
  end

27
  describe "Validation" do
28
    before { subject.stub(set_iid: false) }
29 30
    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_presence_of(:project) }
31 32
  end

33 34
  let(:milestone) { create(:milestone) }
  let(:issue) { create(:issue) }
35

36 37
  describe "#percent_complete" do
    it "should not count open issues" do
38
      milestone.issues << issue
39
      expect(milestone.percent_complete).to eq(0)
40 41
    end

42
    it "should count closed issues" do
Andrew8xx8's avatar
Andrew8xx8 committed
43
      issue.close
44
      milestone.issues << issue
45
      expect(milestone.percent_complete).to eq(100)
46
    end
47

48
    it "should recover from dividing by zero" do
49
      expect(milestone.issues).to receive(:count).and_return(0)
50
      expect(milestone.percent_complete).to eq(0)
51 52 53
    end
  end

54 55 56
  describe "#expires_at" do
    it "should be nil when due_date is unset" do
      milestone.update_attributes(due_date: nil)
57
      expect(milestone.expires_at).to be_nil
58 59
    end

60 61
    it "should not be nil when due_date is set" do
      milestone.update_attributes(due_date: Date.tomorrow)
62
      expect(milestone.expires_at).to be_present
63
    end
64
  end
65 66 67 68 69 70 71

  describe :expired? do
    context "expired" do
      before do
        milestone.stub(due_date: Date.today.prev_year)
      end

72
      it { expect(milestone.expired?).to be_truthy }
73 74 75 76 77 78 79
    end

    context "not expired" do
      before do
        milestone.stub(due_date: Date.today.next_year)
      end

80
      it { expect(milestone.expired?).to be_falsey }
81 82 83 84 85 86 87 88 89 90 91
    end
  end

  describe :percent_complete do
    before do
      milestone.stub(
        closed_items_count: 3,
        total_items_count: 4
      )
    end

92
    it { expect(milestone.percent_complete).to eq(75) }
93 94 95 96 97
  end

  describe :items_count do
    before do
      milestone.issues << create(:issue)
Andrew8xx8's avatar
Andrew8xx8 committed
98
      milestone.issues << create(:closed_issue)
99 100 101
      milestone.merge_requests << create(:merge_request)
    end

102 103 104 105
    it { expect(milestone.closed_items_count).to eq(1) }
    it { expect(milestone.open_items_count).to eq(2) }
    it { expect(milestone.total_items_count).to eq(3) }
    it { expect(milestone.is_empty?).to be_falsey }
106 107 108
  end

  describe :can_be_closed? do
109
    it { expect(milestone.can_be_closed?).to be_truthy }
110
  end
111 112

  describe :is_empty? do
113
    before do
114 115
      issue = create :closed_issue, milestone: milestone
      merge_request = create :merge_request, milestone: milestone
116
    end
117

118
    it 'Should return total count of issues and merge requests assigned to milestone' do
119
      expect(milestone.total_items_count).to eq 2
120 121 122 123
    end
  end

  describe :can_be_closed? do
124
    before do
125
      milestone = create :milestone
126 127 128 129
      create :closed_issue, milestone: milestone

      issue = create :issue
    end
130

Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
131
    it 'should be true if milestone active and all nested issues closed' do
132
      expect(milestone.can_be_closed?).to be_truthy
133 134
    end

Johannes Schleifenbaum's avatar
Johannes Schleifenbaum committed
135
    it 'should be false if milestone active and not all nested issues closed' do
136
      issue.milestone = milestone
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
137
      issue.save
138

139
      expect(milestone.can_be_closed?).to be_falsey
140 141 142
    end
  end

143
end