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

3
describe Issue, "Issuable" do
4 5 6
  let(:issue) { create(:issue) }

  describe "Associations" do
7 8 9 10
    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) }
11 12 13
  end

  describe "Validation" do
14
    before { subject.stub(set_iid: false) }
15 16 17 18 19
    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) }
    it { is_expected.to ensure_length_of(:title).is_at_least(0).is_at_most(255) }
20 21 22
  end

  describe "Scope" do
23 24 25
    it { expect(described_class).to respond_to(:opened) }
    it { expect(described_class).to respond_to(:closed) }
    it { expect(described_class).to respond_to(:assigned) }
26 27 28 29 30 31
  end

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

    it "matches by title" do
32
      expect(described_class.search('able')).to eq([searchable_issue])
33 34 35 36 37 38
    end
  end

  describe "#today?" do
    it "returns true when created today" do
      # Avoid timezone differences and just return exactly what we want
39 40
      allow(Date).to receive(:today).and_return(issue.created_at.to_date)
      expect(issue.today?).to be_truthy
41 42 43
    end

    it "returns false when not created today" do
44 45
      allow(Date).to receive(:today).and_return(Date.yesterday)
      expect(issue.today?).to be_falsey
46 47 48 49 50
    end
  end

  describe "#new?" do
    it "returns true when created today and record hasn't been updated" do
51 52
      allow(issue).to receive(:today?).and_return(true)
      expect(issue.new?).to be_truthy
53 54 55
    end

    it "returns false when not created today" do
56 57
      allow(issue).to receive(:today?).and_return(false)
      expect(issue.new?).to be_falsey
58 59 60
    end

    it "returns false when record has been updated" do
61
      allow(issue).to receive(:today?).and_return(true)
62
      issue.touch
63
      expect(issue.new?).to be_falsey
64 65 66
    end
  end
end