gitlab-org--gitlab-foss/spec/models/todo_spec.rb

143 lines
3.8 KiB
Ruby
Raw Normal View History

2016-02-12 08:17:42 -05:00
# == Schema Information
#
2016-02-20 08:59:59 -05:00
# Table name: todos
2016-02-12 08:17:42 -05:00
#
# id :integer not null, primary key
# user_id :integer not null
# project_id :integer not null
# target_id :integer
2016-02-12 08:17:42 -05:00
# target_type :string not null
# author_id :integer
2016-02-18 08:51:53 -05:00
# action :integer not null
2016-02-12 08:17:42 -05:00
# state :string not null
# created_at :datetime
# updated_at :datetime
# note_id :integer
# commit_id :string
2016-02-12 08:17:42 -05:00
#
require 'spec_helper'
2016-02-20 08:59:59 -05:00
describe Todo, models: true do
let(:project) { create(:project) }
let(:commit) { project.commit }
let(:issue) { create(:issue) }
2016-02-12 08:17:42 -05:00
describe 'relationships' do
it { is_expected.to belong_to(:author).class_name("User") }
it { is_expected.to belong_to(:note) }
2016-02-12 08:17:42 -05:00
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:target).touch(true) }
it { is_expected.to belong_to(:user) }
end
2016-02-12 13:45:44 -05:00
describe 'respond to' do
it { is_expected.to respond_to(:author_name) }
it { is_expected.to respond_to(:author_email) }
end
2016-02-12 08:17:42 -05:00
describe 'validations' do
it { is_expected.to validate_presence_of(:action) }
it { is_expected.to validate_presence_of(:target_type) }
2016-02-12 08:17:42 -05:00
it { is_expected.to validate_presence_of(:user) }
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
2016-02-12 08:17:42 -05:00
end
2016-02-12 13:45:44 -05:00
2016-02-18 14:16:39 -05:00
describe '#body' do
before do
2016-02-18 14:16:39 -05:00
subject.target = build(:issue, title: 'Bugfix')
end
2016-02-18 14:16:39 -05:00
it 'returns target title when note is blank' do
subject.note = nil
2016-02-18 14:16:39 -05:00
expect(subject.body).to eq 'Bugfix'
end
it 'returns note when note is present' do
subject.note = build(:note, note: 'quick fix')
2016-02-18 14:16:39 -05:00
expect(subject.body).to eq 'quick fix'
end
end
describe '#done' do
it 'changes state to done' do
todo = create(:todo, state: :pending)
expect { todo.done }.to change(todo, :state).from('pending').to('done')
end
it 'does not raise error when is already done' do
todo = create(:todo, state: :done)
expect { todo.done }.not_to raise_error
end
2016-02-12 13:45:44 -05:00
end
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
2016-03-18 12:27:27 -04:00
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
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
describe '#target_reference' do
it 'returns the short commit id for commits' do
subject.project = project
subject.target_type = 'Commit'
subject.commit_id = commit.id
expect(subject.target_reference).to eq commit.short_id
end
it 'returns reference for issuables' do
subject.target = issue
expect(subject.target_reference).to eq issue.to_reference
end
end
2016-02-12 08:17:42 -05:00
end