Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

DRY up Projects::StatusController spec

Alexander Brandon Coles00c99b418e8e
diff --git a/spec/controllers/projects/status_controller_spec.rb b/spec/controllers/projects/status_controller_spec.rb
index 125f142dad8..78030b35651 100644
--- a/spec/controllers/projects/status_controller_spec.rb
+++ b/spec/controllers/projects/status_controller_spec.rb
@@ -35,7 +35,7 @@ RSpec.describe Projects::StatusController do
current_user { user }
let(:project) { build_stubbed(:project) }
- let(:service_result) { ServiceResult.success(result: project) }
+ let(:service_result) { ServiceResult.failure }
before do
allow(Project)
@@ -51,134 +51,127 @@ RSpec.describe Projects::StatusController do
.and_return(update_service)
end
- describe "PUT #update" do
- context "when service call succeeds" do
- context "with valid status_code param" do
- context "with a text/html request" do
- it "redirects back or to project show" do
- put :update, params: { project_id: project, status_code: :foo }
-
- expect(response).to redirect_to project_path(project)
- expect(flash[:notice]).to eq I18n.t(:notice_successful_update)
- end
- end
+ shared_examples_for "successful update" do
+ context "with a text/html request" do
+ let(:format) { nil }
- context "with a turbo stream request" do
- it "renders turbo streams updating Projects::StatusButtonComponent and flash action" do
- put :update, params: { project_id: project, status_code: :foo }, format: :turbo_stream
+ it "redirects back or to project show", :aggregate_failures do
+ expect(response).to redirect_to project_path(project)
+ expect(flash[:notice]).to eq I18n.t(:notice_successful_update)
+ end
+ end
- expect(response).to be_successful
- expect(assigns(:project)).to eq project
- expect(response).to have_turbo_stream action: "update", target: "projects-status-button-component"
- expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
- end
- end
+ context "with a turbo stream request" do
+ let(:format) { :turbo_stream }
+
+ it "renders turbo streams updating Projects::StatusButtonComponent and flash action", :aggregate_failures do
+ expect(response).to be_successful
+ expect(assigns(:project)).to eq project
+ expect(response).to have_turbo_stream action: "update", target: "projects-status-button-component"
+ expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
end
+ end
+ end
- context "with valid empty status_code param" do
- context "when service call succeeds" do
- context "with a text/html request" do
- it "redirects back or to project show" do
- put :update, params: { project_id: project, status_code: "" }
-
- expect(response).to redirect_to project_path(project)
- expect(flash[:notice]).to eq I18n.t(:notice_successful_update)
- end
- end
-
- context "with a turbo stream request" do
- it "renders turbo streams updating Projects::StatusButtonComponent and flash action" do
- put :update, params: { project_id: project, status_code: "" }, format: :turbo_stream
-
- expect(response).to be_successful
- expect(assigns(:project)).to eq project
- expect(response).to have_turbo_stream action: "update", target: "projects-status-button-component"
- expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
- end
- end
- end
+ shared_examples_for "unsuccessful update" do
+ context "with a text/html request" do
+ let(:format) { nil }
+
+ it "redirects back or to project show", :aggregate_failures do
+ expect(response).to redirect_to project_path(project)
+ expect(flash[:error]).to start_with I18n.t(:notice_unsuccessful_update_with_reason, reason: "")
end
end
- context "when service call fails" do
- let(:service_result) { ServiceResult.failure(result: project, message: "Custom Field 1 must not be blank") }
+ context "with a turbo stream request" do
+ let(:format) { :turbo_stream }
- context "with a text/html request" do
- it "redirects back or to project show" do
- put :update, params: { project_id: project, status_code: :foo }
+ it "renders turbo stream flash action", :aggregate_failures do
+ put :update, params: { project_id: project, status_code: :foo }, format: :turbo_stream
- expect(response).to redirect_to project_path(project)
- expect(flash[:error]).to start_with I18n.t(:notice_unsuccessful_update_with_reason, reason: "")
- end
+ expect(response).not_to be_successful
+ expect(response).to have_http_status :unprocessable_entity
+ expect(assigns(:project)).to eq project
+ expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
end
+ end
+ end
- context "with a turbo stream request" do
- it "renders turbo stream flash action" do
- put :update, params: { project_id: project, status_code: :foo }, format: :turbo_stream
+ describe "PUT #update" do
+ before do
+ put :update, params: { project_id: project, **params }, format:
+ end
- expect(response).not_to be_successful
- expect(response).to have_http_status :unprocessable_entity
- expect(assigns(:project)).to eq project
- expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
- end
+ context "with valid status_code param" do
+ let(:params) { { status_code: :foo } }
+
+ context "when service call succeeds" do
+ let(:service_result) { ServiceResult.success(result: project) }
+
+ it_behaves_like "successful update"
end
- end
- context "with invalid params" do
- it "invalid params" do
- put :update, params: { project_id: project, not_status_code: "something" }, format: :turbo_stream
+ context "when service call fails" do
+ let(:service_result) { ServiceResult.failure(result: project, message: "") }
- expect(response).not_to be_successful
- expect(response).to have_http_status :bad_request
+ it_behaves_like "unsuccessful update"
end
end
- end
- describe "DELETE #destroy" do
- context "when service call succeeds" do
- context "with a text/html request" do
- it "redirects back or to project show" do
- delete :destroy, params: { project_id: project }
+ context "with valid, empty status_code param" do
+ let(:params) { { status_code: "" } }
- expect(response).to redirect_to project_path(project)
- expect(flash[:notice]).to eq I18n.t(:notice_successful_update)
- end
+ context "when service call succeeds" do
+ let(:service_result) { ServiceResult.success(result: project) }
+
+ it_behaves_like "successful update"
end
- context "with a turbo stream request" do
- it "renders turbo streams updating Projects::StatusButtonComponent and flash action" do
- delete :destroy, params: { project_id: project }, format: :turbo_stream
+ context "when service call fails" do
+ let(:service_result) { ServiceResult.failure(result: project, message: "") }
- expect(response).to be_successful
- expect(assigns(:project)).to eq project
- expect(response).to have_turbo_stream action: "update", target: "projects-status-button-component"
- expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
- end
+ it_behaves_like "unsuccessful update"
end
end
- context "when service call fails" do
- let(:service_result) { ServiceResult.failure(result: project, message: "Custom Field 1 must not be blank") }
+ context "with invalid params" do
+ let(:params) { { not_status_code: "something" } }
context "with a text/html request" do
- it "redirects back or to project show" do
- delete :destroy, params: { project_id: project }
+ let(:format) { nil }
- expect(response).to redirect_to project_path(project)
- expect(flash[:error]).to start_with I18n.t(:notice_unsuccessful_update_with_reason, reason: "")
+ it "responds with 400 Bad Request status", :aggregate_failures do
+ expect(response).not_to be_successful
+ expect(response).to have_http_status :bad_request
end
end
context "with a turbo stream request" do
- it "renders turbo stream flash action" do
- delete :destroy, params: { project_id: project }, format: :turbo_stream
+ let(:format) { :turbo_stream }
+ it "responds with 400 Bad Request status", :aggregate_failures do
expect(response).not_to be_successful
- expect(response).to have_http_status :unprocessable_entity
- expect(assigns(:project)).to eq project
- expect(response).to have_turbo_stream action: "flash", target: "op-primer-flash-component"
+ expect(response).to have_http_status :bad_request
end
end
end
end
+
+ describe "DELETE #destroy" do
+ before do
+ delete :destroy, params: { project_id: project }, format:
+ end
+
+ context "when service call succeeds" do
+ let(:service_result) { ServiceResult.success(result: project) }
+
+ it_behaves_like "successful update"
+ end
+
+ context "when service call fails" do
+ let(:service_result) { ServiceResult.failure(result: project, message: "") }
+
+ it_behaves_like "unsuccessful update"
+ end
+ end
end