Convert admin/announcements controller spec to system spec (#31655)

This commit is contained in:
Matt Jankowski 2024-08-30 03:15:28 -04:00 committed by GitHub
parent 92eba9096b
commit 1ee1c329cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 135 additions and 103 deletions

View file

@ -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)

View file

@ -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

View file

@ -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