Skip to content

Commit

Permalink
Refactor DeleteConditionInput
Browse files Browse the repository at this point in the history
- Don't mock DeleteConditionInput object in request specs
- Delegate to condition record rather than requiring caller to unpack
- Add spec for validation errors
  • Loading branch information
lfdebrux committed Feb 6, 2025
1 parent 35b2605 commit d8b496d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
6 changes: 3 additions & 3 deletions app/controllers/pages/conditions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def update
def delete
condition = ConditionRepository.find(condition_id: params[:condition_id], form_id: current_form.id, page_id: page.id)

delete_condition_input = Pages::DeleteConditionInput.new(form: current_form, page:, record: condition, answer_value: condition.answer_value, goto_page_id: condition.goto_page_id)
delete_condition_input = Pages::DeleteConditionInput.new(form: current_form, page:, record: condition)

render template: "pages/conditions/delete", locals: { delete_condition_input: }
end

def destroy
condition = ConditionRepository.find(condition_id: params[:condition_id], form_id: current_form.id, page_id: page.id)

form_params = delete_condition_input_params.merge(record: condition, answer_value: condition.answer_value, goto_page_id: condition.goto_page_id)
form_params = delete_condition_input_params.merge(record: condition)

delete_condition_input = Pages::DeleteConditionInput.new(form_params)

Expand All @@ -94,7 +94,7 @@ def condition_input_params
end

def delete_condition_input_params
params.require(:pages_delete_condition_input).permit(:answer_value, :goto_page_id, :confirm).merge(form: current_form, page:)
params.require(:pages_delete_condition_input).permit(:confirm).merge(form: current_form, page:)
end

def new_condition_or_show_routes_path(page)
Expand Down
4 changes: 3 additions & 1 deletion app/input_objects/pages/delete_condition_input.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Pages::DeleteConditionInput < ConfirmActionInput
attr_accessor :form, :page, :check_page_id, :routing_page_id, :answer_value, :goto_page_id, :record
attr_accessor :form, :page, :record

delegate :check_page_id, :routing_page_id, :goto_page_id, :answer_value, to: :record

def submit
return false if invalid?
Expand Down
2 changes: 1 addition & 1 deletion spec/input_objects/pages/delete_condition_input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe Pages::DeleteConditionInput, type: :model do
let(:delete_condition_input) { described_class.new(form:, page:, record: condition, goto_page_id:) }
let(:delete_condition_input) { described_class.new(form:, page:, record: condition) }
let(:form) { build :form, :ready_for_routing, id: 1 }
let(:pages) { form.pages }
let(:page) { pages.second }
Expand Down
21 changes: 4 additions & 17 deletions spec/requests/pages/conditions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,6 @@

allow(ConditionRepository).to receive(:find).and_return(condition)

delete_condition_input = Pages::DeleteConditionInput.new(form:, page: selected_page, record: condition, answer_value: "Yes", goto_page_id: 3)

allow(delete_condition_input).to receive(:goto_page_question_text).and_return("What is your name?")

allow(Pages::DeleteConditionInput).to receive(:new).and_return(delete_condition_input)

get delete_condition_path(form_id: 1, page_id: selected_page.id, condition_id: condition.id)
end

Expand Down Expand Up @@ -338,25 +332,19 @@
describe "#destroy" do
let(:condition) { build :condition, id: 1, routing_page_id: pages.first.id, check_page_id: pages.first.id, answer_value: "Wales", goto_page_id: pages.last.id }
let(:confirm) { "yes" }
let(:submit_bool) { true }
let(:destroy_bool) { true }

before do
selected_page.routing_conditions = [condition]
selected_page.position = 1

allow(PageRepository).to receive(:find).and_return(selected_page)
allow(ConditionRepository).to receive_messages(find: condition, destroy: nil)

delete_condition_input = Pages::DeleteConditionInput.new(form:, page: selected_page, record: condition, answer_value: "Wales", goto_page_id: 3, confirm:)

allow(delete_condition_input).to receive_messages(goto_page_question_text: "What is your name?", submit: submit_bool)

allow(Pages::DeleteConditionInput).to receive(:new).and_return(delete_condition_input)
allow(ConditionRepository).to receive_messages(find: condition, destroy: destroy_bool)

delete destroy_condition_path(form_id: form.id,
page_id: selected_page.id,
condition_id: condition.id,
params: { pages_delete_condition_input: { confirm:, goto_page_id: 3, answer_value: "Wales" } })
params: { pages_delete_condition_input: { confirm: } })
end

it "reads the form" do
Expand All @@ -381,15 +369,14 @@
end

context "when the destroy fails" do
let(:submit_bool) { false }
let(:destroy_bool) { false }

it "return 422 error code" do
expect(response).to have_http_status(:unprocessable_entity)
end
end

context "when form submit fails" do
let(:submit_bool) { false }
let(:confirm) { nil }

it "return 422 error code" do
Expand Down
15 changes: 14 additions & 1 deletion spec/views/pages/conditions/delete.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

describe "pages/conditions/delete.html.erb" do
let(:delete_condition_input) { Pages::DeleteConditionInput.new(form:, page:, record: condition, answer_value: condition.answer_value, goto_page_id: condition.goto_page_id) }
let(:delete_condition_input) { Pages::DeleteConditionInput.new(form:, page:, record: condition) }
let(:form) { build :form, :ready_for_routing, id: 1 }
let(:condition) { build :condition, id: 1, routing_page_id: 1, check_page_id: 1, answer_value: "Wales", goto_page_id: pages.last.id }
let(:pages) { form.pages }
Expand Down Expand Up @@ -30,4 +30,17 @@
it "has a submit button" do
expect(rendered).to have_css("button[type='submit'].govuk-button", text: I18n.t("save_and_continue"))
end

context "when there is a validation error" do
let(:delete_condition_input) do
delete_condition_input = Pages::DeleteConditionInput.new(form:, page:, record: condition)
delete_condition_input.confirm = nil
delete_condition_input.validate
delete_condition_input
end

it "renders an error summary" do
expect(rendered).to have_css ".govuk-error-summary"
end
end
end

0 comments on commit d8b496d

Please sign in to comment.