menu_item_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rails_helper'
  2. require 'models/contexts/factory_context'
  3. RSpec.describe KnowledgeBase::MenuItem, type: :model do
  4. subject(:kb_menu_item) { create(:knowledge_base_menu_item) }
  5. include_context 'factory'
  6. context 'item' do
  7. it { is_expected.to validate_presence_of :title }
  8. it { is_expected.to validate_presence_of :url }
  9. it { is_expected.to validate_presence_of :location }
  10. it { is_expected.to validate_inclusion_of(:location).in_array(%w[header footer]) }
  11. end
  12. context 'has scopes for' do
  13. let(:kb_locale) { kb_menu_item.kb_locale }
  14. let(:scope) { described_class.where(kb_locale: kb_locale) }
  15. let!(:header) { create(:knowledge_base_menu_item, :for_header, kb_locale: kb_locale) }
  16. let!(:footer) { create(:knowledge_base_menu_item, :for_footer, kb_locale: kb_locale) }
  17. it 'header' do
  18. expect(scope.location_header).to match [kb_menu_item, header]
  19. end
  20. it 'footer' do
  21. expect(scope.location_footer).to match [footer]
  22. end
  23. end
  24. context 'when url' do
  25. context 'without prefix is added' do
  26. before { kb_menu_item.update(url: Faker::Internet.domain_name) }
  27. it 'is saved' do
  28. expect(kb_menu_item).not_to be_changed
  29. end
  30. it 'prefix is added to hostname' do
  31. expect(kb_menu_item.url).to start_with 'http://'
  32. end
  33. end
  34. context 'with custom prefix is added' do
  35. before { kb_menu_item.update(url: "scheme://#{Faker::Internet.domain_name}") }
  36. it 'is saved' do
  37. expect(kb_menu_item).not_to be_changed
  38. end
  39. it 'given scheme is not touched' do
  40. expect(kb_menu_item.url).to start_with 'scheme://'
  41. end
  42. end
  43. context 'is relative and protocol prefix is not added' do
  44. before { kb_menu_item.update(url: '/loremipsum') }
  45. it 'is saved' do
  46. expect(kb_menu_item).not_to be_changed
  47. end
  48. it 'path is not modified' do
  49. expect(kb_menu_item.url).not_to start_with 'http://'
  50. end
  51. end
  52. end
  53. end