2020-12-02 10:09:37 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe TimeZoneHelper, :aggregate_failures do
|
|
|
|
describe '#timezone_data' do
|
2020-12-05 10:09:32 -05:00
|
|
|
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
|
|
|
|
|
2021-12-06 19:14:07 -05:00
|
|
|
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
|
|
|
|
|
2020-12-05 10:09:32 -05:00
|
|
|
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
|
|
|
|
)
|
2020-12-02 10:09:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-05 10:09:32 -05:00
|
|
|
context 'with unknown format' do
|
|
|
|
subject(:timezone_data) { helper.timezone_data(format: :unknown) }
|
2020-12-02 10:09:37 -05:00
|
|
|
|
2020-12-05 10:09:32 -05:00
|
|
|
it 'raises an exception' do
|
2021-12-06 19:14:07 -05:00
|
|
|
expect { timezone_data }.to raise_error ArgumentError, 'Invalid format :unknown. Valid formats are :short, :abbr, :full.'
|
2020-12-05 10:09:32 -05:00
|
|
|
end
|
2020-12-02 10:09:37 -05:00
|
|
|
end
|
|
|
|
end
|
2021-07-28 14:10:23 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-10-01 11:12:53 -04:00
|
|
|
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
|
|
|
|
|
2021-07-28 14:10:23 -04:00
|
|
|
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
|
2021-11-08 22:42:22 -05:00
|
|
|
|
2021-12-07 10:15:03 -05:00
|
|
|
describe '#local_timezone_instance' do
|
2021-11-08 22:42:22 -05:00
|
|
|
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
|
2021-12-07 10:15:03 -05:00
|
|
|
expect(helper.local_timezone_instance(nil).name).to eq(timezone)
|
2021-11-08 22:42:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when timezone is blank' do
|
|
|
|
it 'returns the system timezone instance' do
|
2021-12-07 10:15:03 -05:00
|
|
|
expect(helper.local_timezone_instance('').name).to eq(timezone)
|
2021-11-08 22:42:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a valid timezone is passed' do
|
|
|
|
it 'returns the local time instance' do
|
2021-12-07 10:15:03 -05:00
|
|
|
expect(helper.local_timezone_instance('America/Los_Angeles').name).to eq('America/Los_Angeles')
|
2021-11-08 22:42:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an invalid timezone is passed' do
|
|
|
|
it 'returns the system timezone instance' do
|
2021-12-07 10:15:03 -05:00
|
|
|
expect(helper.local_timezone_instance('Foo/Bar').name).to eq(timezone)
|
2021-11-08 22:42:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-12-02 10:09:37 -05:00
|
|
|
end
|