debian-mirror-gitlab/spec/serializers/entity_date_helper_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4.1 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe EntityDateHelper do
2017-08-17 22:00:37 +05:30
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-10-15 14:42:47 +05:30
Rails.logger.debug date_helper_class.inspect
2017-08-17 22:00:37 +05:30
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-10-15 14:42:47 +05:30
describe '#remaining_days_in_words' do
around do |example|
2023-03-04 22:38:38 +05:30
travel_to(Time.utc(2017, 3, 17)) { example.run }
2018-10-15 14:42:47 +05:30
end
context 'when less than 31 days remaining' do
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(12.days.from_now.utc.to_date) }
2018-10-15 14:42:47 +05:30
it 'returns days remaining' do
expect(milestone_remaining).to eq("<strong>12</strong> days remaining")
end
end
2019-12-04 20:38:33 +05:30
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
2023-03-04 22:38:38 +05:30
travel_back
travel_to(Time.utc(2017, 3, 17, 13, 10)) do
2019-12-04 20:38:33 +05:30
expect(milestone_remaining).to eq("<strong>1</strong> day remaining")
end
end
end
2018-10-15 14:42:47 +05:30
context 'when less than 1 year and more than 30 days remaining' do
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.months.from_now.utc.to_date) }
2018-10-15 14:42:47 +05:30
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
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words((1.year.from_now + 2.days).utc.to_date) }
2018-10-15 14:42:47 +05:30
it 'returns years remaining' do
expect(milestone_remaining).to eq("<strong>1</strong> year remaining")
end
end
context 'when milestone is expired' do
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.days.ago.utc.to_date) }
2018-10-15 14:42:47 +05:30
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
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.from_now.utc.to_date) }
2018-10-15 14:42:47 +05:30
it 'returns "Upcoming"' do
expect(milestone_remaining).to eq("<strong>Upcoming</strong>")
end
end
context 'when milestone has start_date in the past' do
2019-02-15 15:39:39 +05:30
let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.ago.utc.to_date) }
2018-10-15 14:42:47 +05:30
it 'returns days elapsed' do
expect(milestone_remaining).to eq("<strong>2</strong> days elapsed")
end
end
end
2017-08-17 22:00:37 +05:30
end