Browse Source

feat: fix lettings_logs_controller_spec.rb tests

pull/1318/head
natdeanlewissoftwire 2 years ago
parent
commit
bc9f2f13ef
  1. 2
      spec/factories/lettings_log.rb
  2. 11
      spec/features/form/validations_spec.rb
  3. 117
      spec/requests/lettings_logs_controller_spec.rb

2
spec/factories/lettings_log.rb

@ -18,7 +18,7 @@ FactoryBot.define do
age2 { 19 } age2 { 19 }
renewal { 1 } renewal { 1 }
rent_type { 1 } rent_type { 1 }
startdate { Time.zone.local(2021, 5, 1) } startdate { Time.zone.local(2022, 5, 1) }
end end
trait :soft_validations_triggered do trait :soft_validations_triggered do
status { 1 } status { 1 }

11
spec/features/form/validations_spec.rb

@ -6,7 +6,7 @@ RSpec.describe "validations" do
allow(Time.zone).to receive(:now).and_return(Time.zone.local(2022, 5, 1)) allow(Time.zone).to receive(:now).and_return(Time.zone.local(2022, 5, 1))
end end
context "tests" do context "with logs" do
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:lettings_log) do let(:lettings_log) do
@ -15,13 +15,13 @@ RSpec.describe "validations" do
:in_progress, :in_progress,
created_by: user, created_by: user,
renewal: 0, renewal: 0,
) )
end end
let(:empty_lettings_log) do let(:empty_lettings_log) do
FactoryBot.create( FactoryBot.create(
:lettings_log, :lettings_log,
created_by: user, created_by: user,
) )
end end
let(:completed_without_declaration) do let(:completed_without_declaration) do
FactoryBot.create( FactoryBot.create(
@ -31,7 +31,7 @@ RSpec.describe "validations" do
status: 1, status: 1,
declaration: nil, declaration: nil,
startdate: Time.zone.local(2021, 5, 1), startdate: Time.zone.local(2021, 5, 1),
) )
end end
let(:id) { lettings_log.id } let(:id) { lettings_log.id }
@ -127,7 +127,7 @@ RSpec.describe "validations" do
:in_progress, :in_progress,
ecstat1: 1, ecstat1: 1,
created_by: user, created_by: user,
) )
end end
let(:income_over_soft_limit) { 750 } let(:income_over_soft_limit) { 750 }
let(:income_under_soft_limit) { 700 } let(:income_under_soft_limit) { 700 }
@ -168,5 +168,4 @@ RSpec.describe "validations" do
end end
end end
end end
end end

117
spec/requests/lettings_logs_controller_spec.rb

