Commit a81de9ab authored by Timothy Andrew's avatar Timothy Andrew

Add a spec for the `CycleAnalytics#issue` method.

parent ebc03833
......@@ -319,6 +319,7 @@ group :test do
gem 'webmock', '~> 1.21.0'
gem 'test_after_commit', '~> 0.4.2'
gem 'sham_rack', '~> 1.3.6'
gem 'timecop', '~> 0.8.0'
end
group :production do
......
......@@ -971,6 +971,7 @@ DEPENDENCIES
teaspoon-jasmine (~> 2.2.0)
test_after_commit (~> 0.4.2)
thin (~> 1.7.0)
timecop (~> 0.8.0)
turbolinks (~> 2.5.0)
u2f (~> 0.2.1)
uglifier (~> 2.7.2)
......
require 'spec_helper'
describe 'CycleAnalytics#issue', models: true do
let(:project) { create(:project) }
subject { CycleAnalytics.new }
context "when calculating the median of times between:
start: issue created_at
end: milestone first added to issue
OR
list-label first added to issue
" do
context "when a milestone is added to the issue" do
it "calculates the median of available durations" do
start_and_end_times = Array.new(5) do
start_time = Time.now
end_time = rand(1..10).days.from_now
milestone = create(:milestone, project: project)
issue = Timecop.freeze(start_time) { create(:issue, project: project) }
Timecop.freeze(end_time) { issue.update(milestone: milestone) }
[start_time, end_time]
end
median_start_time, median_end_time = start_and_end_times[2]
expect(subject.issue).to eq(median_end_time - median_start_time)
end
end
context "when a label is added to the issue" do
it "calculates the median of available durations when the label is a list-label" do
start_and_end_times = Array.new(5) do
start_time = Time.now
end_time = rand(1..10).days.from_now
list_label = create(:label, lists: [create(:list)])
issue = Timecop.freeze(start_time) { create(:issue, project: project) }
Timecop.freeze(end_time) { issue.update(label_ids: [list_label.id]) }
[start_time, end_time]
end
median_start_time, median_end_time = start_and_end_times[2]
expect(subject.issue).to eq(median_end_time - median_start_time)
end
it "does not make a calculation for regular labels" do
5.times do
start_time = Time.now
end_time = rand(1..10).days.from_now
regular_label = create(:label)
issue = Timecop.freeze(start_time) { create(:issue, project: project) }
Timecop.freeze(end_time) { issue.update(label_ids: [regular_label.id]) }
[start_time, end_time]
end
expect(subject.issue).to be_nil
end
end
context "when a milestone and list-label are both added to the issue" do
it "uses the time the milestone was added as the 'end time'" do
start_time = Time.now
milestone_add_time = rand(1..10).days.from_now
list_label_add_time = rand(1..10).days.from_now
milestone = create(:milestone, project: project)
list_label = create(:label, lists: [create(:list)])
issue = Timecop.freeze(start_time) { create(:issue, project: project) }
Timecop.freeze(milestone_add_time) { issue.update(milestone: milestone) }
Timecop.freeze(list_label_add_time) { issue.update(label_ids: [list_label.id]) }
expect(subject.issue).to eq(milestone_add_time - start_time)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment