diff options
Diffstat (limited to 'spec/models/issue_link_spec.rb')
-rw-r--r-- | spec/models/issue_link_spec.rb | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/spec/models/issue_link_spec.rb b/spec/models/issue_link_spec.rb index 433b51b8a70..9f77fcef5da 100644 --- a/spec/models/issue_link_spec.rb +++ b/spec/models/issue_link_spec.rb @@ -3,57 +3,42 @@ require 'spec_helper' RSpec.describe IssueLink do - describe 'Associations' do - it { is_expected.to belong_to(:source).class_name('Issue') } - it { is_expected.to belong_to(:target).class_name('Issue') } + it_behaves_like 'issuable link' do + let_it_be_with_reload(:issuable_link) { create(:issue_link) } + let_it_be(:issuable) { create(:issue) } + let(:issuable_class) { 'Issue' } + let(:issuable_link_factory) { :issue_link } end - describe 'link_type' do - it { is_expected.to define_enum_for(:link_type).with_values(relates_to: 0, blocks: 1) } - - it 'provides the "related" as default link_type' do - expect(create(:issue_link).link_type).to eq 'relates_to' - end + describe '.issuable_type' do + it { expect(described_class.issuable_type).to eq(:issue) } end - describe 'Validation' do - subject { create :issue_link } + describe 'Scopes' do + let_it_be(:issue1) { create(:issue) } + let_it_be(:issue2) { create(:issue) } - it { is_expected.to validate_presence_of(:source) } - it { is_expected.to validate_presence_of(:target) } - it do - is_expected.to validate_uniqueness_of(:source) - .scoped_to(:target_id) - .with_message(/already related/) - end + describe '.for_source_issue' do + it 'includes linked issues for source issue' do + source_issue = create(:issue) + issue_link_1 = create(:issue_link, source: source_issue, target: issue1) + issue_link_2 = create(:issue_link, source: source_issue, target: issue2) - it 'is not valid if an opposite link already exists' do - issue_link = build(:issue_link, source: subject.target, target: subject.source) + result = described_class.for_source_issue(source_issue) - expect(issue_link).to be_invalid - expect(issue_link.errors[:source]).to include('is already related to this issue') + expect(result).to contain_exactly(issue_link_1, issue_link_2) + end end - context 'when it relates to itself' do - let(:issue) { create :issue } - - context 'cannot be validated' do - it 'does not invalidate object with self relation error' do - issue_link = build :issue_link, source: issue, target: nil - - issue_link.valid? - - expect(issue_link.errors[:source]).to be_empty - end - end + describe '.for_target_issue' do + it 'includes linked issues for target issue' do + target_issue = create(:issue) + issue_link_1 = create(:issue_link, source: issue1, target: target_issue) + issue_link_2 = create(:issue_link, source: issue2, target: target_issue) - context 'can be invalidated' do - it 'invalidates object' do - issue_link = build :issue_link, source: issue, target: issue + result = described_class.for_target_issue(target_issue) - expect(issue_link).to be_invalid - expect(issue_link.errors[:source]).to include('cannot be related to itself') - end + expect(result).to contain_exactly(issue_link_1, issue_link_2) end end end |