1
0

announcements_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Admin::Announcements' do
  4. include ActionView::RecordIdentifier
  5. describe 'Viewing announcements' do
  6. it 'can view a list of existing announcements' do
  7. announcement = Fabricate :announcement, text: 'Test Announcement'
  8. sign_in admin_user
  9. visit admin_announcements_path
  10. within css_id(announcement) do
  11. expect(page)
  12. .to have_content(announcement.text)
  13. end
  14. end
  15. end
  16. describe 'Creating announcements' do
  17. it 'create a new announcement' do
  18. sign_in admin_user
  19. visit new_admin_announcement_path
  20. fill_in text_label,
  21. with: 'Announcement text'
  22. expect { submit_form }
  23. .to change(Announcement, :count).by(1)
  24. expect(page)
  25. .to have_content(I18n.t('admin.announcements.published_msg'))
  26. end
  27. end
  28. describe 'Updating announcements' do
  29. it 'updates an existing announcement' do
  30. announcement = Fabricate :announcement, text: 'Test Announcement'
  31. sign_in admin_user
  32. visit admin_announcements_path
  33. within css_id(announcement) do
  34. click_on announcement.text
  35. end
  36. fill_in text_label,
  37. with: 'Announcement text'
  38. click_on submit_button
  39. expect(page)
  40. .to have_content(I18n.t('admin.announcements.updated_msg'))
  41. end
  42. end
  43. describe 'Deleting announcements' do
  44. it 'deletes an existing announcement' do
  45. announcement = Fabricate :announcement, text: 'Test Announcement'
  46. sign_in admin_user
  47. visit admin_announcements_path
  48. expect { delete_announcement(announcement) }
  49. .to change(Announcement, :count).by(-1)
  50. expect(page)
  51. .to have_content(I18n.t('admin.announcements.destroyed_msg'))
  52. end
  53. end
  54. describe 'Publishing announcements' do
  55. it 'publishes an existing announcement' do
  56. announcement = Fabricate :announcement, published: false, scheduled_at: 10.days.from_now
  57. sign_in admin_user
  58. visit admin_announcements_path
  59. expect { publish_announcement(announcement) }
  60. .to change { announcement.reload.published? }.to(true)
  61. expect(page)
  62. .to have_content(I18n.t('admin.announcements.published_msg'))
  63. end
  64. it 'unpublishes an existing announcement' do
  65. announcement = Fabricate :announcement, published: true
  66. sign_in admin_user
  67. visit admin_announcements_path
  68. expect { unpublish_announcement(announcement) }
  69. .to change { announcement.reload.published? }.to(false)
  70. expect(page)
  71. .to have_content(I18n.t('admin.announcements.unpublished_msg'))
  72. end
  73. end
  74. private
  75. def publish_announcement(announcement)
  76. within css_id(announcement) do
  77. click_on I18n.t('admin.announcements.publish')
  78. end
  79. end
  80. def unpublish_announcement(announcement)
  81. within css_id(announcement) do
  82. click_on I18n.t('admin.announcements.unpublish')
  83. end
  84. end
  85. def delete_announcement(announcement)
  86. within css_id(announcement) do
  87. click_on I18n.t('generic.delete')
  88. end
  89. end
  90. def submit_form
  91. click_on I18n.t('admin.announcements.new.create')
  92. end
  93. def text_label
  94. I18n.t('simple_form.labels.announcement.text')
  95. end
  96. def admin_user
  97. Fabricate(:user, role: UserRole.find_by(name: 'Admin'))
  98. end
  99. end