diff options
Diffstat (limited to 'spec/models/time_tracking/timelog_category_spec.rb')
-rw-r--r-- | spec/models/time_tracking/timelog_category_spec.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/spec/models/time_tracking/timelog_category_spec.rb b/spec/models/time_tracking/timelog_category_spec.rb new file mode 100644 index 00000000000..d8b938e9d68 --- /dev/null +++ b/spec/models/time_tracking/timelog_category_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe TimeTracking::TimelogCategory, type: :model do + describe 'associations' do + it { is_expected.to belong_to(:namespace).with_foreign_key('namespace_id') } + end + + describe 'validations' do + subject { create(:timelog_category) } + + it { is_expected.to validate_presence_of(:namespace) } + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_uniqueness_of(:name).case_insensitive.scoped_to([:namespace_id]) } + it { is_expected.to validate_length_of(:name).is_at_most(255) } + it { is_expected.to validate_length_of(:description).is_at_most(1024) } + it { is_expected.to validate_length_of(:color).is_at_most(7) } + end + + describe 'validations when billable' do + subject { create(:timelog_category, billable: true, billing_rate: 10.5) } + + it { is_expected.to validate_presence_of(:billing_rate) } + it { is_expected.to validate_numericality_of(:billing_rate).is_greater_than(0) } + end + + describe '#name' do + it 'strips name' do + timelog_category = described_class.new(name: ' TimelogCategoryTest ') + timelog_category.valid? + + expect(timelog_category.name).to eq('TimelogCategoryTest') + end + end + + describe '#color' do + it 'strips color' do + timelog_category = described_class.new(name: 'TimelogCategoryTest', color: ' #fafafa ') + timelog_category.valid? + + expect(timelog_category.color).to eq(::Gitlab::Color.of('#fafafa')) + end + end + + describe '#find_by_name' do + let_it_be(:namespace_a) { create(:namespace) } + let_it_be(:namespace_b) { create(:namespace) } + let_it_be(:timelog_category_a) { create(:timelog_category, namespace: namespace_a, name: 'TimelogCategoryTest') } + + it 'finds the correct timelog category' do + expect(described_class.find_by_name(namespace_a.id, 'TIMELOGCATEGORYTest')).to match_array([timelog_category_a]) + end + + it 'returns empty if not found' do + expect(described_class.find_by_name(namespace_b.id, 'TIMELOGCATEGORYTest')).to be_empty + end + end +end |