Browse Source

Refactor helper tests

pull/2421/head
Kat 2 years ago committed by kosiakkatrina
parent
commit
ff1f4dc307
  1. 5
      spec/factories/sales_log.rb
  2. 27
      spec/helpers/check_answers_helper_spec.rb
  3. 7
      spec/helpers/collection_time_helper_spec.rb
  4. 10
      spec/helpers/duplicate_logs_helper_spec.rb
  5. 14
      spec/helpers/filters_helper_spec.rb
  6. 11
      spec/helpers/form_page_error_helper_spec.rb
  7. 16
      spec/helpers/interruption_screen_helper_spec.rb
  8. 14
      spec/helpers/locations_helper_spec.rb
  9. 6
      spec/helpers/schemes_helper_spec.rb
  10. 48
      spec/helpers/tasklist_helper_spec.rb

5
spec/factories/sales_log.rb

@ -43,17 +43,14 @@ FactoryBot.define do
purchid { rand(999_999_999).to_s } purchid { rand(999_999_999).to_s }
end end
trait :duplicate do trait :duplicate do
shared_ownership_setup_complete
purchid { "PC123" } purchid { "PC123" }
ownershipsch { 2 }
type { 8 }
jointpur { 2 }
saledate { Time.zone.today } saledate { Time.zone.today }
age1_known { 1 } age1_known { 1 }
age1 { 20 } age1 { 20 }
sex1 { "F" } sex1 { "F" }
ecstat1 { 1 } ecstat1 { 1 }
postcode_full { "A1 1AA" } postcode_full { "A1 1AA" }
privacynotice { 1 }
uprn_known { 0 } uprn_known { 0 }
end end
trait :completed do trait :completed do

27
spec/helpers/check_answers_helper_spec.rb

@ -1,23 +1,27 @@
require "rails_helper" require "rails_helper"
RSpec.describe CheckAnswersHelper do RSpec.describe CheckAnswersHelper do
let(:form) { lettings_log.form } let(:lettings_log) { FactoryBot.build(:lettings_log) }
let(:subsection) { form.get_subsection("household_characteristics") }
let(:lettings_log) { FactoryBot.build(:lettings_log, :in_progress) }
let(:current_user) { FactoryBot.build(:user) } let(:current_user) { FactoryBot.build(:user) }
let(:subsection) { instance_double(Form::Subsection, form: lettings_log.form) }
around do |example| let(:questions) do
Timecop.freeze(Time.zone.local(2022, 1, 1)) do [
Singleton.__init__(FormHandler) Form::Lettings::Questions::Hhmemb,
example.run Form::Lettings::Questions::Age1Known,
Form::Lettings::Questions::Age1,
Form::Lettings::Questions::GenderIdentity1,
].map { |q| q.new(nil, nil, instance_double(Form::Page, subsection:, routed_to?: true)) }
end end
before do
allow(subsection).to receive(:applicable_questions).and_return(questions)
end end
describe "display_answered_questions_summary" do describe "display_answered_questions_summary" do
context "when a section hasn't been completed yet" do context "when a section hasn't been completed yet" do
it "returns that you have unanswered questions" do it "returns that you have unanswered questions" do
expect(display_answered_questions_summary(subsection, lettings_log, current_user)) expect(display_answered_questions_summary(subsection, lettings_log, current_user))
.to match(/You have answered 4 of 10 questions./) .to match(/You have answered 0 of 4 questions./)
end end
end end
@ -25,9 +29,8 @@ RSpec.describe CheckAnswersHelper do
it "returns that you have answered all the questions" do it "returns that you have answered all the questions" do
lettings_log.sex1 = "F" lettings_log.sex1 = "F"
lettings_log.hhmemb = 1 lettings_log.hhmemb = 1
lettings_log.propcode = "123" lettings_log.age1_known = 1
lettings_log.ecstat1 = 200 lettings_log.age1 = 18
lettings_log.ecstat2 = 9
expect(display_answered_questions_summary(subsection, lettings_log, current_user)) expect(display_answered_questions_summary(subsection, lettings_log, current_user))
.to match(/You answered all the questions./) .to match(/You answered all the questions./)
expect(display_answered_questions_summary(subsection, lettings_log, current_user)) expect(display_answered_questions_summary(subsection, lettings_log, current_user))

