category_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 'fetches all parents' do
  32. expect(grandchild_category.self_with_parents.count).to eq 3
  33. end
  34. it 'root category has no parent' do
  35. expect(kb_category_with_tree.parent).to be_blank
  36. end
  37. it 'children category has to have a parent' do
  38. expect(child_category.parent).to be_present
  39. end
  40. context 'when fetching self with children' do
  41. it 'root category has multiple layers children and matches all KB categories' do
  42. expect(kb_category_with_tree.self_with_children).to match_array(knowledge_base.categories)
  43. end
  44. it 'child category has multiple layers of children' do
  45. expect(child_category.self_with_children.count).to eq 5
  46. end
  47. it 'grandchild category has single layer of children' do
  48. expect(grandchild_category.self_with_children.count).to eq 3
  49. end
  50. end
  51. context 'when fetchching self with children ids' do
  52. it 'root category has multiple layers children ids' do
  53. expect(kb_category_with_tree.self_with_children_ids).to match_array(knowledge_base.category_ids)
  54. end
  55. it 'child category has with multiple layers of children ids' do
  56. expect(child_category.self_with_children_ids.count).to eq 5
  57. end
  58. it 'grandchild category has single layer of children ids count' do
  59. expect(grandchild_category.self_with_children_ids.count).to eq 3
  60. end
  61. it 'grandchild category children ids matches direct children ids' do
  62. expect(grandchild_category.self_with_children_ids).to match_array([grandchild_category.id] + grandchild_category.child_ids)
  63. end
  64. end
  65. context 'when checking if item is a parent of' do
  66. it 'root category is indirect (and direct) parent of child' do
  67. expect(child_category).to be_self_parent(kb_category_with_tree)
  68. end
  69. it 'root category is indirect parent of grandchild' do
  70. expect(grandchild_category).to be_self_parent(kb_category_with_tree)
  71. end
  72. it 'child category is not a parent of root category' do
  73. expect(kb_category_with_tree).not_to be_self_parent(grandchild_category)
  74. end
  75. end
  76. end
  77. describe '#public_content?' do
  78. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  79. it "returns #{is_visible} when contains #{state} answer" do
  80. object = create(:knowledge_base_category, "containing_#{state}")
  81. expect(object).send is_visible ? :to : :not_to, be_public_content(object.translations.first.kb_locale)
  82. end
  83. end
  84. include_examples 'verify visibility in given state', state: :published, is_visible: true
  85. include_examples 'verify visibility in given state', state: :internal, is_visible: false
  86. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  87. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  88. end
  89. describe '#internal_content?' do
  90. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  91. it "returns #{is_visible} when contains #{state} answer" do
  92. object = create(:knowledge_base_category, "containing_#{state}")
  93. expect(object).send is_visible ? :to : :not_to, be_internal_content(object.translations.first.kb_locale)
  94. end
  95. end
  96. include_examples 'verify visibility in given state', state: :published, is_visible: true
  97. include_examples 'verify visibility in given state', state: :internal, is_visible: true
  98. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  99. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  100. end
  101. end