diff --git a/app/components/check_answers_summary_list_card_component.html.erb b/app/components/check_answers_summary_list_card_component.html.erb index 4a26b8c8d..4c1f9aa2d 100644 --- a/app/components/check_answers_summary_list_card_component.html.erb +++ b/app/components/check_answers_summary_list_card_component.html.erb @@ -35,8 +35,8 @@ <% if @log.collection_period_open_for_editing? %> <% row.with_action( - text: question.action_text(log), - href: action_href(question, log), + text: question.action_text(log, correcting_hard_validation: @correcting_hard_validation), + href: correct_validation_action_href(question, log, applicable_questions.map(&:id), @correcting_hard_validation), visually_hidden_text: question.check_answer_label.to_s.downcase, ) %> <% end %> diff --git a/app/components/check_answers_summary_list_card_component.rb b/app/components/check_answers_summary_list_card_component.rb index 205a8516e..5242c7f41 100644 --- a/app/components/check_answers_summary_list_card_component.rb +++ b/app/components/check_answers_summary_list_card_component.rb @@ -1,10 +1,11 @@ class CheckAnswersSummaryListCardComponent < ViewComponent::Base attr_reader :questions, :log, :user - def initialize(questions:, log:, user:) + def initialize(questions:, log:, user:, correcting_hard_validation: false) @questions = questions @log = log @user = user + @correcting_hard_validation = correcting_hard_validation super end @@ -33,6 +34,16 @@ class CheckAnswersSummaryListCardComponent < ViewComponent::Base send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer:) end + def correct_validation_action_href(question, log, _related_question_ids, correcting_hard_validation) + return action_href(question, log) unless correcting_hard_validation + + if question.displayed_as_answered?(log) + send("#{log.model_name.param_key}_confirm_clear_answer_path", log, question_id: question.id) + else + send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer: "check_errors", related_question_ids: request.query_parameters["related_question_ids"], original_page_id: request.query_parameters["original_page_id"]) + end + end + private def unanswered_value(question) diff --git a/app/controllers/check_errors_controller.rb b/app/controllers/check_errors_controller.rb new file mode 100644 index 000000000..f246e0b01 --- /dev/null +++ b/app/controllers/check_errors_controller.rb @@ -0,0 +1,37 @@ +class CheckErrorsController < ApplicationController + include DuplicateLogsHelper + + before_action :authenticate_user! + before_action :find_resource_by_named_id + + def confirm_clear_answer + return render_not_found unless @log + + @related_question_ids = params[@log.model_name.param_key].keys.reject { |id| id == "page_id" } + @page = @log.form.get_page(params[@log.model_name.param_key]["page_id"]) + + if params["clear_all"] + @questions_to_clear = @related_question_ids.map { |id| + question = @log.form.get_question(id, @log) + next if question.subsection.id == "setup" + + question.page.questions.map(&:id) + }.flatten.compact + + render :confirm_clear_all_answers + else + question_id = @related_question_ids.find { |id| !params[id].nil? } + @question = @log.form.get_question(question_id, @log) + end + end + +private + + def find_resource_by_named_id + @log = if params[:sales_log_id].present? + current_user.sales_logs.visible.find_by(id: params[:sales_log_id]) + else + current_user.lettings_logs.visible.find_by(id: params[:lettings_log_id]) + end + end +end diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index a8d8fca48..d84f8642a 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -9,6 +9,8 @@ class FormController < ApplicationController def submit_form if @log @page = form.get_page(params[@log.model_name.param_key][:page]) + return render_check_errors_page if params["check_errors"] + shown_page_ids_with_unanswered_questions_before_update = @page.subsection.pages .select { |page| page.routed_to?(@log, current_user) } .select { |page| page.has_unanswered_questions?(@log) } @@ -30,7 +32,8 @@ class FormController < ApplicationController mandatory_questions_with_no_response.map do |question| @log.errors.add question.id.to_sym, question.unanswered_error_message, category: :not_answered end - Rails.logger.info "User triggered validation(s) on: #{@log.errors.map(&:attribute).join(', ')}" + error_attributes = @log.errors.map(&:attribute) + Rails.logger.info "User triggered validation(s) on: #{error_attributes.join(', ')}" @subsection = form.subsection_for_page(@page) restore_error_field_values(@page&.questions) render "form/page" @@ -64,12 +67,23 @@ class FormController < ApplicationController @interruption_page_referrer_type = from_referrer_query("referrer") end + if adding_answer_from_check_errors_page? + @related_question_ids = request.params["related_question_ids"] + @original_page_id = request.params["original_page_id"] + @check_errors = true + end + if @log page_id = request.path.split("/")[-1].underscore @page = form.get_page(page_id) @subsection = form.subsection_for_page(@page) - if @page.routed_to?(@log, current_user) || is_referrer_type?("interruption_screen") - render "form/page" + if @page.routed_to?(@log, current_user) || is_referrer_type?("interruption_screen") || adding_answer_from_check_errors_page? + if updated_answer_from_check_errors_page? + @questions = request.params["related_question_ids"].map { |id| @log.form.get_question(id, @log) } + render "form/check_errors" + else + render "form/page" + end else redirect_to @log.lettings? ? lettings_log_path(@log) : sales_log_path(@log) end @@ -213,6 +227,20 @@ private return send("#{@log.class.name.underscore}_#{previous_interruption_screen_page_id}_path", @log, { referrer: previous_interruption_screen_referrer, original_log_id: original_duplicate_log_id_from_query }.compact) end + if params[@log.model_name.param_key]["check_errors"] + @page = form.get_page(params[@log.model_name.param_key]["page"]) + flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).to_sentence}" + original_page_id = params[@log.model_name.param_key]["original_page_id"] + related_question_ids = params[@log.model_name.param_key]["related_question_ids"].split(" ") + return send("#{@log.class.name.underscore}_#{original_page_id}_path", @log, { check_errors: true, related_question_ids: }.compact) + end + + if params["referrer"] == "check_errors" + @page = form.get_page(params[@log.model_name.param_key]["page"]) + flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).to_sentence}" + return send("#{@log.class.name.underscore}_#{params['original_page_id']}_path", @log, { check_errors: true, related_question_ids: params["related_question_ids"] }.compact) + end + is_new_answer_from_check_answers = is_referrer_type?("check_answers_new_answer") redirect_path = form.next_page_redirect_path(@page, @log, current_user, ignore_answered: is_new_answer_from_check_answers) referrer = is_new_answer_from_check_answers ? "check_answers_new_answer" : nil @@ -374,4 +402,34 @@ private true end + + def render_check_errors_page + if params[@log.model_name.param_key]["clear_question_ids"].present? + question_ids = params[@log.model_name.param_key]["clear_question_ids"].split(" ") + question_ids.each do |question_id| + question = @log.form.get_question(question_id, @log) + next if question.subsection.id == "setup" + + question.page.questions.map(&:id).each { |id| @log[id] = nil } + end + @log.save! + @questions = params[@log.model_name.param_key].keys.reject { |id| %w[clear_question_ids page].include?(id) }.map { |id| @log.form.get_question(id, @log) } + else + responses_for_page = responses_for_page(@page) + @log.assign_attributes(responses_for_page) + @log.valid? + @log.reload + error_attributes = @log.errors.map(&:attribute) + @questions = @log.form.questions.select { |q| error_attributes.include?(q.id.to_sym) } + end + render "form/check_errors" + end + + def adding_answer_from_check_errors_page? + request.params["referrer"] == "check_errors" + end + + def updated_answer_from_check_errors_page? + params["check_errors"] + end end diff --git a/app/frontend/styles/_button.scss b/app/frontend/styles/_button.scss index 5975da0cb..4cd815b5b 100644 --- a/app/frontend/styles/_button.scss +++ b/app/frontend/styles/_button.scss @@ -29,3 +29,11 @@ $app-button-inverse-hover-background-colour: govuk-tint($app-button-inverse-fore background-color: $app-button-inverse-hover-background-colour; box-shadow: inset 0 0 0 2px $govuk-focus-colour; } + +.submit-button-link { + background: none; + border: none; + color: #1d70b8; + text-decoration: underline; + cursor: pointer; +} diff --git a/app/helpers/check_errors_helper.rb b/app/helpers/check_errors_helper.rb new file mode 100644 index 000000000..6d1ff0166 --- /dev/null +++ b/app/helpers/check_errors_helper.rb @@ -0,0 +1,11 @@ +module CheckErrorsHelper + include GovukLinkHelper + + def check_errors_answer_text(question, log) + question.displayed_as_answered?(log) ? "Change" : "Answer" + end + + def check_errors_answer_link(log, question, page, applicable_questions) + send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer: "check_errors", original_page_id: page.id, related_question_ids: applicable_questions.map(&:id)) + end +end diff --git a/app/helpers/form_page_error_helper.rb b/app/helpers/form_page_error_helper.rb index 4bfda9eee..8a46accca 100644 --- a/app/helpers/form_page_error_helper.rb +++ b/app/helpers/form_page_error_helper.rb @@ -3,4 +3,8 @@ module FormPageErrorHelper other_page_error_ids = lettings_log.errors.map(&:attribute) - page.questions.map { |q| q.id.to_sym }.concat([:base]) other_page_error_ids.each { |id| lettings_log.errors.delete(id) } end + + def all_questions_affected_by_errors(log) + log.errors.map(&:attribute) - [:base] + end end diff --git a/app/models/form/question.rb b/app/models/form/question.rb index a692574dd..960af8909 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -111,8 +111,10 @@ class Form::Question end end - def action_text(log) - displayed_as_answered?(log) ? "Change" : "Answer" + def action_text(log, correcting_hard_validation: false) + return "Answer" unless displayed_as_answered?(log) + + correcting_hard_validation ? "Clear" : "Change" end def displayed_as_answered?(log) diff --git a/app/views/check_errors/confirm_clear_all_answers.html.erb b/app/views/check_errors/confirm_clear_all_answers.html.erb new file mode 100644 index 000000000..85e936aef --- /dev/null +++ b/app/views/check_errors/confirm_clear_all_answers.html.erb @@ -0,0 +1,32 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to clear all?" %> +<% end %> + +
+
+

