has_tags_examples.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'HasTags' do
  3. subject { create(described_class.name.underscore) }
  4. describe '#tag_add' do
  5. let(:item_name) { 'foo' }
  6. it 'delegates to Tag.tag_add' do
  7. expect(Tag)
  8. .to receive(:tag_add)
  9. .with(object: described_class.name,
  10. o_id: subject.id,
  11. item: item_name,
  12. created_by_id: nil,
  13. sourceable: nil)
  14. subject.tag_add(item_name)
  15. end
  16. it 'optionally accepts a current_user_id argument' do
  17. expect(Tag)
  18. .to receive(:tag_add)
  19. .with(object: described_class.name,
  20. o_id: subject.id,
  21. item: item_name,
  22. created_by_id: 1,
  23. sourceable: nil)
  24. subject.tag_add(item_name, 1)
  25. end
  26. end
  27. describe '#tag_remove' do
  28. let(:item_name) { 'foo' }
  29. it 'delegates to Tag.tag_remove' do
  30. expect(Tag)
  31. .to receive(:tag_remove)
  32. .with(object: described_class.name,
  33. o_id: subject.id,
  34. item: item_name,
  35. created_by_id: nil,
  36. sourceable: nil)
  37. subject.tag_remove(item_name)
  38. end
  39. it 'optionally accepts a current_user_id argument' do
  40. expect(Tag)
  41. .to receive(:tag_remove)
  42. .with(object: described_class.name,
  43. o_id: subject.id,
  44. item: item_name,
  45. created_by_id: 1,
  46. sourceable: nil)
  47. subject.tag_remove(item_name, 1)
  48. end
  49. end
  50. describe '#tag_update' do
  51. let(:items) { %w[foo bar] }
  52. it 'delegates to Tag.tag_update' do
  53. expect(Tag)
  54. .to receive(:tag_update)
  55. .with(object: described_class.name,
  56. o_id: subject.id,
  57. items: items,
  58. created_by_id: nil)
  59. subject.tag_update(items)
  60. end
  61. it 'optionally accepts a current_user_id argument' do
  62. expect(Tag)
  63. .to receive(:tag_update)
  64. .with(object: described_class.name,
  65. o_id: subject.id,
  66. items: items,
  67. created_by_id: 1)
  68. subject.tag_update(items, 1)
  69. end
  70. end
  71. describe '#tag_list' do
  72. it 'delegates to Tag.tag_list' do
  73. expect(Tag)
  74. .to receive(:tag_list)
  75. .with(object: described_class.name,
  76. o_id: subject.id)
  77. subject.tag_list
  78. end
  79. end
  80. shared_context 'with subject and another object being tagged', current_user_id: 1 do
  81. before do
  82. subject.tag_add(tag)
  83. Tag.tag_add(object: 'AnotherObject', o_id: 123, item: tag)
  84. end
  85. let(:tag) { 'tag_name' }
  86. end
  87. describe '.tag_references' do
  88. include_context 'with subject and another object being tagged' do
  89. it 'returns reference to subject' do
  90. expect(described_class.tag_references(tag)).to contain_exactly(subject.id)
  91. end
  92. it 'does not return reference to subject when called with other tag' do
  93. expect(described_class.tag_references('other')).to be_blank
  94. end
  95. end
  96. end
  97. describe '.tag_objects' do
  98. include_context 'with subject and another object being tagged' do
  99. it 'returns subject' do
  100. expect(described_class.tag_objects(tag)).to contain_exactly(subject)
  101. end
  102. it 'does not return subject when called with other tag' do
  103. expect(described_class.tag_objects('other')).to be_blank
  104. end
  105. end
  106. end
  107. end