trabajo-afectivo/spec/models/knowledge_base/menu_item_spec.rb

70 lines
2 KiB
Ruby
Raw Normal View History

2019-06-04 03:40:48 +00:00
require 'rails_helper'
require 'models/contexts/factory_context'
RSpec.describe KnowledgeBase::MenuItem, type: :model do
subject(:kb_menu_item) { create(:knowledge_base_menu_item) }
include_context 'factory'
context 'item' do
it { is_expected.to validate_presence_of :title }
it { is_expected.to validate_presence_of :url }
it { is_expected.to validate_presence_of :location }
it { is_expected.to validate_inclusion_of(:location).in_array(%w[header footer]) }
end
context 'has scopes for' do
let(:kb_locale) { kb_menu_item.kb_locale }
let(:scope) { described_class.where(kb_locale: kb_locale) }
2019-06-04 03:40:48 +00:00
let!(:header) { create(:knowledge_base_menu_item, :for_header, kb_locale: kb_locale) }
let!(:footer) { create(:knowledge_base_menu_item, :for_footer, kb_locale: kb_locale) }
it 'header' do
expect(scope.location_header).to match [kb_menu_item, header]
2019-06-04 03:40:48 +00:00
end
it 'footer' do
expect(scope.location_footer).to match [footer]
2019-06-04 03:40:48 +00:00
end
end
context 'when url' do
context 'without prefix is added' do
before { kb_menu_item.update(url: Faker::Internet.domain_name) }
2019-06-04 03:40:48 +00:00
it 'is saved' do
expect(kb_menu_item).not_to be_changed
end
2019-06-04 03:40:48 +00:00
it 'prefix is added to hostname' do
expect(kb_menu_item.url).to start_with 'http://'
end
2019-06-04 03:40:48 +00:00
end
context 'with custom prefix is added' do
before { kb_menu_item.update(url: "scheme://#{Faker::Internet.domain_name}") }
it 'is saved' do
expect(kb_menu_item).not_to be_changed
end
2019-06-04 03:40:48 +00:00
it 'given scheme is not touched' do
expect(kb_menu_item.url).to start_with 'scheme://'
end
2019-06-04 03:40:48 +00:00
end
context 'is relative and protocol prefix is not added' do
before { kb_menu_item.update(url: '/loremipsum') }
it 'is saved' do
expect(kb_menu_item).not_to be_changed
end
it 'path is not modified' do
expect(kb_menu_item.url).not_to start_with 'http://'
end
2019-06-04 03:40:48 +00:00
end
end
end