category_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'rails_helper'
  2. require 'models/concerns/checks_kb_client_notification_examples'
  3. require 'models/contexts/factory_context'
  4. RSpec.describe KnowledgeBase::Category, type: :model, current_user_id: 1 do
  5. subject(:kb_category) { create(:knowledge_base_category) }
  6. include_context 'factory'
  7. it_behaves_like 'ChecksKbClientNotification'
  8. it { is_expected.to validate_presence_of(:category_icon) }
  9. it { is_expected.not_to validate_presence_of(:parent_id) }
  10. it { is_expected.to have_many(:answers) }
  11. it { is_expected.to have_many(:children) }
  12. it { is_expected.to belong_to(:parent).optional }
  13. it { is_expected.to belong_to(:knowledge_base) }
  14. context 'in multilevel tree' do
  15. subject(:kb_category_with_tree) { create(:kb_category_with_tree) }
  16. let(:knowledge_base) { kb_category_with_tree.knowledge_base }
  17. let(:child_category) { kb_category_with_tree.children.order(position: :asc).last }
  18. let(:grandchild_category) { child_category.children.order(position: :asc).first }
  19. it 'tests to fetch all categories in KB' do
  20. expect(knowledge_base.categories.count).to eq(7)
  21. end
  22. it 'fetches root categories' do
  23. expect(knowledge_base.categories.root).to contain_exactly(kb_category_with_tree)
  24. end
  25. it 'fetches direct children' do
  26. expect(kb_category_with_tree.children.count).to eq 2
  27. end
  28. it 'fetches all children' do
  29. expect(kb_category_with_tree.self_with_children.count).to eq 7
  30. end
  31. it 'root category has no parent' do
  32. expect(kb_category_with_tree.parent).to be_blank
  33. end
  34. it 'children category has to have a parent' do
  35. expect(child_category.parent).to be_present
  36. end
  37. context 'when fetching self with children' do
  38. it 'root category has multiple layers children and matches all KB categories' do
  39. expect(kb_category_with_tree.self_with_children).to match_array(knowledge_base.categories)
  40. end
  41. it 'child category has multiple layers of children' do
  42. expect(child_category.self_with_children.count).to eq 5
  43. end
  44. it 'grandchild category has single layer of children' do
  45. expect(grandchild_category.self_with_children.count).to eq 3
  46. end
  47. end
  48. context 'when fetchching self with children ids' do
  49. it 'root category has multiple layers children ids' do
  50. expect(kb_category_with_tree.self_with_children_ids).to match_array(knowledge_base.category_ids)
  51. end
  52. it 'child category has with multiple layers of children ids' do
  53. expect(child_category.self_with_children_ids.count).to eq 5
  54. end
  55. it 'grandchild category has single layer of children ids count' do
  56. expect(grandchild_category.self_with_children_ids.count).to eq 3
  57. end
  58. it 'grandchild category children ids matches direct children ids' do
  59. expect(grandchild_category.self_with_children_ids).to match_array([grandchild_category.id] + grandchild_category.child_ids)
  60. end
  61. end
  62. context 'when checking if item is a parent of' do
  63. it 'root category is indirect (and direct) parent of child' do
  64. expect(child_category).to be_self_parent(kb_category_with_tree)
  65. end
  66. it 'root category is indirect parent of grandchild' do
  67. expect(grandchild_category).to be_self_parent(kb_category_with_tree)
  68. end
  69. it 'child category is not a parent of root category' do
  70. expect(kb_category_with_tree).not_to be_self_parent(grandchild_category)
  71. end
  72. end
  73. end
  74. describe '#public_content?' do
  75. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  76. it "returns #{is_visible} when contains #{state} answer" do
  77. object = create(:knowledge_base_category, "containing_#{state}")
  78. expect(object).send is_visible ? :to : :not_to, be_public_content(object.translations.first.kb_locale)
  79. end
  80. end
  81. include_examples 'verify visibility in given state', state: :published, is_visible: true
  82. include_examples 'verify visibility in given state', state: :internal, is_visible: false
  83. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  84. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  85. end
  86. describe '#internal_content?' do
  87. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  88. it "returns #{is_visible} when contains #{state} answer" do
  89. object = create(:knowledge_base_category, "containing_#{state}")
  90. expect(object).send is_visible ? :to : :not_to, be_internal_content(object.translations.first.kb_locale)
  91. end
  92. end
  93. include_examples 'verify visibility in given state', state: :published, is_visible: true
  94. include_examples 'verify visibility in given state', state: :internal, is_visible: true
  95. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  96. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  97. end
  98. end