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

D
Dmitriy Zaporozhets 已提交
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 24
  end

  describe "Validation" do
25 26 27 28
    before do
      allow(subject).to receive(:set_iid).and_return(false)
    end

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
A
Andrew8xx8 已提交
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

  describe :expired? do
    context "expired" do
      before do
69
        allow(milestone).to receive(:due_date).and_return(Date.today.prev_year)
70 71
      end

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

    context "not expired" do
      before do
77
        allow(milestone).to receive(:due_date).and_return(Date.today.next_year)
78 79
      end

80
      it { expect(milestone.expired?).to be_falsey }
81 82 83 84 85
    end
  end

  describe :percent_complete do
    before do
86
      allow(milestone).to receive_messages(
87 88 89 90 91
        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)
A
Andrew8xx8 已提交
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
A
Andrew8xx8 已提交
111 112

  describe :is_empty? do
113
    before do
A
Andrew8xx8 已提交
114 115
      issue = create :closed_issue, milestone: milestone
      merge_request = create :merge_request, milestone: milestone
116
    end
A
Andrew8xx8 已提交
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
A
Andrew8xx8 已提交
120 121 122 123
    end
  end

  describe :can_be_closed? do
124
    before do
A
Andrew8xx8 已提交
125
      milestone = create :milestone
126 127 128 129
      create :closed_issue, milestone: milestone

      issue = create :issue
    end
A
Andrew8xx8 已提交
130

J
Johannes Schleifenbaum 已提交
131
    it 'should be true if milestone active and all nested issues closed' do
132
      expect(milestone.can_be_closed?).to be_truthy
A
Andrew8xx8 已提交
133 134
    end

J
Johannes Schleifenbaum 已提交
135
    it 'should be false if milestone active and not all nested issues closed' do
136
      issue.milestone = milestone
A
Andrey Kumanyaev 已提交
137
      issue.save
A
Andrew8xx8 已提交
138

139
      expect(milestone.can_be_closed?).to be_falsey
A
Andrew8xx8 已提交
140 141 142
    end
  end

D
Dmitriy Zaporozhets 已提交
143
end