BigW Consortium Gitlab

todo_spec.rb 3.35 KB
Newer Older
1 2
require 'spec_helper'

3
describe Todo, models: true do
4 5 6 7
  let(:project) { create(:project) }
  let(:commit) { project.commit }
  let(:issue) { create(:issue) }

8 9
  describe 'relationships' do
    it { is_expected.to belong_to(:author).class_name("User") }
10
    it { is_expected.to belong_to(:note) }
11 12 13 14 15
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

16 17 18 19 20
  describe 'respond to' do
    it { is_expected.to respond_to(:author_name) }
    it { is_expected.to respond_to(:author_email) }
  end

21 22
  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
23
    it { is_expected.to validate_presence_of(:target_type) }
24
    it { is_expected.to validate_presence_of(:user) }
25 26 27 28 29 30 31 32 33 34 35 36 37 38

    context 'for commits' do
      subject { described_class.new(target_type: 'Commit') }

      it { is_expected.to validate_presence_of(:commit_id) }
      it { is_expected.not_to validate_presence_of(:target_id) }
    end

    context 'for issuables' do
      subject { described_class.new(target: issue) }

      it { is_expected.to validate_presence_of(:target_id) }
      it { is_expected.not_to validate_presence_of(:commit_id) }
    end
39
  end
40

41
  describe '#body' do
42
    before do
43
      subject.target = build(:issue, title: 'Bugfix')
44 45
    end

46
    it 'returns target title when note is blank' do
47 48
      subject.note = nil

49
      expect(subject.body).to eq 'Bugfix'
50 51 52 53 54
    end

    it 'returns note when note is present' do
      subject.note = build(:note, note: 'quick fix')

55
      expect(subject.body).to eq 'quick fix'
56 57 58
    end
  end

59
  describe '#done' do
60 61
    it 'changes state to done' do
      todo = create(:todo, state: :pending)
62
      expect { todo.done }.to change(todo, :state).from('pending').to('done')
63 64
    end

65 66
    it 'does not raise error when is already done' do
      todo = create(:todo, state: :done)
67
      expect { todo.done }.not_to raise_error
68
    end
69
  end
70 71 72 73 74 75 76 77 78 79 80 81 82 83

  describe '#for_commit?' do
    it 'returns true when target is a commit' do
      subject.target_type = 'Commit'
      expect(subject.for_commit?).to eq true
    end

    it 'returns false when target is an issuable' do
      subject.target_type = 'Issue'
      expect(subject.for_commit?).to eq false
    end
  end

  describe '#target' do
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    context 'for commits' do
      it 'returns an instance of Commit when exists' do
        subject.project = project
        subject.target_type = 'Commit'
        subject.commit_id = commit.id

        expect(subject.target).to be_a(Commit)
        expect(subject.target).to eq commit
      end

      it 'returns nil when does not exists' do
        subject.project = project
        subject.target_type = 'Commit'
        subject.commit_id = 'xxxx'

        expect(subject.target).to be_nil
      end
101 102 103 104 105 106 107 108 109
    end

    it 'returns the issuable for issuables' do
      subject.target_id = issue.id
      subject.target_type = issue.class.name
      expect(subject.target).to eq issue
    end
  end

110
  describe '#target_reference' do
111
    it 'returns the short commit id for commits' do
112
      subject.project = project
113 114
      subject.target_type = 'Commit'
      subject.commit_id = commit.id
115

116
      expect(subject.target_reference).to eq commit.short_id
117 118 119 120
    end

    it 'returns reference for issuables' do
      subject.target = issue
121
      expect(subject.target_reference).to eq issue.to_reference
122 123
    end
  end
124
end