From f64a2b7d92aae42c04fb4260a5396da688024aa9 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:16:53 +0100 Subject: [PATCH] Reset errors if a log gets successfully updated (#459) * reset errors if a log gets successfully updated * Add test --- app/controllers/form_controller.rb | 1 + spec/requests/form_controller_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 15a265b33..295a82c16 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -8,6 +8,7 @@ class FormController < ApplicationController @page = @case_log.form.get_page(params[:case_log][:page]) responses_for_page = responses_for_page(@page) if @case_log.update(responses_for_page) + session[:errors] = nil if is_referrer_check_answers? && !@case_log.form.next_page(@page, @case_log).to_s.include?("value_check") redirect_to(send("case_log_#{@case_log.form.subsection_for_page(@page).id}_check_answers_path", @case_log)) elsif @case_log.form.is_last_question?(@page, @case_log.form.subsection_for_page(@page), @case_log) diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 8c7ea8de8..32d8016d9 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -120,16 +120,35 @@ RSpec.describe FormController, type: :request do }, } end + let(:valid_params) do + { + id: case_log.id, + case_log: { + page: page_id, + age1: valid_answer, + }, + } + end before do post "/logs/#{case_log.id}/form", params: params end context "with invalid answers" do + let(:page) { Capybara::Node::Simple.new(response.body) } let(:answer) { 2000 } + let(:valid_answer) { 20 } it "re-renders the same page with errors if validation fails" do expect(response).to redirect_to("/logs/#{case_log.id}/#{page_id.dasherize}") + follow_redirect! + expect(page).to have_content("There is a problem") + end + + it "resets errors when fixed" do + post "/logs/#{case_log.id}/form", params: valid_params + get "/logs/#{case_log.id}/#{page_id.dasherize}" + expect(page).not_to have_content("There is a problem") end end