summaryrefslogtreecommitdiff
path: root/spec/models/global_milestone_spec.rb
blob: cacbab8bcb1a3a3d767057f52027a6d00648e113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'spec_helper'

describe GlobalMilestone, models: true do
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let(:group) { create(:group) }
  let(:project1) { create(:empty_project, group: group) }
  let(:project2) { create(:empty_project, path: 'gitlab-ci', group: group) }
  let(:project3) { create(:empty_project, path: 'cookbook-gitlab', group: group) }

  describe '.build_collection' do
    let(:milestone1_due_date) { 2.weeks.from_now.to_date }

    let!(:milestone1_project1) do
      create(
        :milestone,
        title: "Milestone v1.2",
        project: project1,
        due_date: milestone1_due_date
      )
    end

    let!(:milestone1_project2) do
      create(
        :milestone,
        title: "Milestone v1.2",
        project: project2,
        due_date: milestone1_due_date
      )
    end

    let!(:milestone1_project3) do
      create(
        :milestone,
        title: "Milestone v1.2",
        project: project3,
        due_date: milestone1_due_date
      )
    end

    let!(:milestone2_project1) do
      create(
        :milestone,
        title: "VD-123",
        project: project1,
        due_date: nil
      )
    end

    let!(:milestone2_project2) do
      create(
        :milestone,
        title: "VD-123",
        project: project2,
        due_date: nil
      )
    end

    let!(:milestone2_project3) do
      create(
        :milestone,
        title: "VD-123",
        project: project3,
        due_date: nil
      )
    end

    before do
      projects = [
        project1,
        project2,
        project3
      ]

      @global_milestones = GlobalMilestone.build_collection(projects, {})
    end

    it 'has all project milestones' do
      expect(@global_milestones.count).to eq(2)
    end

    it 'has all project milestones titles' do
      expect(@global_milestones.map(&:title)).to match_array(['Milestone v1.2', 'VD-123'])
    end

    it 'has all project milestones' do
      expect(@global_milestones.map { |group_milestone| group_milestone.milestones.count }.sum).to eq(6)
    end

    it 'sorts collection by due date' do
      expect(@global_milestones.map(&:due_date)).to eq [nil, milestone1_due_date]
    end
  end

  describe '#initialize' do
    let(:milestone1_project1) { create(:milestone, title: "Milestone v1.2", project: project1) }
    let(:milestone1_project2) { create(:milestone, title: "Milestone v1.2", project: project2) }
    let(:milestone1_project3) { create(:milestone, title: "Milestone v1.2", project: project3) }

    before do
      milestones =
        [
          milestone1_project1,
          milestone1_project2,
          milestone1_project3,
        ]
      milestones_relation = Milestone.where(id: milestones.map(&:id))

      @global_milestone = GlobalMilestone.new(milestone1_project1.title, milestones_relation)
    end

    it 'has exactly one group milestone' do
      expect(@global_milestone.title).to eq('Milestone v1.2')
    end

    it 'has all project milestones with the same title' do
      expect(@global_milestone.milestones.count).to eq(3)
    end
  end

  describe '#safe_title' do
    let(:milestone) { create(:milestone, title: "git / test", project: project1) }

    it 'strips out slashes and spaces' do
      global_milestone = GlobalMilestone.new(milestone.title, Milestone.where(id: milestone.id))

      expect(global_milestone.safe_title).to eq('git-test')
    end
  end
end