@ -1,100 +1,55 @@
require " rails_helper "
RSpec . describe TasklistHelper do
describe " with lettings " do
let ( :empty_lettings_log ) { FactoryBot . create ( :lettings_log ) }
let ( :lettings_log ) { FactoryBot . create ( :lettings_log , :in_progress , needstype : 1 ) }
let ( :fake_2021_2022_form ) { Form . new ( " spec/fixtures/forms/2021_2022.json " ) }
let ( :now ) { Time . utc ( 2022 , 6 , 1 ) }
context " with 2021 2022 form " do
before do
allow ( FormHandler . instance ) . to receive ( :current_lettings_form ) . and_return ( fake_2021_2022_form )
end
around do | example |
Timecop . freeze ( now ) do
Singleton . __init__ ( FormHandler )
example . run
end
Timecop . return
Singleton . __init__ ( FormHandler )
end
describe " get next incomplete section " 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 " )
end
describe " with lettings " do
let ( :empty_lettings_log ) { create ( :lettings_log ) }
let ( :lettings_log ) { build ( :lettings_log , :in_progress , needstype : 1 ) }
it " returns the first subsection name if it is partially completed " do
lettings_log [ " tenancycode " ] = 123
expect ( get_next_incomplete_section ( lettings_log ) . id ) . to eq ( " household_characteristics " )
end
describe " get next incomplete section " 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 " )
end
describe " get sections count " do
it " returns the total of sections if no status is given " do
expect ( get_subsections_count ( empty_lettings_log ) ) . to eq ( 8 )
end
it " returns 0 sections for completed sections if no sections are completed " do
expect ( get_subsections_count ( empty_lettings_log , :completed ) ) . to eq ( 0 )
end
it " returns the number of not started sections " do
expect ( get_subsections_count ( empty_lettings_log , :not_started ) ) . to eq ( 8 )
end
it " returns the number of sections in progress " do
expect ( get_subsections_count ( lettings_log , :in_progress ) ) . to eq ( 3 )
end
it " returns 0 for invalid state " do
expect ( get_subsections_count ( lettings_log , :fake ) ) . to eq ( 0 )
end
it " returns the first subsection name if it is partially completed " do
lettings_log [ " tenancycode " ] = 123
expect ( get_next_incomplete_section ( lettings_log ) . id ) . to eq ( " household_characteristics " )
end
end
describe " get_next_page_or_check_answers " do
let ( :subsection ) { lettings_log . form . get_subsection ( " household_characteristics " ) }
let ( :user ) { FactoryBot . build ( :user ) }
it " returns the check answers page path if the section has been started already " do
expect ( next_page_or_check_answers ( subsection , lettings_log , user ) ) . to match ( / check-answers / )
end
it " returns the first question page path for the section if it has not been started yet " do
expect ( next_page_or_check_answers ( subsection , empty_lettings_log , user ) ) . to match ( / tenant-code-test / )
end
it " when first question being not routed to returns the next routed question link " do
empty_lettings_log . housingneeds_a = " No "
expect ( next_page_or_check_answers ( subsection , empty_lettings_log , user ) ) . to match ( / person-1-gender / )
end
describe " get sections count " do
it " returns the total of sections if no status is given " do
expect ( get_subsections_count ( empty_lettings_log ) ) . to eq ( 1 )
end
describe " subsection link " do
let ( :subsection ) { lettings_log . form . get_subsection ( " household_characteristics " ) }
let ( :user ) { FactoryBot . build ( :user ) }
context " with a subsection that's enabled " do
it " returns the subsection link url " do
expect ( subsection_link ( subsection , lettings_log , user ) ) . to match ( / household-characteristics / )
end
end
it " returns 0 sections for completed sections if no sections are completed " do
expect ( get_subsections_count ( empty_lettings_log , :completed ) ) . to eq ( 0 )
end
context " with a subsection that cannot be started yet " do
before do
allow ( subsection ) . to receive ( :status ) . with ( lettings_log ) . and_return ( :cannot_start_yet )
end
it " returns the number of not started sections " do
expect ( get_subsections_count ( empty_lettings_log , :not_started ) ) . to eq ( 1 )
end
it " returns the label instead of a link " do
expect ( subsection_link ( subsection , lettings_log , user ) ) . to match ( subsection . label )
end
end
it " returns the number of sections in progress " do
expect ( get_subsections_count ( lettings_log , :in_progress ) ) . to eq ( 2 )
end
end
end
describe " # review_log_text " do
around do | example |
Timecop . freeze ( now ) do
Singleton . __init__ ( FormHandler )
example . run
it " returns 0 for invalid state " do
expect ( get_subsections_count ( lettings_log , :fake ) ) . to eq ( 0 )
end
Singleton . __init__ ( FormHandler )
end
context " with lettings log " do
describe " review_log_text " do
context " when collection_period_open? == true " do
context " with 2023 deadline " do
let ( :now ) { Time . utc ( 2022 , 6 , 1 ) }
@ -129,6 +84,30 @@ RSpec.describe TasklistHelper do
end
end
describe " subsection link " do
let ( :lettings_log ) { create ( :lettings_log , :completed ) }
let ( :subsection ) { lettings_log . form . get_subsection ( " household_characteristics " ) }
let ( :user ) { build ( :user ) }
context " with a subsection that's enabled " do
it " returns the subsection link url " do
expect ( subsection_link ( subsection , lettings_log , user ) ) . to match ( / household-characteristics / )
end
end
context " with a subsection that cannot be started yet " do
before do
allow ( subsection ) . to receive ( :status ) . with ( lettings_log ) . and_return ( :cannot_start_yet )
end
it " returns the label instead of a link " do
expect ( subsection_link ( subsection , lettings_log , user ) ) . to match ( subsection . label )
end
end
end
end
describe " # review_log_text " do
context " with sales log " do
context " when collection_period_open? == true " do
let ( :now ) { Time . utc ( 2022 , 6 , 1 ) }
@ -142,11 +121,13 @@ RSpec.describe TasklistHelper do
end
context " when collection_period_open? == false " do
let ( :now ) { Time . utc ( 2023 , 7 , 8 ) }
let ( :sales_log ) { create ( :sales_log , :completed , saledate : Time . utc ( 2023 , 2 , 8 ) ) }
let ( :now ) { Time . utc ( 2022 , 6 , 1 ) }
let! ( :sales_log ) { create ( :sales_log , :completed ) }
it " returns relevant text " do
expect ( review_log_text ( sales_log ) ) . to eq ( " This log is from the 2022/2023 collection window, which is now closed. " )
Timecop . freeze ( now + 1 . year ) do
expect ( review_log_text ( sales_log ) ) . to eq ( " This log is from the 2021/2022 collection window, which is now closed. " )
end
end
end
end