Ensure milestone counts work with no data
Commit originally written by @smcgivern
This commit is contained in:
parent
19e2bf1c21
commit
8499de19c9
2 changed files with 24 additions and 25 deletions
|
@ -36,14 +36,15 @@ module MilestonesHelper
|
|||
end
|
||||
|
||||
# Returns count of milestones for different states
|
||||
# Uses explicit hash keys as the 'opened' state URL params differs from the db value
|
||||
# Uses explicit hash keys as the 'opened' state URL params differs from the db value
|
||||
# and we need to add the total
|
||||
def milestone_counts(milestones)
|
||||
counts = milestones.reorder(nil).group(:state).count
|
||||
|
||||
{
|
||||
opened: counts['active'],
|
||||
closed: counts['closed'],
|
||||
all: counts.values.sum
|
||||
opened: counts['active'] || 0,
|
||||
closed: counts['closed'] || 0,
|
||||
all: counts.values.sum || 0
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -3,33 +3,31 @@ require 'spec_helper'
|
|||
describe MilestonesHelper do
|
||||
describe '#milestone_counts' do
|
||||
let(:project) { FactoryGirl.create(:project) }
|
||||
let!(:milestone_1) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
let!(:milestone_2) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
let!(:milestone_3) { FactoryGirl.create(:closed_milestone, project: project) }
|
||||
|
||||
let(:counts) { helper.milestone_counts(project.milestones) }
|
||||
|
||||
it 'returns a hash containing three items' do
|
||||
expect(counts.length).to eq 3
|
||||
context 'when there are milestones' do
|
||||
let!(:milestone_1) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
let!(:milestone_2) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
let!(:milestone_3) { FactoryGirl.create(:closed_milestone, project: project) }
|
||||
|
||||
it 'returns the correct counts' do
|
||||
expect(counts).to eq(opened: 2, closed: 1, all: 3)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns a hash containing "opened" key' do
|
||||
expect(counts.has_key?(:opened)).to eq true
|
||||
context 'when there are only milestones of one type' do
|
||||
let!(:milestone_1) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
let!(:milestone_2) { FactoryGirl.create(:active_milestone, project: project) }
|
||||
|
||||
it 'returns the correct counts' do
|
||||
expect(counts).to eq(opened: 2, closed: 0, all: 2)
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns a hash containing "closed" key' do
|
||||
expect(counts.has_key?(:closed)).to eq true
|
||||
end
|
||||
|
||||
it 'returns a hash containing "all" key' do
|
||||
expect(counts.has_key?(:all)).to eq true
|
||||
end
|
||||
|
||||
it 'shows "all" object is the sum of "opened" and "closed" objects' do
|
||||
puts counts.as_json
|
||||
total = counts[:opened] + counts[:closed]
|
||||
expect(counts[:all]).to eq total
|
||||
context 'when there are no milestones' do
|
||||
it 'returns the correct counts' do
|
||||
expect(counts).to eq(opened: 0, closed: 0, all: 0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue