Browse Source

Modify method to add punctuation if needed

pull/2674/head
Manny Dinssa 2 years ago
parent
commit
5cd50f372c
  1. 5
      app/models/form/question.rb
  2. 75
      spec/models/form/question_spec.rb

5
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

75
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

Loading…
Cancel
Save