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 10 11 12 13
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

14 15 16 17 18
  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 已提交
19 20
  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
21
    it { is_expected.to validate_presence_of(:target_type) }
D
Douglas Barbosa Alexandre 已提交
22
    it { is_expected.to validate_presence_of(:user) }
23
    it { is_expected.to validate_presence_of(:author) }
24 25 26 27 28 29 30 31 32 33 34 35 36 37

    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 已提交
38
  end
39

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

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

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

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

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

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

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

  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
83
    context 'for commits' do
84 85 86
      let(:project) { create(:project, :repository) }
      let(:commit) { project.commit }

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

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

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

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

124
    it 'returns full reference for issuables' do
125
      subject.target = issue
126
      expect(subject.target_reference).to eq issue.to_reference(full: true)
127 128
    end
  end
129 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

  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 已提交
175
end