From 72db368fe62524a9dd01f09d95f4439e40fa4fa8 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Thu, 16 Feb 2023 15:05:38 +0000 Subject: [PATCH] update to show an example date in the tax year of the relevant log where that year is known --- app/helpers/question_view_helper.rb | 12 ++++++ app/views/form/_date_question.html.erb | 2 +- spec/helpers/question_view_helper_spec.rb | 48 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/app/helpers/question_view_helper.rb b/app/helpers/question_view_helper.rb index fc308b835..8c6b37422 100644 --- a/app/helpers/question_view_helper.rb +++ b/app/helpers/question_view_helper.rb @@ -13,6 +13,18 @@ module QuestionViewHelper } end + def example_date_in_tax_year_of(date) + return Time.zone.today if date.nil? + + year = if date.month > 4 || (date.month == 4 && date.day > 5) + date.year + else + date.year - 1 + end + + Date.new(year, 9, 1) + end + private def label_size(page_header, conditional) diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index dc951e162..bd5ec3a9c 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -3,7 +3,7 @@ <%= f.govuk_date_field question.id.to_sym, caption: caption(caption_text, page_header, conditional), legend: legend(question, page_header, conditional), - hint: { text: question.hint_text&.html_safe || "For example, #{Time.zone.today.to_formatted_s(:govuk_date_number_month)}" }, + hint: { text: question.hint_text&.html_safe || "For example, #{example_date_in_tax_year_of(lettings_log.startdate).to_formatted_s(:govuk_date_number_month)}" }, width: 20, **stimulus_html_attributes(question) do %> <%= govuk_inset_text(text: question.unresolved_hint_text) if question.unresolved_hint_text.present? && @log.unresolved %> diff --git a/spec/helpers/question_view_helper_spec.rb b/spec/helpers/question_view_helper_spec.rb index 82f6d0b08..b342bad6d 100644 --- a/spec/helpers/question_view_helper_spec.rb +++ b/spec/helpers/question_view_helper_spec.rb @@ -76,4 +76,52 @@ RSpec.describe QuestionViewHelper do end end end + + describe "#example_date_in_tax_year_of" do + subject(:result) { example_date_in_tax_year_of(input) } + + context "when called with nil" do + let(:input) { nil } + + it "returns the current date" do + expect(result).to eq(Time.zone.today) + end + end + + context "when called with a date after April" do + calendar_year = 2030 + let(:input) { Date.new(calendar_year, 7, 7) } + + it "returns the first of September from that year" do + expect(result).to eq(Date.new(calendar_year, 9, 1)) + end + end + + context "when called with a date before April" do + calendar_year = 2040 + let(:input) { Date.new(calendar_year, 2, 7) } + + it "returns the first of September from the previous year" do + expect(result).to eq(Date.new(calendar_year - 1, 9, 1)) + end + end + + context "when called with a date in April after the fifth" do + calendar_year = 2050 + let(:input) { Date.new(calendar_year, 4, 7) } + + it "returns the first of September from that year" do + expect(result).to eq(Date.new(calendar_year, 9, 1)) + end + end + + context "when called with a date in April before the sixth" do + calendar_year = 2060 + let(:input) { Date.new(calendar_year, 4, 4) } + + it "returns the first of September from the previous year" do + expect(result).to eq(Date.new(calendar_year - 1, 9, 1)) + end + end + end end