public_menu_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Admin Knowledge Base Public Menu', authenticated_as: :current_user, type: :request do
  4. let(:url) { "/api/v1/knowledge_bases/manage/#{knowledge_base.id}/update_menu_items" }
  5. let(:params) do
  6. {
  7. menu_items_sets: [{
  8. kb_locale_id: kb_locale.id,
  9. location: location,
  10. menu_items: menu_items
  11. }]
  12. }
  13. end
  14. let(:current_user) { create(:admin) }
  15. let(:menu_item) { create(:knowledge_base_menu_item) }
  16. let(:kb_locale) { menu_item.kb_locale }
  17. let(:knowledge_base) { kb_locale.knowledge_base }
  18. let(:location) { 'header' }
  19. it 'edit title' do
  20. attrs = to_params(menu_item)
  21. attrs[:title] = 'new title'
  22. params = build_params([attrs])
  23. expect { make_request(params) }.to change { menu_item.reload.title }.to 'new title'
  24. end
  25. it 'delete item' do
  26. attrs = to_params(menu_item)
  27. attrs[:_destroy] = true
  28. params = build_params([attrs])
  29. expect { make_request(params) }.to change(KnowledgeBase::MenuItem, :count).by(-1)
  30. end
  31. it 'add item' do
  32. new_item = {
  33. title: 'new item',
  34. new_tab: false,
  35. url: '/new_url'
  36. }
  37. params = build_params([to_params(menu_item), new_item])
  38. expect { make_request(params) }.to change(KnowledgeBase::MenuItem, :count).by(1)
  39. end
  40. def to_params(item)
  41. item.slice :id, :title, :url, :new_tab
  42. end
  43. def make_request(params)
  44. patch url, params: params, as: :json
  45. end
  46. def build_params(menu_items)
  47. {
  48. menu_items_sets: [{
  49. kb_locale_id: kb_locale.id,
  50. location: location,
  51. menu_items: menu_items
  52. }]
  53. }
  54. end
  55. end