<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index be8d4e983..f1e133100 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -28,6 +28,7 @@
delete :discard_draft, on: :member
get :confirm_publish, on: :member
+ get :confirm_discard, on: :member
get :original_publication_date, on: :member, action: :edit_original_publication_date
put :original_publication_date, on: :member, action: :update_original_publication_date
diff --git a/features/deleting-a-manual.feature b/features/deleting-a-manual.feature
index c95c1d9e2..12e08036f 100644
--- a/features/deleting-a-manual.feature
+++ b/features/deleting-a-manual.feature
@@ -16,6 +16,7 @@ Feature: Rake task to delete a manual
Given a draft manual exists without any sections
And a draft section exists for the manual
When I discard the draft manual
+ And I confirm draft deletion
Then the manual and its sections are deleted
Scenario: Deleting a published manual
diff --git a/features/step_definitions/deleting_manuals_steps.rb b/features/step_definitions/deleting_manuals_steps.rb
index 71291e71d..7c0078e84 100644
--- a/features/step_definitions/deleting_manuals_steps.rb
+++ b/features/step_definitions/deleting_manuals_steps.rb
@@ -29,3 +29,8 @@
When(/^I discard the draft manual$/) do
discard_draft_manual(@manual.title)
end
+
+When(/^I confirm draft deletion$/) do
+ expect(page).to have_button("Discard manual")
+ click_on "Discard manual"
+end
diff --git a/spec/controllers/manuals_controller_spec.rb b/spec/controllers/manuals_controller_spec.rb
index 664cd1cf3..6492c470c 100644
--- a/spec/controllers/manuals_controller_spec.rb
+++ b/spec/controllers/manuals_controller_spec.rb
@@ -34,6 +34,48 @@
end
end
+ describe "#confirm_discard" do
+ let(:manual_id) { "manual-1" }
+ let(:service) { double(Manual::ShowService, call: manual) }
+ let(:manual) { instance_double(Manual, title: manual_title) }
+ let(:manual_title) { "My manual title" }
+
+ before do
+ login_as_stub_user
+ allow(Manual::ShowService).to receive(:new).and_return(service)
+ end
+
+ context "when the manual has been previously published" do
+ before do
+ allow(manual).to receive(:has_ever_been_published?).and_return(true)
+ get :confirm_discard, params: { id: manual_id }
+ end
+
+ it "sets a flash message indicating failure" do
+ expect(flash[:error]).to include("#{manual_title} cannot be discarded as it has already been published")
+ end
+
+ it "redirects to the show page for the manual" do
+ expect(response).to redirect_to manual_path(manual_id)
+ end
+ end
+
+ context "when the manual has not been previously published" do
+ before do
+ allow(manual).to receive(:has_ever_been_published?).and_return(false)
+ get :confirm_discard, params: { id: manual_id }
+ end
+
+ it "renders the discard confirmation page" do
+ expect(response).to render_template(:confirm_discard)
+ end
+
+ it "renders with the design system layout" do
+ expect(response).to render_template("design_system")
+ end
+ end
+ end
+
describe "#discard_draft" do
let(:manual_id) { "manual-1" }
let(:service) { double(Manual::DiscardDraftService, call: result) }
@@ -49,7 +91,7 @@
let(:discard_success) { true }
it "sets a flash message indicating success" do
- expect(flash[:notice]).to include("Discarded draft of My manual")
+ expect(flash[:success]).to include("Discarded draft of My manual")
end
it "redirects to the manuals index" do
@@ -61,7 +103,7 @@
let(:discard_success) { false }
it "sets a flash message indicating failure" do
- expect(flash[:notice]).to include("Unable to discard draft of My manual")
+ expect(flash[:error]).to include("Unable to discard draft of My manual")
end
it "redirects to the show page for the manual" do
diff --git a/spec/views/manuals/show.html.erb_spec.rb b/spec/views/manuals/show.html.erb_spec.rb
new file mode 100644
index 000000000..c35a8c597
--- /dev/null
+++ b/spec/views/manuals/show.html.erb_spec.rb
@@ -0,0 +1,26 @@
+require "spec_helper"
+
+describe "manuals/show", type: :view do
+ before do
+ allow(view).to receive(:current_user_is_gds_editor?).and_return(true)
+ allow(view).to receive(:current_user_can_publish?).and_return(true)
+ end
+
+ it "does not render the discard button for a published manual" do
+ manual = FactoryBot.build_stubbed(:manual, ever_been_published: true)
+ manual.publish_tasks = []
+
+ render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] }
+
+ expect(rendered).not_to match(/Discard draft/)
+ end
+
+ it "renders the discard button for an unpublished manual" do
+ manual = FactoryBot.build_stubbed(:manual, ever_been_published: false)
+ manual.publish_tasks = []
+
+ render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] }
+
+ expect(rendered).to match(/Discard draft/)
+ end
+end