summaryrefslogtreecommitdiff
path: root/spec/helpers/time_zone_helper_spec.rb
blob: e8d96ee070044a3b86ad4fd4e262cde211c097a7 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe TimeZoneHelper, :aggregate_failures do
  describe '#timezone_data' do
    context 'with short format' do
      subject(:timezone_data) { helper.timezone_data }

      it 'matches schema' do
        expect(timezone_data).not_to be_empty

        timezone_data.each_with_index do |timezone_hash, i|
          expect(timezone_hash.keys).to contain_exactly(
            :identifier,
            :name,
            :offset
          ), "Failed at index #{i}"
        end
      end

      it 'formats for display' do
        tz = ActiveSupport::TimeZone.all[0]

        expect(timezone_data[0]).to eq(
          identifier: tz.tzinfo.identifier,
          name: tz.name,
          offset: tz.now.utc_offset
        )
      end
    end

    context 'with abbr format' do
      subject(:timezone_data) { helper.timezone_data(format: :abbr) }

      it 'matches schema' do
        expect(timezone_data).not_to be_empty

        timezone_data.each_with_index do |timezone_hash, i|
          expect(timezone_hash.keys).to contain_exactly(
            :identifier,
            :abbr
          ), "Failed at index #{i}"
        end
      end

      it 'formats for display' do
        tz = ActiveSupport::TimeZone.all[0]

        expect(timezone_data[0]).to eq(
          identifier: tz.tzinfo.identifier,
          abbr: tz.tzinfo.strftime('%Z')
        )
      end
    end

    context 'with full format' do
      subject(:timezone_data) { helper.timezone_data(format: :full) }

      it 'matches schema' do
        expect(timezone_data).not_to be_empty

        timezone_data.each_with_index do |timezone_hash, i|
          expect(timezone_hash.keys).to contain_exactly(
            :identifier,
            :name,
            :abbr,
            :offset,
            :formatted_offset
          ), "Failed at index #{i}"
        end
      end

      it 'formats for display' do
        tz = ActiveSupport::TimeZone.all[0]

        expect(timezone_data[0]).to eq(
          identifier: tz.tzinfo.identifier,
          name: tz.name,
          abbr: tz.tzinfo.strftime('%Z'),
          offset: tz.now.utc_offset,
          formatted_offset: tz.now.formatted_offset
        )
      end
    end

    context 'with unknown format' do
      subject(:timezone_data) { helper.timezone_data(format: :unknown) }

      it 'raises an exception' do
        expect { timezone_data }.to raise_error ArgumentError, 'Invalid format :unknown. Valid formats are :short, :abbr, :full.'
      end
    end
  end

  describe '#local_time' do
    let_it_be(:timezone) { 'America/Los_Angeles' }

    before do
      travel_to Time.find_zone(timezone).local(2021, 7, 20, 15, 30, 45)
    end

    context 'when timezone is `nil`' do
      it 'returns `nil`' do
        expect(helper.local_time(nil)).to eq(nil)
      end
    end

    context 'when timezone is blank' do
      it 'returns `nil`' do
        expect(helper.local_time('')).to eq(nil)
      end
    end

    context 'when a valid timezone is passed' do
      it 'returns local time' do
        expect(helper.local_time(timezone)).to eq('3:30 PM')
      end
    end

    context 'when an invalid timezone is passed' do
      it 'returns local time using the configured default timezone (UTC in this case)' do
        expect(helper.local_time('Foo/Bar')).to eq('10:30 PM')
      end
    end
  end

  describe '#local_timezone_instance' do
    let_it_be(:timezone) { 'UTC' }

    before do
      travel_to Time.find_zone(timezone).local(2021, 7, 20, 15, 30, 45)
    end

    context 'when timezone is `nil`' do
      it 'returns the system timezone instance' do
        expect(helper.local_timezone_instance(nil).name).to eq(timezone)
      end
    end

    context 'when timezone is blank' do
      it 'returns the system timezone instance' do
        expect(helper.local_timezone_instance('').name).to eq(timezone)
      end
    end

    context 'when a valid timezone is passed' do
      it 'returns the local time instance' do
        expect(helper.local_timezone_instance('America/Los_Angeles').name).to eq('America/Los_Angeles')
      end
    end

    context 'when an invalid timezone is passed' do
      it 'returns the system timezone instance' do
        expect(helper.local_timezone_instance('Foo/Bar').name).to eq(timezone)
      end
    end
  end
end