has_optional_groups_examples.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'HasOptionalGroups' do |model_factory:|
  3. it_behaves_like 'Association clears cache', association: :groups
  4. describe '.available_in_groups' do
  5. let(:group) { create(:group) }
  6. let(:object) { create(model_factory, groups:) }
  7. before { object }
  8. context 'when object has a group' do
  9. let(:groups) { [group] }
  10. it 'returns object if group matches' do
  11. expect(described_class.available_in_groups([group]))
  12. .to include(object)
  13. end
  14. it 'returns object if one of groups matches' do
  15. expect(described_class.available_in_groups([group, create(:group)]))
  16. .to include(object)
  17. end
  18. it 'does not return object if group does not match' do
  19. expect(described_class.available_in_groups([create(:group)]))
  20. .not_to include(object)
  21. end
  22. context 'when object is inactive' do
  23. before { object.update!(active: false) }
  24. it 'does not return inactive macros' do
  25. expect(described_class.available_in_groups([group]))
  26. .not_to include(object)
  27. end
  28. end
  29. end
  30. context 'when object has multiple groups' do
  31. let(:groups) { [group, create(:group)] }
  32. it 'returns object if one of given group matches' do
  33. expect(described_class.available_in_groups([group]))
  34. .to include(object)
  35. end
  36. it 'returns object if one of given groups matches' do
  37. expect(described_class.available_in_groups([group, create(:group)]))
  38. .to include(object)
  39. end
  40. it 'does not return object if no group matches' do
  41. expect(described_class.available_in_groups([create(:group)]))
  42. .not_to include(object)
  43. end
  44. context 'when object is inactive' do
  45. before { object.update!(active: false) }
  46. it 'does not return inactive macros' do
  47. expect(described_class.available_in_groups([group]))
  48. .not_to include(object)
  49. end
  50. end
  51. end
  52. context 'when object has no group limitations' do
  53. let(:groups) { [] }
  54. it 'returns object for any group' do
  55. expect(described_class.available_in_groups([group]))
  56. .to include(object)
  57. end
  58. context 'when object is inactive' do
  59. before { object.update!(active: false) }
  60. it 'does not return inactive macros' do
  61. expect(described_class.available_in_groups([group]))
  62. .not_to include(object)
  63. end
  64. end
  65. end
  66. end
  67. end