macro_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/has_collection_update_examples'
  4. require 'models/concerns/has_xss_sanitized_note_examples'
  5. require 'models/application_model/has_cache_examples'
  6. RSpec.describe Macro, type: :model do
  7. it_behaves_like 'HasCollectionUpdate', collection_factory: :macro
  8. it_behaves_like 'HasXssSanitizedNote', model_factory: :macro
  9. it_behaves_like 'Association clears cache', association: :groups
  10. describe 'validation' do
  11. it 'uses Validations::VerifyPerformRulesValidator' do
  12. expect(described_class).to have_validator(Validations::VerifyPerformRulesValidator).on(:perform)
  13. end
  14. end
  15. describe 'Instance methods:' do
  16. describe '#applicable_on?' do
  17. let(:ticket) { create(:ticket) }
  18. let(:ticket_a) { create(:ticket, group: group_a) }
  19. let(:ticket_b) { create(:ticket, group: group_b) }
  20. let(:ticket_c) { create(:ticket, group: group_c) }
  21. let(:group_a) { create(:group) }
  22. let(:group_b) { create(:group) }
  23. let(:group_c) { create(:group) }
  24. context 'when macro has no groups' do
  25. subject(:macro) { create(:macro, groups: []) }
  26. it 'return true for a single group' do
  27. expect(macro).to be_applicable_on(ticket)
  28. end
  29. it 'return true for multiple tickets' do
  30. expect(macro).to be_applicable_on([ticket, ticket_a, ticket_b])
  31. end
  32. end
  33. context 'when macro has a single group' do
  34. subject(:macro) { create(:macro, groups: [group_a]) }
  35. it 'returns true if macro group matches ticket' do
  36. expect(macro).to be_applicable_on(ticket_a)
  37. end
  38. it 'returns false if macro group does not match ticket' do
  39. expect(macro).not_to be_applicable_on(ticket_b)
  40. end
  41. it 'returns false if macro group match a ticket and not the other' do
  42. expect(macro).not_to be_applicable_on([ticket_a, ticket_b])
  43. end
  44. end
  45. context 'when macro has multiple groups' do
  46. subject(:macro) { create(:macro, groups: [group_a, group_c]) }
  47. it 'returns true if macro groups include ticket group' do
  48. expect(macro).to be_applicable_on(ticket_a)
  49. end
  50. it 'returns false if macro groups do not include ticket group' do
  51. expect(macro).not_to be_applicable_on(ticket_b)
  52. end
  53. it 'returns true if macro groups match tickets groups' do
  54. expect(macro).to be_applicable_on([ticket_a, ticket_c])
  55. end
  56. it 'returns false if macro groups does not match one of tickets group' do
  57. expect(macro).not_to be_applicable_on([ticket_a, ticket_b])
  58. end
  59. end
  60. end
  61. end
  62. end