diff options
Diffstat (limited to 'spec/lib/gitlab/danger/teammate_spec.rb')
-rw-r--r-- | spec/lib/gitlab/danger/teammate_spec.rb | 125 |
1 files changed, 66 insertions, 59 deletions
diff --git a/spec/lib/gitlab/danger/teammate_spec.rb b/spec/lib/gitlab/danger/teammate_spec.rb index ea5aecbc597..a0540a9fbf5 100644 --- a/spec/lib/gitlab/danger/teammate_spec.rb +++ b/spec/lib/gitlab/danger/teammate_spec.rb @@ -2,14 +2,27 @@ require 'fast_spec_helper' +require 'timecop' require 'rspec-parameterized' require 'gitlab/danger/teammate' -describe Gitlab::Danger::Teammate do +RSpec.describe Gitlab::Danger::Teammate do + using RSpec::Parameterized::TableSyntax + subject { described_class.new(options.stringify_keys) } - let(:options) { { username: 'luigi', projects: projects, role: role } } + let(:tz_offset_hours) { 2.0 } + let(:options) do + { + username: 'luigi', + projects: projects, + role: role, + markdown_name: '[Luigi](https://gitlab.com/luigi) (`@luigi`)', + tz_offset_hours: tz_offset_hours + } + end + let(:capabilities) { ['reviewer backend'] } let(:projects) { { project => capabilities } } let(:role) { 'Engineer, Manage' } let(:labels) { [] } @@ -115,78 +128,72 @@ describe Gitlab::Danger::Teammate do end end - describe '#status' do - let(:capabilities) { ['dish washing'] } - - context 'with empty cache' do - context 'for successful request' do - it 'returns the response' do - mock_status = double(does_not: 'matter') - expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json) - .and_return(mock_status) + describe '#local_hour' do + around do |example| + Timecop.freeze(Time.utc(2020, 6, 23, 10)) { example.run } + end - expect(subject.status).to be mock_status - end + context 'when author is given' do + where(:tz_offset_hours, :expected_local_hour) do + -12 | 22 + -10 | 0 + 2 | 12 + 4 | 14 + 12 | 22 end - context 'for failing request' do - it 'returns nil' do - expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json) - .and_raise(Gitlab::Danger::RequestHelper::HTTPError.new) - - expect(subject.status).to be nil + with_them do + it 'returns the correct local_hour' do + expect(subject.local_hour).to eq(expected_local_hour) end end end + end - context 'with filled cache' do - it 'returns the cached response' do - mock_status = double(does_not: 'matter') - expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json) - .and_return(mock_status) - subject.status - - expect(Gitlab::Danger::RequestHelper).not_to receive(:http_get_json) - expect(subject.status).to be mock_status + describe '#markdown_name' do + context 'when timezone_experiment == false' do + it 'returns markdown name as-is' do + expect(subject.markdown_name).to eq(options[:markdown_name]) + expect(subject.markdown_name(timezone_experiment: false)).to eq(options[:markdown_name]) end end - end - describe '#available?' do - using RSpec::Parameterized::TableSyntax - - let(:capabilities) { ['dry head'] } - - where(:status, :result) do - {} | true - { message: 'dear reader' } | true - { message: 'OOO: massage' } | false - { message: 'love it SOOO much' } | false - { emoji: 'red_circle' } | false - { emoji: 'palm_tree' } | false - { emoji: 'beach' } | false - { emoji: 'beach_umbrella' } | false - { emoji: 'beach_with_umbrella' } | false - { emoji: nil } | true - { emoji: '' } | true - { emoji: 'dancer' } | true - end + context 'when timezone_experiment == true' do + it 'returns markdown name with timezone info' do + expect(subject.markdown_name(timezone_experiment: true)).to eq("#{options[:markdown_name]} (UTC+2)") + end + + context 'when offset is 1.5' do + let(:tz_offset_hours) { 1.5 } - with_them do - before do - expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json) - .and_return(status&.stringify_keys) + it 'returns markdown name with timezone info, not truncated' do + expect(subject.markdown_name(timezone_experiment: true)).to eq("#{options[:markdown_name]} (UTC+1.5)") + end end - it { expect(subject.available?).to be result } - end + context 'when author is given' do + where(:tz_offset_hours, :author_offset, :diff_text) do + -12 | -10 | "2 hours behind `@mario`" + -10 | -12 | "2 hours ahead `@mario`" + -10 | 2 | "12 hours behind `@mario`" + 2 | 4 | "2 hours behind `@mario`" + 4 | 2 | "2 hours ahead `@mario`" + 2 | 3 | "1 hour behind `@mario`" + 3 | 2 | "1 hour ahead `@mario`" + 2 | 2 | "same timezone as `@mario`" + end - it 'returns true if request fails' do - expect(Gitlab::Danger::RequestHelper) - .to receive(:http_get_json) - .and_raise(Gitlab::Danger::RequestHelper::HTTPError.new) + with_them do + it 'returns markdown name with timezone info' do + author = described_class.new(options.merge(username: 'mario', tz_offset_hours: author_offset).stringify_keys) - expect(subject.available?).to be true + floored_offset_hours = subject.__send__(:floored_offset_hours) + utc_offset = floored_offset_hours >= 0 ? "+#{floored_offset_hours}" : floored_offset_hours + + expect(subject.markdown_name(timezone_experiment: true, author: author)).to eq("#{options[:markdown_name]} (UTC#{utc_offset}, #{diff_text})") + end + end + end end end end |