From 1ee1c329cc12fa7a167eb312d6c91693f56327d9 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 30 Aug 2024 03:15:28 -0400 Subject: [PATCH] Convert `admin/announcements` controller spec to system spec (#31655) --- .../announcements/_announcement.html.haml | 2 +- .../admin/announcements_controller_spec.rb | 102 ------------- spec/system/admin/announcements_spec.rb | 134 ++++++++++++++++++ 3 files changed, 135 insertions(+), 103 deletions(-) delete mode 100644 spec/controllers/admin/announcements_controller_spec.rb create mode 100644 spec/system/admin/announcements_spec.rb diff --git a/app/views/admin/announcements/_announcement.html.haml b/app/views/admin/announcements/_announcement.html.haml index eac78efb2..8190f87d2 100644 --- a/app/views/admin/announcements/_announcement.html.haml +++ b/app/views/admin/announcements/_announcement.html.haml @@ -1,4 +1,4 @@ -.announcements-list__item +.announcements-list__item{ id: dom_id(announcement) } = link_to edit_admin_announcement_path(announcement), class: 'announcements-list__item__title' do = truncate(announcement.text) diff --git a/spec/controllers/admin/announcements_controller_spec.rb b/spec/controllers/admin/announcements_controller_spec.rb deleted file mode 100644 index c2d3135d9..000000000 --- a/spec/controllers/admin/announcements_controller_spec.rb +++ /dev/null @@ -1,102 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe Admin::AnnouncementsController do - render_views - - let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) } - - before do - sign_in user, scope: :user - end - - describe 'GET #index' do - it 'returns http success' do - get :index - - expect(response).to have_http_status(:success) - end - end - - describe 'GET #new' do - it 'returns http success and renders new' do - get :new - - expect(response).to have_http_status(:success) - expect(response).to render_template(:new) - end - end - - describe 'GET #edit' do - let(:announcement) { Fabricate(:announcement) } - - it 'returns http success and renders edit' do - get :edit, params: { id: announcement.id } - - expect(response).to have_http_status(:success) - expect(response).to render_template(:edit) - end - end - - describe 'POST #create' do - it 'creates a new announcement and redirects' do - expect do - post :create, params: { announcement: { text: 'The announcement message.' } } - end.to change(Announcement, :count).by(1) - - expect(response).to redirect_to(admin_announcements_path) - expect(flash.notice).to match(I18n.t('admin.announcements.published_msg')) - end - end - - describe 'PUT #update' do - let(:announcement) { Fabricate(:announcement, text: 'Original text') } - - it 'updates an announcement and redirects' do - put :update, params: { id: announcement.id, announcement: { text: 'Updated text.' } } - - expect(response).to redirect_to(admin_announcements_path) - expect(flash.notice).to match(I18n.t('admin.announcements.updated_msg')) - end - end - - describe 'DELETE #destroy' do - let!(:announcement) { Fabricate(:announcement, text: 'Original text') } - - it 'destroys an announcement and redirects' do - expect do - delete :destroy, params: { id: announcement.id } - end.to change(Announcement, :count).by(-1) - - expect(response).to redirect_to(admin_announcements_path) - expect(flash.notice).to match(I18n.t('admin.announcements.destroyed_msg')) - end - end - - describe 'POST #publish' do - subject { post :publish, params: { id: announcement.id } } - - let(:announcement) { Fabricate(:announcement, published_at: nil) } - - it 'marks announcement published' do - subject - - expect(announcement.reload).to be_published - expect(response).to redirect_to admin_announcements_path - end - end - - describe 'POST #unpublish' do - subject { post :unpublish, params: { id: announcement.id } } - - let(:announcement) { Fabricate(:announcement, published_at: 4.days.ago) } - - it 'marks announcement as not published' do - subject - - expect(announcement.reload).to_not be_published - expect(response).to redirect_to admin_announcements_path - end - end -end diff --git a/spec/system/admin/announcements_spec.rb b/spec/system/admin/announcements_spec.rb new file mode 100644 index 000000000..5c4e5d3e6 --- /dev/null +++ b/spec/system/admin/announcements_spec.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Admin::Announcements' do + include ActionView::RecordIdentifier + + describe 'Viewing announcements' do + it 'can view a list of existing announcements' do + announcement = Fabricate :announcement, text: 'Test Announcement' + sign_in admin_user + visit admin_announcements_path + + within css_id(announcement) do + expect(page) + .to have_content(announcement.text) + end + end + end + + describe 'Creating announcements' do + it 'create a new announcement' do + sign_in admin_user + visit new_admin_announcement_path + + fill_in text_label, + with: 'Announcement text' + + expect { submit_form } + .to change(Announcement, :count).by(1) + expect(page) + .to have_content(I18n.t('admin.announcements.published_msg')) + end + end + + describe 'Updating announcements' do + it 'updates an existing announcement' do + announcement = Fabricate :announcement, text: 'Test Announcement' + sign_in admin_user + visit admin_announcements_path + + within css_id(announcement) do + click_on announcement.text + end + + fill_in text_label, + with: 'Announcement text' + save_changes + + expect(page) + .to have_content(I18n.t('admin.announcements.updated_msg')) + end + end + + describe 'Deleting announcements' do + it 'deletes an existing announcement' do + announcement = Fabricate :announcement, text: 'Test Announcement' + sign_in admin_user + visit admin_announcements_path + + expect { delete_announcement(announcement) } + .to change(Announcement, :count).by(-1) + + expect(page) + .to have_content(I18n.t('admin.announcements.destroyed_msg')) + end + end + + describe 'Publishing announcements' do + it 'publishes an existing announcement' do + announcement = Fabricate :announcement, published: false, scheduled_at: 10.days.from_now + sign_in admin_user + visit admin_announcements_path + + expect { publish_announcement(announcement) } + .to change { announcement.reload.published? }.to(true) + + expect(page) + .to have_content(I18n.t('admin.announcements.published_msg')) + end + + it 'unpublishes an existing announcement' do + announcement = Fabricate :announcement, published: true + sign_in admin_user + visit admin_announcements_path + + expect { unpublish_announcement(announcement) } + .to change { announcement.reload.published? }.to(false) + + expect(page) + .to have_content(I18n.t('admin.announcements.unpublished_msg')) + end + end + + private + + def css_id(record) + "##{dom_id(record)}" # TODO: Extract to system spec helper? + end + + def publish_announcement(announcement) + within css_id(announcement) do + click_on I18n.t('admin.announcements.publish') + end + end + + def unpublish_announcement(announcement) + within css_id(announcement) do + click_on I18n.t('admin.announcements.unpublish') + end + end + + def delete_announcement(announcement) + within css_id(announcement) do + click_on I18n.t('generic.delete') + end + end + + def save_changes + click_on I18n.t('generic.save_changes') + end + + def submit_form + click_on I18n.t('admin.announcements.new.create') + end + + def text_label + I18n.t('simple_form.labels.announcement.text') + end + + def admin_user + Fabricate(:user, role: UserRole.find_by(name: 'Admin')) + end +end