task_spec.rb 2.41 KB
Newer Older
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
1 2 3 4 5 6 7 8 9 10
# == Schema Information
#
# Table name: tasks
#
#  id          :integer          not null, primary key
#  user_id     :integer          not null
#  project_id  :integer          not null
#  target_id   :integer          not null
#  target_type :string           not null
#  author_id   :integer
11
#  note_id     :integer
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
12 13 14 15 16 17 18 19 20 21 22
#  action      :integer
#  state       :string           not null
#  created_at  :datetime
#  updated_at  :datetime
#

require 'spec_helper'

describe Task, models: true do
  describe 'relationships' do
    it { is_expected.to belong_to(:author).class_name("User") }
23
    it { is_expected.to belong_to(:note) }
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
24 25 26 27 28
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

29 30 31 32 33
  describe 'respond to' do
    it { is_expected.to respond_to(:author_name) }
    it { is_expected.to respond_to(:author_email) }
  end

Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
34 35 36 37 38
  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
    it { is_expected.to validate_presence_of(:target) }
    it { is_expected.to validate_presence_of(:user) }
  end
39 40 41 42 43 44 45 46 47 48

  describe '#action_name' do
    it 'returns assigned when action is assigned' do
      subject.action = Task::ASSIGNED

      expect(subject.action_name).to eq 'assigned'
    end
  end

  describe '#body?' do
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    let(:issue) { build(:issue) }

    before do
      subject.target = issue
    end

    it 'returns true when target respond to title' do
      expect(subject.body?).to eq true
    end

    it 'returns false when target does not respond to title' do
      allow(issue).to receive(:respond_to?).with(:title).and_return(false)

      expect(subject.body?).to eq false
    end
64 65
  end

66 67 68 69 70 71 72 73 74 75 76 77 78 79
  describe '#note_text' do
    it 'returns nil when note is blank' do
      subject.note = nil

      expect(subject.note_text).to be_nil
    end

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

      expect(subject.note_text).to eq 'quick fix'
    end
  end

80
  describe '#target_iid' do
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    let(:issue) { build(:issue, id: 1, iid: 5) }

    before do
      subject.target = issue
    end

    it 'returns target.iid when target respond to iid' do
      expect(subject.target_iid).to eq 5
    end

    it 'returns target_id when target does not respond to iid' do
      allow(issue).to receive(:respond_to?).with(:iid).and_return(false)

      expect(subject.target_iid).to eq 1
    end
96
  end
Douglas Barbosa Alexandre's avatar
Douglas Barbosa Alexandre committed
97
end