1
0

announcement_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Announcement do
  4. describe 'Scopes' do
  5. context 'with published and unpublished records' do
  6. let!(:published) { Fabricate(:announcement, published: true) }
  7. let!(:unpublished) { Fabricate(:announcement, published: false, scheduled_at: 10.days.from_now) }
  8. describe '#unpublished' do
  9. it 'returns records with published false' do
  10. results = described_class.unpublished
  11. expect(results).to eq([unpublished])
  12. end
  13. end
  14. describe '#published' do
  15. it 'returns records with published true' do
  16. results = described_class.published
  17. expect(results).to eq([published])
  18. end
  19. end
  20. end
  21. context 'with timestamped announcements' do
  22. let!(:adam_announcement) { Fabricate(:announcement, starts_at: 100.days.ago, scheduled_at: 10.days.ago, published_at: 10.days.ago, ends_at: 5.days.from_now) }
  23. let!(:brenda_announcement) { Fabricate(:announcement, starts_at: 10.days.ago, scheduled_at: 100.days.ago, published_at: 10.days.ago, ends_at: 5.days.from_now) }
  24. let!(:clara_announcement) { Fabricate(:announcement, starts_at: 10.days.ago, scheduled_at: 10.days.ago, published_at: 100.days.ago, ends_at: 5.days.from_now) }
  25. let!(:darnelle_announcement) { Fabricate(:announcement, starts_at: 10.days.ago, scheduled_at: 10.days.ago, published_at: 10.days.ago, ends_at: 5.days.from_now, created_at: 100.days.ago) }
  26. describe '#chronological' do
  27. it 'orders the records correctly' do
  28. results = described_class.chronological
  29. expect(results).to eq(
  30. [
  31. adam_announcement,
  32. brenda_announcement,
  33. clara_announcement,
  34. darnelle_announcement,
  35. ]
  36. )
  37. end
  38. end
  39. describe '#reverse_chronological' do
  40. it 'orders the records correctly' do
  41. results = described_class.reverse_chronological
  42. expect(results).to eq(
  43. [
  44. darnelle_announcement,
  45. clara_announcement,
  46. brenda_announcement,
  47. adam_announcement,
  48. ]
  49. )
  50. end
  51. end
  52. end
  53. end
  54. describe 'Validations' do
  55. it { is_expected.to validate_presence_of(:text) }
  56. describe 'ends_at' do
  57. context 'when starts_at is present' do
  58. subject { Fabricate.build :announcement, starts_at: 1.day.ago }
  59. it { is_expected.to validate_presence_of(:ends_at) }
  60. end
  61. context 'when starts_at is missing' do
  62. subject { Fabricate.build :announcement, starts_at: nil }
  63. it { is_expected.to_not validate_presence_of(:ends_at) }
  64. end
  65. end
  66. describe 'starts_at' do
  67. context 'when ends_at is present' do
  68. subject { Fabricate.build :announcement, ends_at: 1.day.ago }
  69. it { is_expected.to validate_presence_of(:starts_at) }
  70. end
  71. context 'when ends_at is missing' do
  72. subject { Fabricate.build :announcement, ends_at: nil }
  73. it { is_expected.to_not validate_presence_of(:starts_at) }
  74. end
  75. end
  76. end
  77. describe '#publish!' do
  78. it 'publishes an unpublished record' do
  79. announcement = Fabricate(:announcement, published: false, scheduled_at: 10.days.from_now)
  80. announcement.publish!
  81. expect(announcement).to be_published
  82. expect(announcement.published_at).to_not be_nil
  83. expect(announcement.scheduled_at).to be_nil
  84. end
  85. end
  86. describe '#unpublish!' do
  87. it 'unpublishes a published record' do
  88. announcement = Fabricate(:announcement, published: true)
  89. announcement.unpublish!
  90. expect(announcement).to_not be_published
  91. expect(announcement.scheduled_at).to be_nil
  92. end
  93. end
  94. describe '#reactions' do
  95. context 'with announcement_reactions present' do
  96. let(:account_reaction_emoji) { Fabricate :custom_emoji }
  97. let(:other_reaction_emoji) { Fabricate :custom_emoji }
  98. let!(:account) { Fabricate(:account) }
  99. let!(:announcement) { Fabricate(:announcement) }
  100. before do
  101. Fabricate(:announcement_reaction, announcement: announcement, created_at: 10.days.ago, name: other_reaction_emoji.shortcode)
  102. Fabricate(:announcement_reaction, announcement: announcement, created_at: 5.days.ago, account: account, name: account_reaction_emoji.shortcode)
  103. Fabricate(:announcement_reaction) # For some other announcement
  104. end
  105. it 'returns the announcement reactions for the announcement' do
  106. results = announcement.reactions
  107. expect(results).to have_attributes(
  108. size: eq(2),
  109. first: have_attributes(name: other_reaction_emoji.shortcode, me: false),
  110. last: have_attributes(name: account_reaction_emoji.shortcode, me: false)
  111. )
  112. end
  113. it 'returns the announcement reactions for the announcement with `me` set correctly' do
  114. results = announcement.reactions(account)
  115. expect(results).to have_attributes(
  116. size: eq(2),
  117. first: have_attributes(name: other_reaction_emoji.shortcode, me: false),
  118. last: have_attributes(name: account_reaction_emoji.shortcode, me: true)
  119. )
  120. end
  121. end
  122. end
  123. describe '#statuses' do
  124. let(:announcement) { Fabricate(:announcement, status_ids: status_ids) }
  125. context 'with empty status_ids' do
  126. let(:status_ids) { nil }
  127. it 'returns empty array' do
  128. results = announcement.statuses
  129. expect(results).to eq([])
  130. end
  131. end
  132. context 'with relevant status_ids' do
  133. let(:status) { Fabricate(:status, visibility: :public) }
  134. let(:direct_status) { Fabricate(:status, visibility: :direct) }
  135. let(:status_ids) { [status.id, direct_status.id] }
  136. it 'returns public and unlisted statuses' do
  137. results = announcement.statuses
  138. expect(results).to include(status)
  139. expect(results).to_not include(direct_status)
  140. end
  141. end
  142. end
  143. end