+ <%= content_for(:title) %> +

+

You've selected <%= @questions_to_clear.count %> answers to clear

+ + <%= govuk_warning_text(text: "Dependent answers related to this question may also get cleared. You will not be able to undo this action") %> + <%= form_with model: @log, url: send("#{@log.model_name.param_key}_#{@page.id}_path", @log), method: "post", local: true do |f| %> + + <% @related_question_ids.each do |id| %> + <%= f.hidden_field id, value: @log[id] %> + <% end %> + + <%= f.hidden_field :clear_question_ids, value: @questions_to_clear %> + <%= f.hidden_field :page, value: @page.id %> + +
+ <%= f.govuk_submit "Confirm and continue", name: "check_errors" %> + <%= govuk_button_link_to( + "Cancel", + "javascript:history.back()", + secondary: true, + ) %> +
+ <% end %> +
+
diff --git a/app/views/check_errors/confirm_clear_answer.html.erb b/app/views/check_errors/confirm_clear_answer.html.erb new file mode 100644 index 000000000..9dba77fae --- /dev/null +++ b/app/views/check_errors/confirm_clear_answer.html.erb @@ -0,0 +1,31 @@ +<% content_for :before_content do %> + <% content_for :title, "Are you sure you want to clear #{@question.check_answer_label}?" %> +<% end %> + +
+
+

