Browse Source

Fix tests

pull/708/head
baarkerlounger 3 years ago
parent
commit
000bda9abd
  1. 5
      app/controllers/form_controller.rb
  2. 1
      app/models/case_log.rb
  3. 8
      app/models/form/question.rb
  4. 10
      app/models/validations/submission_validations.rb
  5. 4
      spec/features/form/form_navigation_spec.rb
  6. 1
      spec/features/form/page_routing_spec.rb
  7. 1
      spec/features/form/progressive_total_field_spec.rb

5
app/controllers/form_controller.rb

@ -15,7 +15,7 @@ class FormController < ApplicationController
else else
redirect_path = "case_log_#{@page.id}_path" redirect_path = "case_log_#{@page.id}_path"
mandatory_questions_with_no_response.map do |question| mandatory_questions_with_no_response.map do |question|
@case_log.errors.add question.id.to_sym, I18n.t("validations.not_answered", question: question.display_label.downcase) @case_log.errors.add question.id.to_sym, question.unanswered_error_message
end end
session[:errors] = @case_log.errors.to_json session[:errors] = @case_log.errors.to_json
Rails.logger.info "User triggered validation(s) on: #{@case_log.errors.map(&:attribute).join(', ')}" Rails.logger.info "User triggered validation(s) on: #{@case_log.errors.map(&:attribute).join(', ')}"
@ -128,12 +128,13 @@ private
def mandatory_questions_with_no_response(responses_for_page) def mandatory_questions_with_no_response(responses_for_page)
@page.questions.select do |question| @page.questions.select do |question|
question_is_required?(question) && question_missing_response?(responses_for_page, question) question_is_required?(question) && question_missing_response?(responses_for_page, question)
end end
end end
def question_is_required?(question) def question_is_required?(question)
CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && CaseLog::OPTIONAL_FIELDS.exclude?(question.id) &&
@page.questions.map(&:result_field).exclude?(question.id) &&
@page.subsection.applicable_questions(@case_log).map(&:id).include?(question.id) @page.subsection.applicable_questions(@case_log).map(&:id).include?(question.id)
end end

1
app/models/case_log.rb

@ -8,7 +8,6 @@ class CaseLogValidator < ActiveModel::Validator
include Validations::TenancyValidations include Validations::TenancyValidations
include Validations::DateValidations include Validations::DateValidations
include Validations::LocalAuthorityValidations include Validations::LocalAuthorityValidations
include Validations::SubmissionValidations
def validate(record) def validate(record)
validation_methods = public_methods.select { |method| method.starts_with?("validate_") } validation_methods = public_methods.select { |method| method.starts_with?("validate_") }

8
app/models/form/question.rb

@ -173,10 +173,16 @@ class Form::Question
type == "radio" && RADIO_REFUSED_VALUE[id.to_sym]&.include?(value) type == "radio" && RADIO_REFUSED_VALUE[id.to_sym]&.include?(value)
end end
def display_label def display_label
check_answer_label || header || id.humanize check_answer_label || header || id.humanize
end end
def unanswered_error_message
return I18n.t("validations.declaration.missing") if id == "declaration"
I18n.t("validations.not_answered", question: display_label.downcase)
end
private private
def selected_answer_option_is_derived?(case_log) def selected_answer_option_is_derived?(case_log)

10
app/models/validations/submission_validations.rb

@ -1,10 +0,0 @@
module Validations::SubmissionValidations
# Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_declaration(record)
if record.declaration&.zero?
record.errors.add :declaration, I18n.t("validations.declaration.missing")
end
end
end

4
spec/features/form/form_navigation_spec.rb

@ -137,7 +137,7 @@ RSpec.describe "Form Navigation" do
expect(page).to have_title("Error") expect(page).to have_title("Error")
end end
context "when the page has a main and conditional question" do context "when the page has a main and conditional question" do
context "when the conditional question is required but not answered" do context "when the conditional question is required but not answered" do
xit "shows a validation error for the conditional question", js: true do xit "shows a validation error for the conditional question", js: true do
visit("/logs/#{id}/armed-forces") visit("/logs/#{id}/armed-forces")
@ -147,7 +147,7 @@ RSpec.describe "Form Navigation" do
expect(page).to have_selector("#case-log-leftreg-error") expect(page).to have_selector("#case-log-leftreg-error")
expect(page).to have_title("Error") expect(page).to have_title("Error")
end end
end end
end end
end end

1
spec/features/form/page_routing_spec.rb

@ -43,6 +43,7 @@ RSpec.describe "Form Page Routing" do
choose("case-log-preg-occ-2-field", allow_label_click: true) choose("case-log-preg-occ-2-field", allow_label_click: true)
click_button("Save and continue") click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/conditional-question-no-page") expect(page).to have_current_path("/logs/#{id}/conditional-question-no-page")
choose("case-log-cbl-0-field", allow_label_click: true)
click_button("Save and continue") click_button("Save and continue")
expect(page).to have_current_path("/logs/#{id}/conditional-question/check-answers") expect(page).to have_current_path("/logs/#{id}/conditional-question/check-answers")
end end

1
spec/features/form/progressive_total_field_spec.rb

@ -33,6 +33,7 @@ RSpec.describe "Accessible Automcomplete" do
it "total displays despite error message", js: true do it "total displays despite error message", js: true do
visit("/logs/#{case_log.id}/rent") visit("/logs/#{case_log.id}/rent")
choose("case-log-period-1-field", allow_label_click: true)
fill_in("case-log-brent-field", with: 500) fill_in("case-log-brent-field", with: 500)
fill_in("case-log-scharge-field", with: 50) fill_in("case-log-scharge-field", with: 50)
fill_in("case-log-pscharge-field", with: 50) fill_in("case-log-pscharge-field", with: 50)

Loading…
Cancel
Save