menu_item_spec.rb 2.0 KB

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