2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-16 09:55:20 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe EntityDateHelper do
|
|
|
|
let(:date_helper_class) { Class.new { include EntityDateHelper }.new }
|
|
|
|
|
|
|
|
it 'converts 0 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(0)).to eq(seconds: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 40 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(40)).to eq(seconds: 40)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 60 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(60)).to eq(mins: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 70 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(70)).to eq(mins: 1, seconds: 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 3600 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(3600)).to eq(hours: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 3750 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(3750)).to eq(hours: 1, mins: 2, seconds: 30)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 86400 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(86400)).to eq(days: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 86560 seconds' do
|
2018-04-19 10:43:20 -04:00
|
|
|
Rails.logger.debug date_helper_class.inspect
|
2016-11-16 09:55:20 -05:00
|
|
|
expect(date_helper_class.distance_of_time_as_hash(86560)).to eq(days: 1, mins: 2, seconds: 40)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 86760 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(99760)).to eq(days: 1, hours: 3, mins: 42, seconds: 40)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts 986760 seconds' do
|
|
|
|
expect(date_helper_class.distance_of_time_as_hash(986760)).to eq(days: 11, hours: 10, mins: 6)
|
|
|
|
end
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
describe '#remaining_days_in_words' do
|
|
|
|
around do |example|
|
|
|
|
Timecop.freeze(Time.utc(2017, 3, 17)) { example.run }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when less than 31 days remaining' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(12.days.from_now.utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns days remaining' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>12</strong> days remaining")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-22 07:29:40 -04:00
|
|
|
context 'when milestone due date is today' do
|
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(Date.today) }
|
|
|
|
|
|
|
|
it 'returns today' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>Today</strong>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when milestone due date is tomorrow' do
|
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(Date.tomorrow) }
|
|
|
|
|
|
|
|
it 'returns 1 day remaining' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>1</strong> day remaining")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 1 day remaining when queried mid-day' do
|
|
|
|
Timecop.freeze(Time.utc(2017, 3, 17, 13, 10)) do
|
|
|
|
expect(milestone_remaining).to eq("<strong>1</strong> day remaining")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-19 10:43:20 -04:00
|
|
|
context 'when less than 1 year and more than 30 days remaining' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.months.from_now.utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns months remaining' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>2</strong> months remaining")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when more than 1 year remaining' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words((1.year.from_now + 2.days).utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns years remaining' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>1</strong> year remaining")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when milestone is expired' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.days.ago.utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns "Past due"' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>Past due</strong>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when milestone has start_date in the future' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.from_now.utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns "Upcoming"' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>Upcoming</strong>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when milestone has start_date in the past' do
|
2018-11-29 23:03:35 -05:00
|
|
|
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.ago.utc.to_date) }
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
it 'returns days elapsed' do
|
|
|
|
expect(milestone_remaining).to eq("<strong>2</strong> days elapsed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-16 10:09:24 -05:00
|
|
|
end
|