Browse Source

move helper method and reuse some existing logic, adjust tests accordingly

pull/1293/head
Arthur Campbell 3 years ago
parent
commit
3e5d37d8b1
  1. 5
      app/helpers/collection_time_helper.rb
  2. 12
      app/helpers/question_view_helper.rb
  3. 2
      app/views/form/_date_question.html.erb
  4. 41
      spec/helpers/collection_time_helper_spec.rb
  5. 48
      spec/helpers/question_view_helper_spec.rb

5
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) date < window_end_date ? Time.zone.local(date.year - 1, 4, 1) : Time.zone.local(date.year, 4, 1)
end 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 def current_collection_start_date
Time.zone.local(current_collection_start_year, 4, 1) Time.zone.local(current_collection_start_year, 4, 1)
end end

12
app/helpers/question_view_helper.rb

@ -13,18 +13,6 @@ module QuestionViewHelper
} }
end 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 private
def label_size(page_header, conditional) def label_size(page_header, conditional)

2
app/views/form/_date_question.html.erb

@ -3,7 +3,7 @@
<%= f.govuk_date_field question.id.to_sym, <%= f.govuk_date_field question.id.to_sym,
caption: caption(caption_text, page_header, conditional), caption: caption(caption_text, page_header, conditional),
legend: legend(question, 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, width: 20,
**stimulus_html_attributes(question) do %> **stimulus_html_attributes(question) do %>
<%= govuk_inset_text(text: question.unresolved_hint_text) if question.unresolved_hint_text.present? && @log.unresolved %> <%= govuk_inset_text(text: question.unresolved_hint_text) if question.unresolved_hint_text.present? && @log.unresolved %>

41
spec/helpers/collection_time_helper_spec.rb

@ -4,13 +4,13 @@ RSpec.describe CollectionTimeHelper do
let(:current_user) { create(:user, :data_coordinator) } let(:current_user) { create(:user, :data_coordinator) }
let(:user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) }
around do |example| describe "Current collection start year" do
Timecop.freeze(now) do around do |example|
example.run Timecop.freeze(now) do
example.run
end
end end
end
describe "Current collection start year" do
context "when the date is after 1st of April" do context "when the date is after 1st of April" do
let(:now) { Time.utc(2022, 8, 3) } let(:now) { Time.utc(2022, 8, 3) }
@ -31,4 +31,35 @@ RSpec.describe CollectionTimeHelper do
end end
end 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 end

48
spec/helpers/question_view_helper_spec.rb

@ -76,52 +76,4 @@ RSpec.describe QuestionViewHelper do
end end
end 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 end

Loading…
Cancel
Save