issuable_spec.rb 6.4 KB
Newer Older
1 2
require 'spec_helper'

D
Dmitriy Zaporozhets 已提交
3
describe Issue, "Issuable" do
4
  let(:issue) { create(:issue) }
5
  let(:user) { create(:user) }
6 7

  describe "Associations" do
8 9 10 11
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:author) }
    it { is_expected.to belong_to(:assignee) }
    it { is_expected.to have_many(:notes).dependent(:destroy) }
12
    it { is_expected.to have_many(:todos).dependent(:destroy) }
13 14 15
  end

  describe "Validation" do
16 17 18 19
    before do
      allow(subject).to receive(:set_iid).and_return(false)
    end

20 21 22 23
    it { is_expected.to validate_presence_of(:project) }
    it { is_expected.to validate_presence_of(:iid) }
    it { is_expected.to validate_presence_of(:author) }
    it { is_expected.to validate_presence_of(:title) }
24
    it { is_expected.to validate_length_of(:title).is_at_least(0).is_at_most(255) }
25 26 27
  end

  describe "Scope" do
28 29 30
    it { expect(described_class).to respond_to(:opened) }
    it { expect(described_class).to respond_to(:closed) }
    it { expect(described_class).to respond_to(:assigned) }
31 32 33 34 35
  end

  describe ".search" do
    let!(:searchable_issue) { create(:issue, title: "Searchable issue") }

Y
Yorick Peterse 已提交
36
    it 'returns notes with a matching title' do
37 38 39 40 41
      expect(described_class.search(searchable_issue.title)).
        to eq([searchable_issue])
    end

    it 'returns notes with a partially matching title' do
42
      expect(described_class.search('able')).to eq([searchable_issue])
43
    end
44 45 46 47 48 49 50 51 52 53 54 55

    it 'returns notes with a matching title regardless of the casing' do
      expect(described_class.search(searchable_issue.title.upcase)).
        to eq([searchable_issue])
    end
  end

  describe ".full_search" do
    let!(:searchable_issue) do
      create(:issue, title: "Searchable issue", description: 'kittens')
    end

Y
Yorick Peterse 已提交
56
    it 'returns notes with a matching title' do
57 58 59 60 61 62 63 64 65 66 67 68 69
      expect(described_class.full_search(searchable_issue.title)).
        to eq([searchable_issue])
    end

    it 'returns notes with a partially matching title' do
      expect(described_class.full_search('able')).to eq([searchable_issue])
    end

    it 'returns notes with a matching title regardless of the casing' do
      expect(described_class.full_search(searchable_issue.title.upcase)).
        to eq([searchable_issue])
    end

Y
Yorick Peterse 已提交
70
    it 'returns notes with a matching description' do
71 72 73 74 75 76 77 78 79 80 81 82 83
      expect(described_class.full_search(searchable_issue.description)).
        to eq([searchable_issue])
    end

    it 'returns notes with a partially matching description' do
      expect(described_class.full_search(searchable_issue.description)).
        to eq([searchable_issue])
    end

    it 'returns notes with a matching description regardless of the casing' do
      expect(described_class.full_search(searchable_issue.description.upcase)).
        to eq([searchable_issue])
    end
84 85 86 87 88
  end

  describe "#today?" do
    it "returns true when created today" do
      # Avoid timezone differences and just return exactly what we want
89 90
      allow(Date).to receive(:today).and_return(issue.created_at.to_date)
      expect(issue.today?).to be_truthy
91 92 93
    end

    it "returns false when not created today" do
94 95
      allow(Date).to receive(:today).and_return(Date.yesterday)
      expect(issue.today?).to be_falsey
96 97 98 99 100
    end
  end

  describe "#new?" do
    it "returns true when created today and record hasn't been updated" do
101 102
      allow(issue).to receive(:today?).and_return(true)
      expect(issue.new?).to be_truthy
103 104 105
    end

    it "returns false when not created today" do
106 107
      allow(issue).to receive(:today?).and_return(false)
      expect(issue.new?).to be_falsey
108 109 110
    end

    it "returns false when record has been updated" do
111
      allow(issue).to receive(:today?).and_return(true)
112
      issue.touch
113
      expect(issue.new?).to be_falsey
114 115
    end
  end
116

117 118 119 120 121 122 123 124 125 126 127 128 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
  describe '#subscribed?' do
    context 'user is not a participant in the issue' do
      before { allow(issue).to receive(:participants).with(user).and_return([]) }

      it 'returns false when no subcription exists' do
        expect(issue.subscribed?(user)).to be_falsey
      end

      it 'returns true when a subcription exists and subscribed is true' do
        issue.subscriptions.create(user: user, subscribed: true)

        expect(issue.subscribed?(user)).to be_truthy
      end

      it 'returns false when a subcription exists and subscribed is false' do
        issue.subscriptions.create(user: user, subscribed: false)

        expect(issue.subscribed?(user)).to be_falsey
      end
    end

    context 'user is a participant in the issue' do
      before { allow(issue).to receive(:participants).with(user).and_return([user]) }

      it 'returns false when no subcription exists' do
        expect(issue.subscribed?(user)).to be_truthy
      end

      it 'returns true when a subcription exists and subscribed is true' do
        issue.subscriptions.create(user: user, subscribed: true)

        expect(issue.subscribed?(user)).to be_truthy
      end

      it 'returns false when a subcription exists and subscribed is false' do
        issue.subscriptions.create(user: user, subscribed: false)

        expect(issue.subscribed?(user)).to be_falsey
      end
    end
  end

159
  describe "#to_hook_data" do
160 161 162
    let(:data) { issue.to_hook_data(user) }
    let(:project) { issue.project }

163 164

    it "returns correct hook data" do
165 166 167 168
      expect(data[:object_kind]).to eq("issue")
      expect(data[:user]).to eq(user.hook_attrs)
      expect(data[:object_attributes]).to eq(issue.hook_attrs)
      expect(data).to_not have_key(:assignee)
169 170 171 172 173 174
    end

    context "issue is assigned" do
      before { issue.update_attribute(:assignee, user) }

      it "returns correct hook data" do
175 176
        expect(data[:object_attributes]['assignee_id']).to eq(user.id)
        expect(data[:assignee]).to eq(user.hook_attrs)
177
      end
178
    end
179 180 181

    include_examples 'project hook data'
    include_examples 'deprecated repository hook data'
182
  end
183 184 185 186 187 188 189

  describe '#card_attributes' do
    it 'includes the author name' do
      allow(issue).to receive(:author).and_return(double(name: 'Robert'))
      allow(issue).to receive(:assignee).and_return(nil)

      expect(issue.card_attributes).
D
Douwe Maan 已提交
190
        to eq({ 'Author' => 'Robert', 'Assignee' => nil })
191 192 193 194 195 196 197
    end

    it 'includes the assignee name' do
      allow(issue).to receive(:author).and_return(double(name: 'Robert'))
      allow(issue).to receive(:assignee).and_return(double(name: 'Douwe'))

      expect(issue.card_attributes).
D
Douwe Maan 已提交
198
        to eq({ 'Author' => 'Robert', 'Assignee' => 'Douwe' })
199 200
    end
  end
201
end