2014-07-29 10:10:15 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe Label, models: true do
|
2014-07-29 10:10:15 -04:00
|
|
|
let(:label) { create(:label) }
|
|
|
|
|
2015-05-02 23:11:21 -04:00
|
|
|
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) }
|
2014-08-20 06:31:16 -04:00
|
|
|
|
|
|
|
it 'should validate color code' do
|
2015-05-02 23:11:21 -04:00
|
|
|
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)
|
2014-08-20 06:31:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should validate title' do
|
2015-05-02 23:11:21 -04:00
|
|
|
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)
|
2016-06-16 19:09:13 -04:00
|
|
|
expect(label).to allow_value('G?ITLAB').for(:title)
|
|
|
|
expect(label).to allow_value('G&ITLAB').for(:title)
|
2015-05-02 23:11:21 -04:00
|
|
|
expect(label).to allow_value("customer's request").for(:title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-16 19:09:13 -04:00
|
|
|
describe '#title' do
|
|
|
|
it 'sanitizes title' do
|
|
|
|
label = described_class.new(title: '<b>foo & bar?</b>')
|
|
|
|
expect(label.title).to eq('foo & bar?')
|
2016-05-04 17:21:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-02 23:11:21 -04:00
|
|
|
describe '#to_reference' do
|
2015-05-15 16:07:25 -04:00
|
|
|
context 'using id' do
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(label.to_reference).to eq "~#{label.id}"
|
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
2014-08-20 06:31:16 -04:00
|
|
|
|
2015-05-15 16:07:25 -04:00
|
|
|
context 'using name' do
|
|
|
|
it 'returns a String reference to the object' do
|
2016-02-25 02:37:49 -05:00
|
|
|
expect(label.to_reference(format: :name)).to eq %(~"#{label.name}")
|
2015-05-15 16:07:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses id when name contains double quote' do
|
|
|
|
label = create(:label, name: %q{"irony"})
|
2016-02-25 02:37:49 -05:00
|
|
|
expect(label.to_reference(format: :name)).to eq "~#{label.id}"
|
2015-05-15 16:07:25 -04:00
|
|
|
end
|
2014-08-20 06:31:16 -04:00
|
|
|
end
|
2016-02-25 04:14:12 -05:00
|
|
|
|
|
|
|
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
|
2014-08-20 06:31:16 -04:00
|
|
|
end
|
2014-07-29 10:10:15 -04:00
|
|
|
end
|