Welcome to mirror list, hosted at ThFree Co, Russian Federation.

entity_date_helper_spec.rb « serializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70094991c09d633455c306db7cc927f67d23e81d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.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
    Rails.logger.debug date_helper_class.inspect
    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

  describe '#remaining_days_in_words' do
    let(:current_time) { Time.utc(2017, 3, 17) }

    around do |example|
      travel_to(current_time) { example.run }
    end

    context 'when less than 31 days remaining' do
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words(12.days.from_now.utc.to_date) }

      it 'returns days remaining' do
        expect(milestone_remaining).to eq("<strong>12</strong> days remaining")
      end
    end

    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

      context 'when queried mid-day' do
        let(:current_time) { Time.utc(2017, 3, 17, 13, 10) }

        it 'returns 1 day remaining' do
          expect(milestone_remaining).to eq("<strong>1</strong> day remaining")
        end
      end
    end

    context 'when less than 1 year and more than 30 days remaining' do
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.months.from_now.utc.to_date) }

      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
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words((1.year.from_now + 2.days).utc.to_date) }

      it 'returns years remaining' do
        expect(milestone_remaining).to eq("<strong>1</strong> year remaining")
      end
    end

    context 'when milestone is expired' do
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words(2.days.ago.utc.to_date) }

      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
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.from_now.utc.to_date) }

      it 'returns "Upcoming"' do
        expect(milestone_remaining).to eq("<strong>Upcoming</strong>")
      end
    end

    context 'when milestone has start_date in the past' do
      let(:milestone_remaining) { date_helper_class.remaining_days_in_words(nil, 2.days.ago.utc.to_date) }

      it 'returns days elapsed' do
        expect(milestone_remaining).to eq("<strong>2</strong> days elapsed")
      end
    end
  end
end