@ -323,8 +323,7 @@ RSpec.describe LettingsLogsController, type: :request do
context "with year filter" do context "with year filter" do
let!(:lettings_log_2021) do let!(:lettings_log_2021) do
FactoryBot.create(:lettings_log, :in_progress, FactoryBot.create(:lettings_log, :in_progress,
created_by: user, created_by: user)
startdate: Time.zone.local(2022, 3, 1))
end end
let!(:lettings_log_2022) do let!(:lettings_log_2022) do
lettings_log = FactoryBot.build(:lettings_log, :completed, lettings_log = FactoryBot.build(:lettings_log, :completed,
@ -337,16 +336,24 @@ RSpec.describe LettingsLogsController, type: :request do
lettings_log lettings_log
end end
it "shows lettings logs for multiple selected years" do context "with previous logs" do
get "/lettings-logs?years[]=2021&years[]=2022", headers: headers, params: {} before do
expect(page).to have_link(lettings_log_2021.id.to_s) Timecop.freeze(Time.zone.local(2022, 3, 1))
expect(page).to have_link(lettings_log_2022.id.to_s) lettings_log_2021.update!(startdate: Time.zone.local(2022, 3, 1))
end Timecop.unfreeze
end
it "shows lettings logs for one selected year" do it "shows lettings logs for multiple selected years" do
get "/lettings-logs?years[]=2021", headers: headers, params: {} get "/lettings-logs?years[]=2021&years[]=2022", headers: headers, params: {}
expect(page).to have_link(lettings_log_2021.id.to_s) expect(page).to have_link(lettings_log_2021.id.to_s)
expect(page).not_to have_link(lettings_log_2022.id.to_s) expect(page).to have_link(lettings_log_2022.id.to_s)
end
it "shows lettings logs for one selected year" do
get "/lettings-logs?years[]=2021", headers: headers, params: {}
expect(page).to have_link(lettings_log_2021.id.to_s)
expect(page).not_to have_link(lettings_log_2022.id.to_s)
end
end end
end end
@ -362,7 +369,6 @@ RSpec.describe LettingsLogsController, type: :request do
let!(:lettings_log_2021) do let!(:lettings_log_2021) do
FactoryBot.create(:lettings_log, :in_progress, FactoryBot.create(:lettings_log, :in_progress,
owning_organisation: organisation, owning_organisation: organisation,
startdate: Time.zone.local(2022, 3, 1),
managing_organisation: organisation, managing_organisation: organisation,
created_by: user) created_by: user)
end end
@ -386,18 +392,26 @@ RSpec.describe LettingsLogsController, type: :request do
created_by: user) created_by: user)
end end
it "shows lettings logs for multiple selected statuses and years" do context "with previous logs" do
get "/lettings-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {} before do
expect(page).to have_link(lettings_log_2021.id.to_s) Timecop.freeze(Time.zone.local(2022, 3, 1))
expect(page).to have_link(lettings_log_2022.id.to_s) lettings_log_2021.update!(startdate: Time.zone.local(2022, 3, 1))
expect(page).to have_link(lettings_log_2022_in_progress.id.to_s) Timecop.unfreeze
end end
it "shows lettings logs for one selected status" do it "shows lettings logs for multiple selected statuses and years" do
get "/lettings-logs?years[]=2022&status[]=in_progress", headers: headers, params: {} get "/lettings-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {}
expect(page).to have_link(lettings_log_2022_in_progress.id.to_s) expect(page).to have_link(lettings_log_2021.id.to_s)
expect(page).not_to have_link(lettings_log_2021.id.to_s) expect(page).to have_link(lettings_log_2022.id.to_s)
expect(page).not_to have_link(lettings_log_2022.id.to_s) expect(page).to have_link(lettings_log_2022_in_progress.id.to_s)
end
it "shows lettings logs for one selected status" do
get "/lettings-logs?years[]=2022&status[]=in_progress", headers: headers, params: {}
expect(page).to have_link(lettings_log_2022_in_progress.id.to_s)
expect(page).not_to have_link(lettings_log_2021.id.to_s)
expect(page).not_to have_link(lettings_log_2022.id.to_s)
end
end end
end end
@ -843,14 +857,21 @@ RSpec.describe LettingsLogsController, type: :request do
expect(page).to have_link("review and make changes to this log", href: "/lettings-logs/#{completed_lettings_log.id}/review") expect(page).to have_link("review and make changes to this log", href: "/lettings-logs/#{completed_lettings_log.id}/review")
end end
it "displays a closed collection window message for previous collection year logs" do context "with past lettings logs" do
completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1)) before do
completed_lettings_log.reload Timecop.freeze(Time.zone.local(2021, 4, 1))
completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1))
Timecop.unfreeze
end
get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {} it "displays a closed collection window message for previous collection year logs" do
expect(completed_lettings_log.form.end_date).to eq(Time.zone.local(2022, 7, 1)) completed_lettings_log.reload
expect(completed_lettings_log.status).to eq("completed")
expect(page).to have_content("This log is from the 2021/2022 collection window, which is now closed.") get "/lettings-logs/#{completed_lettings_log.id}", headers:, params: {}
expect(completed_lettings_log.form.end_date).to eq(Time.zone.local(2022, 7, 1))
expect(completed_lettings_log.status).to eq("completed")
expect(page).to have_content("This log is from the 2021/2022 collection window, which is now closed.")
end
end end
end end
@ -919,7 +940,7 @@ RSpec.describe LettingsLogsController, type: :request do
postcode_known: "No") postcode_known: "No")
end end
let(:id) { postcode_lettings_log.id } let(:id) { postcode_lettings_log.id }
let(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed, owning_organisation: user.organisation, managing_organisation: user.organisation, created_by: user, startdate: Time.zone.local(2021, 4, 1)) } let(:completed_lettings_log) { FactoryBot.create(:lettings_log, :completed, owning_organisation: user.organisation, managing_organisation: user.organisation, created_by: user) }
before do before do
stub_request(:get, /api.postcodes.io/) stub_request(:get, /api.postcodes.io/)
@ -953,19 +974,27 @@ RSpec.describe LettingsLogsController, type: :request do
expect(CGI.unescape_html(response.body)).to include("You didn’t answer this question") expect(CGI.unescape_html(response.body)).to include("You didn’t answer this question")
end end
it "does not allow you to change the answers for previous collection year logs" do context "with previous logs" do
get "/lettings-logs/#{completed_lettings_log.id}/setup/check-answers", headers: { "Accept" => "text/html" }, params: {} before do
expect(page).not_to have_link("Change") Timecop.freeze(Time.zone.local(2021, 4, 1))
expect(page).not_to have_link("Answer") completed_lettings_log.update!(startdate: Time.zone.local(2021, 4, 1))
Timecop.unfreeze
end
get "/lettings-logs/#{completed_lettings_log.id}/income-and-benefits/check-answers", headers: { "Accept" => "text/html" }, params: {} it "does not allow you to change the answers for previous collection year logs" do
expect(page).not_to have_link("Change") get "/lettings-logs/#{completed_lettings_log.id}/setup/check-answers", headers: { "Accept" => "text/html" }, params: {}
expect(page).not_to have_link("Answer") expect(page).not_to have_link("Change")
end expect(page).not_to have_link("Answer")
it "does not let the user navigate to questions for previous collection year logs" do get "/lettings-logs/#{completed_lettings_log.id}/income-and-benefits/check-answers", headers: { "Accept" => "text/html" }, params: {}
get "/lettings-logs/#{completed_lettings_log.id}/needs-type", headers: { "Accept" => "text/html" }, params: {} expect(page).not_to have_link("Change")
expect(response).to redirect_to("/lettings-logs/#{completed_lettings_log.id}") expect(page).not_to have_link("Answer")
end
it "does not let the user navigate to questions for previous collection year logs" do
get "/lettings-logs/#{completed_lettings_log.id}/needs-type", headers: { "Accept" => "text/html" }, params: {}
expect(response).to redirect_to("/lettings-logs/#{completed_lettings_log.id}")
end
end end
end end
@ -1127,7 +1156,7 @@ RSpec.describe LettingsLogsController, type: :request do
end end
context "with an invalid lettings log params" do context "with an invalid lettings log params" do
let(:params) { { age1: 200 } } let(:params) { { beds: 200 } }
it "returns 422" do it "returns 422" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -1135,7 +1164,7 @@ RSpec.describe LettingsLogsController, type: :request do
it "returns an error message" do it "returns an error message" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["errors"]).to eq({ "age1" => ["Lead tenant’s age must be between 16 and 120"] }) expect(json_response["errors"]).to eq({ "beds" => ["Number of bedrooms cannot be more than 12"] })
end end
end end

Loading…
Cancel
Save