+ <%= content_for(:title) %> +

+ + <%= govuk_warning_text(text: "Dependent answers related to this question may also get cleared. You will not be able to undo this action.") %> + <%= form_with model: @log, url: send("#{@log.model_name.param_key}_#{@page.id}_path", @log), method: "post", local: true do |f| %> + + <% @related_question_ids.each do |id| %> + <%= f.hidden_field id, value: @log[id] %> + <% end %> + + <%= f.hidden_field :clear_question_ids, value: [@question.id] %> + <%= f.hidden_field :page, value: @page.id %> + +
+ <%= f.govuk_submit "Confirm and continue", name: "check_errors" %> + <%= govuk_button_link_to( + "Cancel", + "javascript:history.back()", + secondary: true, + ) %> +
+ <% end %> +
+
diff --git a/app/views/form/check_errors.html.erb b/app/views/form/check_errors.html.erb new file mode 100644 index 000000000..27fa3ea0c --- /dev/null +++ b/app/views/form/check_errors.html.erb @@ -0,0 +1,66 @@ +
+
+ + <%= form_with model: @log, url: send("#{@log.model_name.param_key}_confirm_clear_answer_path", @log), method: "post", local: true do |f| %> + <%= f.govuk_error_summary %> + <%= f.hidden_field :page_id, value: @page.id %> + +