7
spec/helpers/collection_time_helper_spec.rb

@ -5,11 +5,8 @@ RSpec.describe CollectionTimeHelper do
let(:user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) }
describe "Current collection start year" do describe "Current collection start year" do
around do |example| before do
Timecop.freeze(now) do allow(Time).to receive(:now).and_return(now)
example.run
end
Timecop.return
end end
context "when the date is after 1st of April" do context "when the date is after 1st of April" do

10
spec/helpers/duplicate_logs_helper_spec.rb

@ -1,16 +1,6 @@
require "rails_helper" require "rails_helper"
RSpec.describe DuplicateLogsHelper do RSpec.describe DuplicateLogsHelper do
before do
Timecop.freeze(Time.zone.local(2024, 3, 1))
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
describe "#duplicates_for_user" do describe "#duplicates_for_user" do
let(:org) { create(:organisation) } let(:org) { create(:organisation) }
let(:other_org) { create(:organisation) } let(:other_org) { create(:organisation) }

14
spec/helpers/filters_helper_spec.rb

@ -241,11 +241,8 @@ RSpec.describe FiltersHelper do
describe "#collection_year_options" do describe "#collection_year_options" do
context "with 23/24 as the current collection year" do context "with 23/24 as the current collection year" do
around do |example| before do
Timecop.freeze(Time.zone.local(2023, 5, 1)) do allow(Time).to receive(:now).and_return(Time.zone.local(2023, 5, 1))
example.run
end
Timecop.return
end end
it "has the correct options" do it "has the correct options" do
@ -258,11 +255,8 @@ RSpec.describe FiltersHelper do
end end
context "with 24/25 as the current collection year" do context "with 24/25 as the current collection year" do
around do |example| before do
Timecop.freeze(Time.zone.local(2024, 5, 1)) do allow(Time).to receive(:now).and_return(Time.zone.local(2024, 5, 1))
example.run
end
Timecop.return
end end
it "has the correct options" do it "has the correct options" do

11
spec/helpers/form_page_error_helper_spec.rb

@ -3,15 +3,6 @@ require "rails_helper"
RSpec.describe FormPageErrorHelper do RSpec.describe FormPageErrorHelper do
describe "#remove_other_page_errors" do describe "#remove_other_page_errors" do
context "when non base other questions are removed" do context "when non base other questions are removed" do
around do |example|
Timecop.freeze(Time.zone.local(2022, 1, 1)) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
let!(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress) } let!(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress) }
let!(:form) { lettings_log.form } let!(:form) { lettings_log.form }
@ -22,7 +13,7 @@ RSpec.describe FormPageErrorHelper do
end end
it "returns details and user tabs" do it "returns details and user tabs" do
page = form.get_page("rent") page = form.get_question("period", lettings_log).page
remove_other_page_errors(lettings_log, page) remove_other_page_errors(lettings_log, page)
expect(lettings_log.errors.count).to eq(2) expect(lettings_log.errors.count).to eq(2)
expect(lettings_log.errors.map(&:attribute)).to include(:period) expect(lettings_log.errors.map(&:attribute)).to include(:period)

16
spec/helpers/interruption_screen_helper_spec.rb

@ -1,9 +1,6 @@
require "rails_helper" require "rails_helper"
RSpec.describe InterruptionScreenHelper do RSpec.describe InterruptionScreenHelper do
form_handler = FormHandler.instance
let(:form) { form_handler.get_form("test_form") }
let(:subsection) { form.get_subsection("household_characteristics") }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:lettings_log) do let(:lettings_log) do
create( create(
@ -11,7 +8,9 @@ RSpec.describe InterruptionScreenHelper do
:in_progress, :in_progress,
hhmemb: 1, hhmemb: 1,
ecstat1: 1, ecstat1: 1,
period: 1,
earnings: 750, earnings: 750,
net_income_known: 0,
incfreq: 1, incfreq: 1,
assigned_to: user, assigned_to: user,
sex1: "F", sex1: "F",
@ -19,15 +18,6 @@ RSpec.describe InterruptionScreenHelper do
) )
end end
around do |example|
Timecop.freeze(Time.zone.local(2022, 1, 1)) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
describe "display_informative_text" do describe "display_informative_text" do
context "when 2 out of 2 arguments are given" do context "when 2 out of 2 arguments are given" do
it "returns correct informative text" do it "returns correct informative text" do
@ -249,7 +239,7 @@ RSpec.describe InterruptionScreenHelper do
it "returns a list of questions affected by the soft validation" do it "returns a list of questions affected by the soft validation" do
expect(soft_validation_affected_questions(question, lettings_log).count).to eq(2) expect(soft_validation_affected_questions(question, lettings_log).count).to eq(2)
expect(soft_validation_affected_questions(question, lettings_log).map(&:id)).to eq(%w[age1 ecstat1]) expect(soft_validation_affected_questions(question, lettings_log).map(&:id)).to eq(%w[ecstat1 age1])
end end
end end
end end

