From 5cd50f372c68a43b81accea43a1f64537712651e Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:36:14 +0100 Subject: [PATCH] Modify method to add punctuation if needed --- app/models/form/question.rb | 5 ++- spec/models/form/question_spec.rb | 75 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 1fbced841..113ec9cbc 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -204,11 +204,12 @@ class Form::Question end def error_display_label - error_label || check_answer_label || header || id.humanize + label = error_label || check_answer_label || header || id.humanize + label.match?(/[[:punct:]]\z/) && !label.match?(/[(){}\[\]]\z/) ? label : "#{label}." end def unanswered_error_message - question_text = error_display_label.presence || "this question" + question_text = error_display_label.presence || "this question." I18n.t("validations.not_answered", question: question_text.downcase) end diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index ca67e1103..76cb7edc1 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -375,4 +375,79 @@ RSpec.describe Form::Question, type: :model do end end end + + describe "#error_display_label" do + let(:question_with_error_label_no_punctuation) { described_class.new("address_line1_input", { "header" => "Address line 1", "error_label" => "Address line 1" }, page) } + let(:question_with_error_label_with_full_stop) { described_class.new("address_line1_input", { "header" => "Address line 1", "error_label" => "Address line 1." }, page) } + let(:question_with_error_label_with_question_mark) { described_class.new("address_line1_input", { "header" => "Address line 1", "error_label" => "Address line 1?" }, page) } + let(:question_with_error_label_with_brackets) { described_class.new("address_line1_input", { "header" => "Address line 1", "error_label" => "(Address line 1)" }, page) } + + let(:question_with_check_answer_label_no_punctuation) { described_class.new("address_line1_input", { "header" => "Address line 1", "check_answer_label" => "Address line 1" }, page) } + let(:question_with_check_answer_label_with_full_stop) { described_class.new("address_line1_input", { "header" => "Address line 1", "check_answer_label" => "Address line 1." }, page) } + let(:question_with_check_answer_label_with_question_mark) { described_class.new("address_line1_input", { "header" => "Address line 1", "check_answer_label" => "Address line 1?" }, page) } + let(:question_with_check_answer_label_with_brackets) { described_class.new("address_line1_input", { "header" => "Address line 1", "check_answer_label" => "(Address line 1)" }, page) } + + let(:question_with_header_no_punctuation) { described_class.new("address_line1_input", { "header" => "Address line 1" }, page) } + let(:question_with_header_with_full_stop) { described_class.new("address_line1_input", { "header" => "Address line 1." }, page) } + let(:question_with_header_with_question_mark) { described_class.new("address_line1_input", { "header" => "Address line 1?" }, page) } + let(:question_with_header_with_brackets) { described_class.new("address_line1_input", { "header" => "(Address line 1)" }, page) } + + let(:question_with_id_only_no_punctuation) { described_class.new("address_line1_input", {}, page) } + + context "when the error label should stay the same" do + it "returns the error label" do + expect(question_with_error_label_with_full_stop.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_error_label_with_question_mark.error_display_label).to eq("Address line 1?") + end + + it "returns the error label" do + expect(question_with_check_answer_label_with_full_stop.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_check_answer_label_with_question_mark.error_display_label).to eq("Address line 1?") + end + + it "returns the error label" do + expect(question_with_header_with_full_stop.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_header_with_question_mark.error_display_label).to eq("Address line 1?") + end + end + + context "when the error label should have a full stop added" do + it "returns the error label" do + expect(question_with_error_label_no_punctuation.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_error_label_with_brackets.error_display_label).to eq("(Address line 1).") + end + + it "returns the error label" do + expect(question_with_check_answer_label_no_punctuation.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_check_answer_label_with_brackets.error_display_label).to eq("(Address line 1).") + end + + it "returns the error label" do + expect(question_with_header_no_punctuation.error_display_label).to eq("Address line 1.") + end + + it "returns the error label" do + expect(question_with_header_with_brackets.error_display_label).to eq("(Address line 1).") + end + + it "returns the error label" do + expect(question_with_id_only_no_punctuation.error_display_label).to eq("Address line1 input.") + end + end + end end