From 3e5d37d8b1181b76a56812778d3b50f458bd4100 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Thu, 16 Feb 2023 17:16:48 +0000 Subject: [PATCH] move helper method and reuse some existing logic, adjust tests accordingly --- app/helpers/collection_time_helper.rb | 5 +++ app/helpers/question_view_helper.rb | 12 ------ app/views/form/_date_question.html.erb | 2 +- spec/helpers/collection_time_helper_spec.rb | 41 +++++++++++++++--- spec/helpers/question_view_helper_spec.rb | 48 --------------------- 5 files changed, 42 insertions(+), 66 deletions(-) diff --git a/app/helpers/collection_time_helper.rb b/app/helpers/collection_time_helper.rb index 8478d06e7..11b73e8cf 100644 --- a/app/helpers/collection_time_helper.rb +++ b/app/helpers/collection_time_helper.rb @@ -10,6 +10,11 @@ module CollectionTimeHelper date < window_end_date ? Time.zone.local(date.year - 1, 4, 1) : Time.zone.local(date.year, 4, 1) end + def date_mid_collection_year_formatted(date) + example_date = date.nil? ? Time.zone.today : collection_start_date(date).to_date + 5.months + example_date.to_formatted_s(:govuk_date_number_month) + end + def current_collection_start_date Time.zone.local(current_collection_start_year, 4, 1) end diff --git a/app/helpers/question_view_helper.rb b/app/helpers/question_view_helper.rb index 8c6b37422..fc308b835 100644 --- a/app/helpers/question_view_helper.rb +++ b/app/helpers/question_view_helper.rb @@ -13,18 +13,6 @@ 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 bd5ec3a9c..167e02a31 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, #{example_date_in_tax_year_of(lettings_log.startdate).to_formatted_s(:govuk_date_number_month)}" }, + hint: { text: question.hint_text&.html_safe || "For example, #{date_mid_collection_year_formatted(lettings_log.startdate)}" }, 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/collection_time_helper_spec.rb b/spec/helpers/collection_time_helper_spec.rb index 3b02802f2..21514e3d6 100644 --- a/spec/helpers/collection_time_helper_spec.rb +++ b/spec/helpers/collection_time_helper_spec.rb @@ -4,13 +4,13 @@ RSpec.describe CollectionTimeHelper do let(:current_user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) } - around do |example| - Timecop.freeze(now) do - example.run + describe "Current collection start year" do + around do |example| + Timecop.freeze(now) do + example.run + end end - end - describe "Current collection start year" do context "when the date is after 1st of April" do let(:now) { Time.utc(2022, 8, 3) } @@ -31,4 +31,35 @@ RSpec.describe CollectionTimeHelper do end end end + + describe "#date_mid_collection_year_formatted" do + subject(:result) { date_mid_collection_year_formatted(input) } + + context "when called with nil" do + let(:input) { nil } + + it "returns the current date" do + today = Time.zone.today + expect(result).to eq("#{today.day} #{today.month} #{today.year}") + end + end + + context "when called with a date after the first of 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("1 9 #{calendar_year}") + 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("1 9 #{calendar_year - 1}") + end + end + end end diff --git a/spec/helpers/question_view_helper_spec.rb b/spec/helpers/question_view_helper_spec.rb index b342bad6d..82f6d0b08 100644 --- a/spec/helpers/question_view_helper_spec.rb +++ b/spec/helpers/question_view_helper_spec.rb @@ -76,52 +76,4 @@ 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