14
spec/helpers/locations_helper_spec.rb

@ -51,7 +51,7 @@ RSpec.describe LocationsHelper do
let(:location) { FactoryBot.create(:location, startdate: nil) } let(:location) { FactoryBot.create(:location, startdate: nil) }
before do before do
Timecop.freeze(2023, 10, 10) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 10, 10))
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true) allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end end
@ -228,7 +228,7 @@ RSpec.describe LocationsHelper do
context "with previous deactivations" do context "with previous deactivations" do
context "and all reactivated deactivations" do context "and all reactivated deactivations" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: Time.zone.local(2022, 9, 28), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: Time.zone.local(2022, 9, 28), location:)
Timecop.return Timecop.return
@ -244,7 +244,7 @@ RSpec.describe LocationsHelper do
context "and non reactivated deactivation" do context "and non reactivated deactivation" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil, location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil, location:)
Timecop.return Timecop.return
@ -262,7 +262,7 @@ RSpec.describe LocationsHelper do
context "with out of order deactivations" do context "with out of order deactivations" do
context "and all reactivated deactivations" do context "and all reactivated deactivations" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: Time.zone.local(2022, 6, 18), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: Time.zone.local(2022, 6, 18), location:)
Timecop.return Timecop.return
@ -278,7 +278,7 @@ RSpec.describe LocationsHelper do
context "and one non reactivated deactivation" do context "and one non reactivated deactivation" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, location:)
Timecop.return Timecop.return
@ -296,7 +296,7 @@ RSpec.describe LocationsHelper do
context "with multiple out of order deactivations" do context "with multiple out of order deactivations" do
context "and one non reactivated deactivation" do context "and one non reactivated deactivation" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 24), reactivation_date: Time.zone.local(2022, 9, 28), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 24), reactivation_date: Time.zone.local(2022, 10, 28), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 24), reactivation_date: Time.zone.local(2022, 10, 28), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 15), reactivation_date: nil, location:)
@ -314,7 +314,7 @@ RSpec.describe LocationsHelper do
context "with intersecting deactivations" do context "with intersecting deactivations" do
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 11, 10))
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 10), reactivation_date: Time.zone.local(2022, 12, 1), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 10), reactivation_date: Time.zone.local(2022, 12, 1), location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 11, 11), reactivation_date: Time.zone.local(2022, 12, 11), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 11, 11), reactivation_date: Time.zone.local(2022, 12, 11), location:)
Timecop.return Timecop.return

6
spec/helpers/schemes_helper_spec.rb

@ -5,11 +5,7 @@ RSpec.describe SchemesHelper do
let(:scheme) { FactoryBot.create(:scheme, created_at: Time.zone.today) } let(:scheme) { FactoryBot.create(:scheme, created_at: Time.zone.today) }
before do before do
Timecop.freeze(2023, 1, 10) allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 10))
end
after do
Timecop.unfreeze
end end
it "returns one active period without to date" do it "returns one active period without to date" do

48
spec/helpers/tasklist_helper_spec.rb

