announcements_controller_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Admin::AnnouncementsController do
  4. render_views
  5. let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  6. before do
  7. sign_in user, scope: :user
  8. end
  9. describe 'GET #index' do
  10. it 'returns http success' do
  11. get :index
  12. expect(response).to have_http_status(:success)
  13. end
  14. end
  15. describe 'GET #new' do
  16. it 'returns http success and renders new' do
  17. get :new
  18. expect(response).to have_http_status(:success)
  19. expect(response).to render_template(:new)
  20. end
  21. end
  22. describe 'GET #edit' do
  23. let(:announcement) { Fabricate(:announcement) }
  24. it 'returns http success and renders edit' do
  25. get :edit, params: { id: announcement.id }
  26. expect(response).to have_http_status(:success)
  27. expect(response).to render_template(:edit)
  28. end
  29. end
  30. describe 'POST #create' do
  31. it 'creates a new announcement and redirects' do
  32. expect do
  33. post :create, params: { announcement: { text: 'The announcement message.' } }
  34. end.to change(Announcement, :count).by(1)
  35. expect(response).to redirect_to(admin_announcements_path)
  36. expect(flash.notice).to match(I18n.t('admin.announcements.published_msg'))
  37. end
  38. end
  39. describe 'PUT #update' do
  40. let(:announcement) { Fabricate(:announcement, text: 'Original text') }
  41. it 'updates an announcement and redirects' do
  42. put :update, params: { id: announcement.id, announcement: { text: 'Updated text.' } }
  43. expect(response).to redirect_to(admin_announcements_path)
  44. expect(flash.notice).to match(I18n.t('admin.announcements.updated_msg'))
  45. end
  46. end
  47. describe 'DELETE #destroy' do
  48. let!(:announcement) { Fabricate(:announcement, text: 'Original text') }
  49. it 'destroys an announcement and redirects' do
  50. expect do
  51. delete :destroy, params: { id: announcement.id }
  52. end.to change(Announcement, :count).by(-1)
  53. expect(response).to redirect_to(admin_announcements_path)
  54. expect(flash.notice).to match(I18n.t('admin.announcements.destroyed_msg'))
  55. end
  56. end
  57. describe 'POST #publish' do
  58. subject { post :publish, params: { id: announcement.id } }
  59. let(:announcement) { Fabricate(:announcement, published_at: nil) }
  60. it 'marks announcement published' do
  61. subject
  62. expect(announcement.reload).to be_published
  63. expect(response).to redirect_to admin_announcements_path
  64. end
  65. end
  66. describe 'POST #unpublish' do
  67. subject { post :unpublish, params: { id: announcement.id } }
  68. let(:announcement) { Fabricate(:announcement, published_at: 4.days.ago) }
  69. it 'marks announcement as not published' do
  70. subject
  71. expect(announcement.reload).to_not be_published
  72. expect(response).to redirect_to admin_announcements_path
  73. end
  74. end
  75. end