todo_spec.rb 4.5 KB
Newer Older
D
Douglas Barbosa Alexandre 已提交
1 2
require 'spec_helper'

3
describe Todo do
4 5
  let(:issue) { create(:issue) }

D
Douglas Barbosa Alexandre 已提交
6 7
  describe 'relationships' do
    it { is_expected.to belong_to(:author).class_name("User") }
8
    it { is_expected.to belong_to(:note) }
D
Douglas Barbosa Alexandre 已提交
9
    it { is_expected.to belong_to(:project) }
10
    it { is_expected.to belong_to(:group) }
D
Douglas Barbosa Alexandre 已提交
11 12 13 14
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

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

D
Douglas Barbosa Alexandre 已提交
20 21
  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
22
    it { is_expected.to validate_presence_of(:target_type) }
D
Douglas Barbosa Alexandre 已提交
23
    it { is_expected.to validate_presence_of(:user) }
24
    it { is_expected.to validate_presence_of(:author) }
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
D
Douglas Barbosa Alexandre 已提交
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
    context 'for commits' do
85 86 87
      let(:project) { create(:project, :repository) }
      let(:commit) { project.commit }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      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
104 105 106 107 108 109 110 111 112
    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

113
  describe '#target_reference' do
114
    it 'returns commit full reference with short id' do
115 116 117
      project = create(:project, :repository)
      commit = project.commit

118
      subject.project = project
119 120
      subject.target_type = 'Commit'
      subject.commit_id = commit.id
121

122
      expect(subject.target_reference).to eq commit.reference_link_text(full: true)
123 124
    end

125
    it 'returns full reference for issuables' do
126
      subject.target = issue
127
      expect(subject.target_reference).to eq issue.to_reference(full: true)
128 129
    end
  end
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

  describe '#self_added?' do
    let(:user_1) { build(:user) }

    before do
      subject.user = user_1
    end

    it 'is true when the user is the author' do
      subject.author = user_1

      expect(subject).to be_self_added
    end

    it 'is false when the user is not the author' do
      subject.author = build(:user)

      expect(subject).not_to be_self_added
    end
  end

  describe '#self_assigned?' do
    let(:user_1) { build(:user) }

    before do
      subject.user = user_1
      subject.author = user_1
      subject.action = Todo::ASSIGNED
    end

    it 'is true when todo is ASSIGNED and self_added' do
      expect(subject).to be_self_assigned
    end

    it 'is false when the todo is not ASSIGNED' do
      subject.action = Todo::MENTIONED

      expect(subject).not_to be_self_assigned
    end

    it 'is false when todo is not self_added' do
      subject.author = build(:user)

      expect(subject).not_to be_self_assigned
    end
  end
D
Douglas Barbosa Alexandre 已提交
176
end