BigW Consortium Gitlab

label_spec.rb 3.04 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10
# == Schema Information
#
# Table name: labels
#
#  id         :integer          not null, primary key
#  title      :string(255)
#  color      :string(255)
#  project_id :integer
#  created_at :datetime
#  updated_at :datetime
Dmitriy Zaporozhets committed
11
#  template   :boolean          default(FALSE)
Dmitriy Zaporozhets committed
12 13
#

14 15
require 'spec_helper'

Douwe Maan committed
16
describe Label, models: true do
17 18
  let(:label) { create(:label) }

19 20 21 22 23 24 25 26 27 28 29 30 31 32
  describe 'associations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to have_many(:label_links).dependent(:destroy) }
    it { is_expected.to have_many(:issues).through(:label_links).source(:target) }
  end

  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Referable) }
  end

  describe 'validation' do
    it { is_expected.to validate_presence_of(:project) }
33 34

    it 'should validate color code' do
35 36 37 38 39 40 41 42 43
      expect(label).not_to allow_value('G-ITLAB').for(:color)
      expect(label).not_to allow_value('AABBCC').for(:color)
      expect(label).not_to allow_value('#AABBCCEE').for(:color)
      expect(label).not_to allow_value('GGHHII').for(:color)
      expect(label).not_to allow_value('#').for(:color)
      expect(label).not_to allow_value('').for(:color)

      expect(label).to allow_value('#AABBCC').for(:color)
      expect(label).to allow_value('#abcdef').for(:color)
44 45 46
    end

    it 'should validate title' do
47 48 49 50 51 52 53 54 55 56 57 58
      expect(label).not_to allow_value('G,ITLAB').for(:title)
      expect(label).not_to allow_value('G?ITLAB').for(:title)
      expect(label).not_to allow_value('G&ITLAB').for(:title)
      expect(label).not_to allow_value('').for(:title)

      expect(label).to allow_value('GITLAB').for(:title)
      expect(label).to allow_value('gitlab').for(:title)
      expect(label).to allow_value("customer's request").for(:title)
    end
  end

  describe '#to_reference' do
59 60 61 62
    context 'using id' do
      it 'returns a String reference to the object' do
        expect(label.to_reference).to eq "~#{label.id}"
      end
63
    end
64

65 66
    context 'using name' do
      it 'returns a String reference to the object' do
67
        expect(label.to_reference(format: :name)).to eq %(~"#{label.name}")
68 69 70 71
      end

      it 'uses id when name contains double quote' do
        label = create(:label, name: %q{"irony"})
72
        expect(label.to_reference(format: :name)).to eq "~#{label.id}"
73
      end
74
    end
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    context 'using invalid format' do
      it 'raises error' do
        expect { label.to_reference(format: :invalid) }
          .to raise_error StandardError, /Unknown format/
      end
    end

    context 'cross project reference' do
      let(:project) { create(:project) }

      context 'using name' do
        it 'returns cross reference with label name' do
          expect(label.to_reference(project, format: :name))
            .to eq %Q(#{label.project.to_reference}~"#{label.name}")
        end
      end

      context 'using id' do
        it 'returns cross reference with label id' do
          expect(label.to_reference(project, format: :id))
            .to eq %Q(#{label.project.to_reference}~#{label.id})
        end
      end
    end
100
  end
101
end