+
+ + Make sure these answers are correct: + + + + +
+

+ +
+
+ <% applicable_questions = @questions.reject { |q| q.hidden_in_check_answers?(@log, current_user) } %> +
+ <% applicable_questions.each do |question| %> + <%= f.hidden_field question.id, value: @log[question.id] %> +
+
+ <%= get_question_label(question) %> +
+
+ <%= simple_format( + get_answer_label(question, @log), + wrapper_tag: "span", + class: "govuk-!-margin-right-4", + ) %> + + <% extra_value = question.get_extra_check_answer_value(@log) %> + + <% if extra_value && question.answer_label(@log).present? %> + <%= simple_format( + extra_value, + wrapper_tag: "span", + class: "govuk-!-font-weight-regular app-!-colour-muted", + ) %> + <% end %> + + <% question.get_inferred_answers(@log).each do |inferred_answer| %> + <%= inferred_answer %> + <% end %> +
+
+ <% if !question.displayed_as_answered?(@log) || question.subsection.id == "setup" %> + <%= govuk_link_to check_errors_answer_text(question, @log), check_errors_answer_link(@log, question, @page, applicable_questions) %> + <% else %> + + <% end %> +
+
+ <% end %> +
+
+
+ <% end %> + + <%= govuk_button_link_to "Confirm and continue", @original_page_id ? send("#{@log.model_name.param_key}_#{@original_page_id}_path", @log) : send("#{@log.model_name.param_key}_#{@page.id}_path", @log) %> +
+
diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index fffc22411..95dc38ec6 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -16,6 +16,7 @@ <%= form_with model: @log, url: request.original_url, method: "post", local: true do |f| %>
+ <% all_questions_with_errors = all_questions_affected_by_errors(@log) %> <% remove_other_page_errors(@log, @page) %> <%= f.govuk_error_summary %> @@ -69,6 +70,17 @@ <%= f.hidden_field :page, value: @page.id %> <%= f.hidden_field :interruption_page_id, value: @interruption_page_id %> <%= f.hidden_field :interruption_page_referrer_type, value: @interruption_page_referrer_type %> + <%= f.hidden_field :related_question_ids, value: @related_question_ids %> + <%= f.hidden_field :original_page_id, value: @original_page_id %> + <% if @check_errors %> + <%= f.hidden_field :check_errors, value: @check_errors %> + <% end %> + + <% if all_questions_with_errors.count > 1 %> +
+ <%= f.submit "See all related answers", name: "check_errors", class: "govuk-body govuk-link submit-button-link" %> +
+ <% end %>
<% if !@page.interruption_screen? %> diff --git a/config/routes.rb b/config/routes.rb index e8932ea45..a6de59a65 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -214,6 +214,8 @@ Rails.application.routes.draw do get "delete-confirmation", to: "lettings_logs#delete_confirmation" get "duplicate-logs", to: "duplicate_logs#show" get "delete-duplicates", to: "duplicate_logs#delete_duplicates" + post "confirm-clear-answer", to: "check_errors#confirm_clear_answer" + post "confirm-clear-all-answers", to: "check_errors#confirm_clear_all_answers" collection do get "csv-download", to: "lettings_logs#download_csv" @@ -284,6 +286,8 @@ Rails.application.routes.draw do get "delete-confirmation", to: "sales_logs#delete_confirmation" get "duplicate-logs", to: "duplicate_logs#show" get "delete-duplicates", to: "duplicate_logs#delete_duplicates" + post "confirm-clear-answer", to: "check_errors#confirm_clear_answer" + post "confirm-clear-all-answers", to: "check_errors#confirm_clear_all_answers" collection do get "csv-download", to: "sales_logs#download_csv" diff --git a/spec/features/form/page_routing_spec.rb b/spec/features/form/page_routing_spec.rb index 8ee63de03..42a2c25fb 100644 --- a/spec/features/form/page_routing_spec.rb +++ b/spec/features/form/page_routing_spec.rb @@ -119,6 +119,16 @@ RSpec.describe "Form Page Routing" do expect(find_field("lettings_log[startdate(2i)]").value).to eq(nil) expect(find_field("lettings_log[startdate(1i)]").value).to eq(nil) end + + it "does not show see all related answers link if only 1 field has an error" do + visit("/lettings-logs/#{id}/tenancy-start-date") + fill_in("lettings_log[startdate(1i)]", with: "202") + fill_in("lettings_log[startdate(2i)]", with: "32") + fill_in("lettings_log[startdate(3i)]", with: "0") + click_button("Save and continue") + + expect(page).not_to have_link("See all related answers") + end end end @@ -262,4 +272,21 @@ RSpec.describe "Form Page Routing" do end end end + + describe "composite validations" do + context "when error is added to multiple fields" do + before do + lettings_log.update(needstype: 1, declaration: 1, ecstat1: 10, hhmemb: 2, net_income_known: 0, incfreq: 1, earnings: 1000) + end + + it "does shows see all related answers link" do + visit("/lettings-logs/#{id}/income-amount") + fill_in("lettings-log-earnings-field", with: "100000") + click_button("Save and continue") + + expect(page).to have_current_path("/lettings-logs/#{id}/income-amount") + expect(page).to have_button("See all related answers") + end + end + end end diff --git a/spec/requests/check_errors_controller_spec.rb b/spec/requests/check_errors_controller_spec.rb new file mode 100644 index 000000000..c101e7959 --- /dev/null +++ b/spec/requests/check_errors_controller_spec.rb @@ -0,0 +1,455 @@ +require "rails_helper" + +RSpec.describe CheckErrorsController, type: :request do + let(:page) { Capybara::Node::Simple.new(response.body) } + let(:user) { create(:user, :data_coordinator) } + let(:lettings_log) { create(:lettings_log, :setup_completed, assigned_to: user) } + let(:sales_log) { create(:sales_log, :shared_ownership_setup_complete, assigned_to: user) } + + describe "check errors page" do + context "when user is not signed in" do + it "redirects to sign in page for lettings" do + post "/lettings-logs/#{lettings_log.id}/net-income", params: {} + expect(response).to redirect_to("/account/sign-in") + end + + it "redirects to sign in page for sales" do + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: {} + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when the user is from different organisation" do + let(:other_user) { create(:user) } + + before do + sign_in other_user + end + + it "renders page not found for lettings" do + post "/lettings-logs/#{lettings_log.id}/net-income", params: {} + expect(response).to have_http_status(:not_found) + end + + it "renders page not found for sales" do + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: {} + expect(response).to have_http_status(:not_found) + end + end + + context "when user is signed in" do + context "with multiple error fields and answered questions for lettings" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + page: "income_amount", + earnings: "100000", + incfreq: "1", + }, + check_errors: "", + } + end + + before do + lettings_log.update!(needstype: 1, declaration: 1, ecstat1: 10, hhmemb: 2, net_income_known: 0, incfreq: nil, earnings: nil) + sign_in user + post "/lettings-logs/#{lettings_log.id}/income-amount", params: params + end + + it "displays correct clear links" do + expect(page).to have_selector("input[type=submit][value='Clear']", count: 2) + expect(page).to have_button("Clear all") + end + end + + context "with multiple error fields and answered questions for sales" do + let(:params) do + { + id: sales_log.id, + sales_log: { + page: "buyer_1_income", + income1: "100000", + la: "E09000001", + ownershipsch: "1", + }, + check_errors: "", + } + end + + before do + sales_log.update!(income1: 1000, la: "E09000001") + sign_in user + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: params + end + + it "displays correct clear and change links" do + expect(page.all(:button, value: "Clear").count).to eq(2) + expect(page).to have_link("Change", count: 1) + expect(page).to have_button("Clear all") + end + end + end + end + + describe "confirm clear answer page" do + context "when user is not signed in" do + it "redirects to sign in page for lettings" do + post "/lettings-logs/#{lettings_log.id}/confirm-clear-answer", params: {} + expect(response).to redirect_to("/account/sign-in") + end + + it "redirects to sign in page for sales" do + post "/sales-logs/#{sales_log.id}/confirm-clear-answer", params: {} + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when the user is from different organisation" do + let(:other_user) { create(:user) } + + before do + sign_in other_user + end + + it "renders page not found for lettings" do + post "/lettings-logs/#{lettings_log.id}/confirm-clear-answer", params: {} + expect(response).to have_http_status(:not_found) + end + + it "renders page not found for sales" do + post "/sales-logs/#{sales_log.id}/confirm-clear-answer", params: {} + expect(response).to have_http_status(:not_found) + end + end + + context "when user is signed in" do + context "and clearing specific lettings question" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + earnings: "100000", + incfreq: "1", + hhmemb: "2", + page_id: "income_amount", + }, + hhmemb: "", + } + end + + before do + sign_in user + post "/lettings-logs/#{lettings_log.id}/confirm-clear-answer", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Are you sure you want to clear Number of household members?") + expect(page).to have_content("Dependent answers related to this question may also get cleared. You will not be able to undo this action.") + expect(page).to have_link("Cancel") + expect(page).to have_button("Confirm and continue") + end + end + + context "and clearing specific sales question" do + let(:params) do + { + id: sales_log.id, + sales_log: { + income1: "100000", + la: "E09000001", + ownershipsch: "1", + page_id: "buyer_1_income", + }, + income1: "", + } + end + + before do + sign_in user + post "/sales-logs/#{sales_log.id}/confirm-clear-answer", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Are you sure you want to clear Buyer 1’s gross annual income?") + expect(page).to have_content("Dependent answers related to this question may also get cleared. You will not be able to undo this action.") + expect(page).to have_link("Cancel") + expect(page).to have_button("Confirm and continue") + end + end + end + end + + describe "confirm clear all answers" do + context "when user is signed in" do + context "and clearing all lettings questions" do + let(:params) do + { + id: lettings_log.id, + clear_all: "Clear all", + lettings_log: { + earnings: "100000", + incfreq: "1", + hhmemb: "2", + page_id: "income_amount", + }, + } + end + + before do + sign_in user + post "/lettings-logs/#{lettings_log.id}/confirm-clear-answer", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Are you sure you want to clear all") + expect(page).to have_content("You've selected 5 answers to clear") + expect(page).to have_content("You will not be able to undo this action") + expect(page).to have_link("Cancel") + expect(page).to have_button("Confirm and continue") + end + end + + context "and clearing all sales question" do + let(:params) do + { + id: sales_log.id, + clear_all: "Clear all", + sales_log: { + income1: "100000", + la: "E09000001", + ownershipsch: "1", + page_id: "buyer_1_income", + }, + } + end + + before do + sign_in user + post "/sales-logs/#{sales_log.id}/confirm-clear-answer", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Are you sure you want to clear all") + expect(page).to have_content("You've selected 3 answers to clear") + expect(page).to have_content("You will not be able to undo this action") + expect(page).to have_link("Cancel") + expect(page).to have_button("Confirm and continue") + end + end + end + end + + describe "clear answer" do + context "when user is not signed in" do + it "redirects to sign in page for lettings" do + post "/lettings-logs/#{lettings_log.id}/income-amount", params: {} + expect(response).to redirect_to("/account/sign-in") + end + + it "redirects to sign in page for sales" do + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: {} + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when the user is from different organisation" do + let(:other_user) { create(:user) } + + before do + sign_in other_user + end + + it "renders page not found for lettings" do + post "/lettings-logs/#{lettings_log.id}/income-amount", params: {} + expect(response).to have_http_status(:not_found) + end + + it "renders page not found for sales" do + post "/sales-logs/#{lettings_log.id}/buyer-1-income", params: {} + expect(response).to have_http_status(:not_found) + end + end + + context "when user is signed in" do + context "and clearing specific lettings question" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + earnings: "100000", + incfreq: "1", + hhmemb: "2", + clear_question_ids: "hhmemb", + page: "income_amount", + }, + check_errors: "", + } + end + + before do + sign_in user + post "/lettings-logs/#{lettings_log.id}/income-amount", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Make sure these answers are correct") + expect(page).to have_content("You didn’t answer this question") + expect(page).to have_link("Answer") + expect(lettings_log.reload.earnings).to eq(nil) + end + end + + context "and clearing specific sales question" do + let(:params) do + { + id: sales_log.id, + sales_log: { + income1: "100000", + la: "E09000001", + ownershipsch: "1", + clear_question_ids: "income1", + page: "buyer_1_income", + }, + check_errors: "", + } + end + + before do + sign_in user + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Make sure these answers are correct") + expect(page).to have_content("You didn’t answer this question") + expect(page).to have_link("Answer") + expect(sales_log.reload.income1).to eq(nil) + end + end + end + end + + describe "answer incomplete question" do + context "when user is signed in" do + context "and answering specific lettings question" do + let(:params) do + { + original_page_id: "household_members", + referrer: "check_errors", + related_question_ids: %w[hhmemb ecstat1 earnings], + lettings_log: { + page: "household_members", + hhmemb: "2", + }, + } + end + + before do + sign_in user + post "/lettings-logs/#{lettings_log.id}/household-members", params: + end + + it "maintains original check_errors data in query params" do + follow_redirect! + expect(request.query_parameters["check_errors"]).to eq("true") + expect(request.query_parameters["related_question_ids"]).to eq(%w[hhmemb ecstat1 earnings]) + expect(page).to have_content("You have successfully updated Number of household members") + expect(page).to have_link("Confirm and continue", href: "/lettings-logs/#{lettings_log.id}/household-members") + end + end + + context "and answering specific sales question" do + let(:params) do + { + original_page_id: "buyer_1_income", + referrer: "check_errors", + related_question_ids: %w[income1 la ownershipsch], + sales_log: { + page: "buyer_1_income", + income1: "1000", + income1nk: "0", + }, + } + end + + before do + sign_in user + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: + end + + it "maintains original check_errors data in query params" do + follow_redirect! + expect(request.query_parameters["check_errors"]).to eq("true") + expect(request.query_parameters["related_question_ids"]).to eq(%w[income1 la ownershipsch]) + expect(page).to have_content("You have successfully updated Buyer 1’s gross annual income known? and Buyer 1’s gross annual income") + expect(page).to have_link("Confirm and continue", href: "/sales-logs/#{sales_log.id}/buyer-1-income") + end + end + end + end + + describe "clear all answers" do + context "when user is signed in" do + context "and clearing all lettings question" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + earnings: "100000", + incfreq: "1", + hhmemb: "2", + clear_question_ids: "earnings incfreq hhmemb", + page: "income_amount", + }, + check_errors: "", + } + end + + before do + sign_in user + post "/lettings-logs/#{lettings_log.id}/income-amount", params: + end + + it "correctly clears the values" do + expect(page).to have_content("Make sure these answers are correct") + expect(page).to have_content("You didn’t answer this question") + expect(page.all(:button, value: "Clear").count).to eq(0) + expect(lettings_log.reload.earnings).to eq(nil) + expect(lettings_log.reload.incfreq).to eq(nil) + expect(lettings_log.reload.hhmemb).to eq(nil) + end + end + + context "and clearing all sales question" do + let(:params) do + { + id: sales_log.id, + sales_log: { + income1: "100000", + la: "E09000001", + ownershipsch: "1", + clear_question_ids: "income1 la ownershipsch", + page: "buyer_1_income", + }, + check_errors: "", + } + end + + before do + sign_in user + post "/sales-logs/#{sales_log.id}/buyer-1-income", params: + end + + it "displays correct clear links" do + expect(page).to have_content("Make sure these answers are correct") + expect(page).to have_content("You didn’t answer this question") + expect(page.all(:button, value: "Clear").count).to eq(0) + expect(sales_log.reload.income1).to eq(nil) + expect(sales_log.reload.la).to eq(nil) + expect(sales_log.reload.ownershipsch).not_to eq(nil) + end + end + end + end +end