@ -1,14 +1,10 @@
require "rails_helper" require "rails_helper"
RSpec.describe TasklistHelper do RSpec.describe TasklistHelper do
let(:now) { Time.utc(2022, 1, 1) } let(:now) { Time.utc(2025, 1, 1) }
around do |example| before do
Timecop.freeze(now) do allow(Time).to receive(:now).and_return(now)
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
end end
describe "with lettings" do describe "with lettings" do
@ -17,12 +13,12 @@ RSpec.describe TasklistHelper do
describe "get next incomplete section" do describe "get next incomplete section" do
it "returns the first subsection name if it is not completed" do it "returns the first subsection name if it is not completed" do
expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") expect(get_next_incomplete_section(lettings_log).id).to eq("property_information")
end end
it "returns the first subsection name if it is partially completed" do it "returns the first subsection name if it is partially completed" do
lettings_log["tenancycode"] = 123 lettings_log["uprn_known"] = 0
expect(get_next_incomplete_section(lettings_log).id).to eq("household_characteristics") expect(get_next_incomplete_section(lettings_log).id).to eq("property_information")
end end
end end
@ -82,8 +78,9 @@ RSpec.describe TasklistHelper do
end end
describe "with sales" do describe "with sales" do
let(:now) { Time.utc(2022, 4, 4) }
let(:empty_sales_log) { create(:sales_log, owning_organisation: nil) } let(:empty_sales_log) { create(:sales_log, owning_organisation: nil) }
let(:completed_sales_log) { create(:sales_log, :completed) } let(:completed_sales_log) { create(:sales_log, :completed, saledate: now) }
describe "get sections count" do describe "get sections count" do
context "with an empty sales log" do context "with an empty sales log" do
@ -126,15 +123,13 @@ RSpec.describe TasklistHelper do
end end
context "when collection_period_open? == false" do context "when collection_period_open? == false" do
let(:now) { Time.utc(2022, 6, 1) } let(:now) { Time.utc(2024, 6, 1) }
let!(:sales_log) { create(:sales_log, :completed, saledate: now) } let!(:sales_log) { build(:sales_log, :completed, saledate: Time.utc(2022, 6, 1)) }
it "returns relevant text" do it "returns relevant text" do
Timecop.freeze(now + 1.year) do
expect(review_log_text(sales_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.") expect(review_log_text(sales_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.")
end end
end end
end
context "when older_than_previous_collection_year" do context "when older_than_previous_collection_year" do
let(:now) { Time.utc(2023, 6, 1) } let(:now) { Time.utc(2023, 6, 1) }
@ -148,39 +143,28 @@ RSpec.describe TasklistHelper do
context "with lettings log" do context "with lettings log" do
context "when collection_period_open? == true" do context "when collection_period_open? == true" do
context "with 2023 deadline" do
let(:now) { Time.utc(2022, 6, 1) } let(:now) { Time.utc(2022, 6, 1) }
let(:lettings_log) { create(:lettings_log, :completed) } let(:lettings_log) { build(:lettings_log, :completed, startdate: now, id: 123) }
it "returns relevant text" do before do
expect(review_log_text(lettings_log)).to eq( allow(lettings_log.form).to receive(:submission_deadline).and_return(Time.utc(2023, 6, 9))
"You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(lettings_log)} until 9 June 2023.".html_safe,
)
end
end end
context "with 2024 deadline" do
let(:now) { Time.utc(2023, 6, 20) }
let(:lettings_log) { create(:lettings_log, :completed, national: 18, waityear: 2) }
it "returns relevant text" do it "returns relevant text" do
expect(review_log_text(lettings_log)).to eq( expect(review_log_text(lettings_log)).to eq(
"You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(lettings_log)} until 7 June 2024.".html_safe, "You can #{govuk_link_to 'review and make changes to this log', review_lettings_log_path(lettings_log)} until 9 June 2023.".html_safe,
) )
end end
end end
end
context "when collection_period_open? == false" do context "when collection_period_open? == false" do
let(:now) { Time.utc(2022, 6, 1) } let(:now) { Time.utc(2024, 6, 1) }
let!(:sales_log) { create(:lettings_log, :completed) } let!(:sales_log) { build(:lettings_log, :completed, startdate: Time.utc(2022, 6, 1), id: 123) }
it "returns relevant text" do it "returns relevant text" do
Timecop.freeze(now + 1.year) do
expect(review_log_text(sales_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.") expect(review_log_text(sales_log)).to eq("This log is from the 2022/2023 collection window, which is now closed.")
end end
end end
end
context "when older_than_previous_collection_year" do context "when older_than_previous_collection_year" do
let(:now) { Time.utc(2023, 6, 1) } let(:now) { Time.utc(2023, 6, 1) }

Loading…
Cancel
Save