From a137999d294585a6b8cd1995556619326709da45 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 3 Dec 2024 08:19:59 +0000 Subject: [PATCH] Add addresses flow feature tests and update the flow a bit (#2805) --- .../lettings_log_variables.rb | 9 + .../derived_variables/sales_log_variables.rb | 9 + app/models/log.rb | 4 +- spec/features/lettings_log_spec.rb | 348 ++++++++++++++++++ spec/features/sales_log_spec.rb | 348 ++++++++++++++++++ 5 files changed, 717 insertions(+), 1 deletion(-) diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 4692a0e6b..ced530b17 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -84,6 +84,15 @@ module DerivedVariables::LettingsLogVariables if uprn_known&.zero? self.uprn = nil + if uprn_known_was == 1 + self.address_line1 = nil + self.address_line2 = nil + self.town_or_city = nil + self.county = nil + self.postcode_known = nil + self.postcode_full = nil + self.la = nil + end end if uprn_known == 1 && uprn_confirmed&.zero? diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 4f4c3105d..6e12ec488 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -53,6 +53,15 @@ module DerivedVariables::SalesLogVariables if uprn_known&.zero? self.uprn = nil + if uprn_known_was == 1 + self.address_line1 = nil + self.address_line2 = nil + self.town_or_city = nil + self.county = nil + self.pcodenk = nil + self.postcode_full = nil + self.la = nil + end end if uprn_known == 1 && uprn_confirmed&.zero? diff --git a/app/models/log.rb b/app/models/log.rb index dd4301550..bcbea9c92 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -126,9 +126,11 @@ class Log < ApplicationRecord end def address_options - return @address_options if @address_options + return @address_options if @address_options && @last_searched_address_string == address_string if [address_line1_input, postcode_full_input].all?(&:present?) + @last_searched_address_string = address_string + service = AddressClient.new(address_string) service.call if service.result.blank? || service.error.present? diff --git a/spec/features/lettings_log_spec.rb b/spec/features/lettings_log_spec.rb index efb7e7665..b10dfc6e5 100644 --- a/spec/features/lettings_log_spec.rb +++ b/spec/features/lettings_log_spec.rb @@ -729,5 +729,353 @@ RSpec.describe "Lettings Log Features" do expect(duplicate_log.duplicate_set_id).to be_nil end end + + context "when filling out address fields" do + let(:lettings_log) { create(:lettings_log, :setup_completed, assigned_to: user) } + + before do + body = { + results: [ + { + DPA: { + "POSTCODE": "AA1 1AA", + "POST_TOWN": "Bristol", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=111") + .to_return(status: 200, body:, headers: {}) + + body = { results: [{ DPA: { UPRN: "111" } }] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?query=Address+line+1%2C+AA1+1AA&key=OS_DATA_KEY&maxresults=10&minmatch=0.4") + .to_return(status: 200, body:, headers: {}) + + WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA") + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) + + WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA12AA") + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 2AA\",\"admin_district\":\"Wigan\",\"codes\":{\"admin_district\":\"E08000010\"}}}", headers: {}) + + body = { results: [] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?query=Address+line+1%2C+AA1+1AB&key=OS_DATA_KEY&maxresults=10&minmatch=0.4") + .to_return(status: 200, body:, headers: {}) + + visit("/lettings-logs/#{lettings_log.id}/uprn") + end + + context "and uprn is known and answered" do + before do + choose "Yes" + fill_in("lettings_log[uprn]", with: "111") + click_button("Save and continue") + end + + context "and uprn is confirmed" do + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(1) # yes + expect(lettings_log.uprn).to eq("111") + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Some Place") + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq("Bristol") + expect(lettings_log.address_line1_input).to eq(nil) + expect(lettings_log.postcode_full_input).to eq(nil) + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + + choose "Yes" + click_button("Save and continue") + + lettings_log.reload + expect(lettings_log.uprn_known).to eq(1) # yes + expect(lettings_log.uprn).to eq("111") + expect(lettings_log.uprn_confirmed).to eq(1) # yes + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Some Place") + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq("Bristol") + expect(lettings_log.address_line1_input).to eq(nil) + expect(lettings_log.postcode_full_input).to eq(nil) + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + end + + context "and changes to uprn not known" do + it "sets correct address fields" do + visit("/lettings-logs/#{lettings_log.id}/uprn") + + choose "No" + click_button("Save and continue") + + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(nil) + expect(lettings_log.postcode_full).to eq(nil) + expect(lettings_log.address_line1).to eq(nil) + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq(nil) + expect(lettings_log.postcode_full_input).to eq(nil) + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq(nil) + end + end + end + + context "and uprn is not confirmed" do + before do + choose "No, I want to search for the address instead" + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(nil) + expect(lettings_log.postcode_full).to eq(nil) + expect(lettings_log.address_line1).to eq(nil) + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq(nil) + expect(lettings_log.postcode_full_input).to eq(nil) + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq(nil) + end + end + end + + context "and uprn is not known" do + before do + choose "No" + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(nil) + expect(lettings_log.postcode_full).to eq(nil) + expect(lettings_log.address_line1).to eq(nil) + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq(nil) + expect(lettings_log.postcode_full_input).to eq(nil) + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq(nil) + end + + context "and the address is not found" do + it "sets correct address fields" do + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AB") + click_button("Search") + + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(nil) + expect(lettings_log.postcode_full).to eq(nil) + expect(lettings_log.address_line1).to eq(nil) + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AB") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq(nil) + + click_button("Confirm and continue") + + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(nil) + expect(lettings_log.postcode_full).to eq(nil) + expect(lettings_log.address_line1).to eq(nil) + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AB") + expect(lettings_log.address_search_value_check).to eq(0) + expect(lettings_log.la).to eq(nil) + end + end + + context "and address is found, re-searched and not found" do + before do + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + visit("/lettings-logs/#{lettings_log.id}/address-matcher") + + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AB") + click_button("Search") + end + + it "routes to the correct page" do + expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/no-address-found") + end + end + + context "and the user selects 'address_not_listed'" do + before do + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose "The address is not listed, I want to enter the address manually" + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq("uprn_not_listed") + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Address line 1") + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq(nil) + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AA") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + end + + context "and the user enters a new address manually" do + context "without changing a valid postcode" do + before do + fill_in("lettings_log[town_or_city]", with: "Town") + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq("uprn_not_listed") + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Address line 1") + expect(lettings_log.address_line2).to eq("") + expect(lettings_log.town_or_city).to eq("Town") + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AA") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + end + end + + context "with changing the postcode" do + before do + fill_in("lettings_log[town_or_city]", with: "Town") + fill_in("lettings_log[postcode_full]", with: "AA12AA") + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(0) # no + expect(lettings_log.uprn).to eq(nil) + expect(lettings_log.uprn_confirmed).to eq(nil) + expect(lettings_log.uprn_selection).to eq("uprn_not_listed") + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 2AA") + expect(lettings_log.address_line1).to eq("Address line 1") + expect(lettings_log.address_line2).to eq("") + expect(lettings_log.town_or_city).to eq("Town") + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AA") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E08000010") + end + end + end + end + + context "and the user selects 'address_not_listed' and then changes their mind and selects an address" do + before do + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose "The address is not listed, I want to enter the address manually" + click_button("Save and continue") + + visit("/lettings-logs/#{lettings_log.id}/uprn-selection") + choose("lettings-log-uprn-selection-111-field", allow_label_click: true) + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(1) + expect(lettings_log.uprn).to eq("111") + expect(lettings_log.uprn_confirmed).to eq(1) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Some Place") + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq("Bristol") + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AA") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + end + end + + context "and possible addresses found and selected" do + before do + fill_in("lettings_log[address_line1_input]", with: "Address line 1") + fill_in("lettings_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose("lettings-log-uprn-selection-111-field", allow_label_click: true) + click_button("Save and continue") + end + + it "sets correct address fields" do + lettings_log.reload + expect(lettings_log.uprn_known).to eq(1) + expect(lettings_log.uprn).to eq("111") + expect(lettings_log.uprn_confirmed).to eq(1) + expect(lettings_log.uprn_selection).to eq(nil) + expect(lettings_log.postcode_known).to eq(1) + expect(lettings_log.postcode_full).to eq("AA1 1AA") + expect(lettings_log.address_line1).to eq("Some Place") + expect(lettings_log.address_line2).to eq(nil) + expect(lettings_log.town_or_city).to eq("Bristol") + expect(lettings_log.address_line1_input).to eq("Address line 1") + expect(lettings_log.postcode_full_input).to eq("AA1 1AA") + expect(lettings_log.address_search_value_check).to eq(nil) + expect(lettings_log.la).to eq("E09000033") + end + end + end + end end end diff --git a/spec/features/sales_log_spec.rb b/spec/features/sales_log_spec.rb index 879f2b5c8..d418bcb37 100644 --- a/spec/features/sales_log_spec.rb +++ b/spec/features/sales_log_spec.rb @@ -310,6 +310,354 @@ RSpec.describe "Sales Log Features" do expect(page).to have_current_path("/sales-logs/bulk-uploads") end end + + context "when filling out address fields" do + let(:sales_log) { create(:sales_log, :shared_ownership_setup_complete, assigned_to: user) } + + before do + body = { + results: [ + { + DPA: { + "POSTCODE": "AA1 1AA", + "POST_TOWN": "Bristol", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=111") + .to_return(status: 200, body:, headers: {}) + + body = { results: [{ DPA: { UPRN: "111" } }] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?query=Address+line+1%2C+AA1+1AA&key=OS_DATA_KEY&maxresults=10&minmatch=0.4") + .to_return(status: 200, body:, headers: {}) + + WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA11AA") + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 1AA\",\"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {}) + + WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/AA12AA") + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"AA1 2AA\",\"admin_district\":\"Wigan\",\"codes\":{\"admin_district\":\"E08000010\"}}}", headers: {}) + + body = { results: [] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?query=Address+line+1%2C+AA1+1AB&key=OS_DATA_KEY&maxresults=10&minmatch=0.4") + .to_return(status: 200, body:, headers: {}) + + visit("/sales-logs/#{sales_log.id}/uprn") + end + + context "and uprn is known and answered" do + before do + choose "Yes" + fill_in("sales_log[uprn]", with: "111") + click_button("Save and continue") + end + + context "and uprn is confirmed" do + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(1) # yes + expect(sales_log.uprn).to eq("111") + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Some Place") + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq("Bristol") + expect(sales_log.address_line1_input).to eq(nil) + expect(sales_log.postcode_full_input).to eq(nil) + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + + choose "Yes" + click_button("Save and continue") + + sales_log.reload + expect(sales_log.uprn_known).to eq(1) # yes + expect(sales_log.uprn).to eq("111") + expect(sales_log.uprn_confirmed).to eq(1) # yes + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Some Place") + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq("Bristol") + expect(sales_log.address_line1_input).to eq(nil) + expect(sales_log.postcode_full_input).to eq(nil) + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + end + + context "and changes to uprn not known" do + it "sets correct address fields" do + visit("/sales-logs/#{sales_log.id}/uprn") + + choose "No" + click_button("Save and continue") + + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(nil) + expect(sales_log.postcode_full).to eq(nil) + expect(sales_log.address_line1).to eq(nil) + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq(nil) + expect(sales_log.postcode_full_input).to eq(nil) + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq(nil) + end + end + end + + context "and uprn is not confirmed" do + before do + choose "No, I want to search for the address instead" + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(nil) + expect(sales_log.postcode_full).to eq(nil) + expect(sales_log.address_line1).to eq(nil) + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq(nil) + expect(sales_log.postcode_full_input).to eq(nil) + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq(nil) + end + end + end + + context "and uprn is not known" do + before do + choose "No" + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(nil) + expect(sales_log.postcode_full).to eq(nil) + expect(sales_log.address_line1).to eq(nil) + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq(nil) + expect(sales_log.postcode_full_input).to eq(nil) + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq(nil) + end + + context "and the address is not found" do + it "sets correct address fields" do + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AB") + click_button("Search") + + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(nil) + expect(sales_log.postcode_full).to eq(nil) + expect(sales_log.address_line1).to eq(nil) + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AB") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq(nil) + + click_button("Confirm and continue") + + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(nil) + expect(sales_log.postcode_full).to eq(nil) + expect(sales_log.address_line1).to eq(nil) + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AB") + expect(sales_log.address_search_value_check).to eq(0) + expect(sales_log.la).to eq(nil) + end + end + + context "and address is found, re-searched and not found" do + before do + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + visit("/sales-logs/#{sales_log.id}/address-matcher") + + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AB") + click_button("Search") + end + + it "routes to the correct page" do + expect(page).to have_current_path("/sales-logs/#{sales_log.id}/no-address-found") + end + end + + context "and the user selects 'address_not_listed'" do + before do + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose "The address is not listed, I want to enter the address manually" + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq("uprn_not_listed") + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Address line 1") + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq(nil) + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AA") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + end + + context "and the user enters a new address manually" do + context "without changing a valid postcode" do + before do + fill_in("sales_log[town_or_city]", with: "Town") + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq("uprn_not_listed") + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Address line 1") + expect(sales_log.address_line2).to eq("") + expect(sales_log.town_or_city).to eq("Town") + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AA") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + end + end + + context "with changing the postcode" do + before do + fill_in("sales_log[town_or_city]", with: "Town") + fill_in("sales_log[postcode_full]", with: "AA12AA") + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(0) # no + expect(sales_log.uprn).to eq(nil) + expect(sales_log.uprn_confirmed).to eq(nil) + expect(sales_log.uprn_selection).to eq("uprn_not_listed") + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 2AA") + expect(sales_log.address_line1).to eq("Address line 1") + expect(sales_log.address_line2).to eq("") + expect(sales_log.town_or_city).to eq("Town") + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AA") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E08000010") + end + end + end + end + + context "and the user selects 'address_not_listed' and then changes their mind and selects an address" do + before do + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose "The address is not listed, I want to enter the address manually" + click_button("Save and continue") + + visit("/sales-logs/#{sales_log.id}/uprn-selection") + choose("sales-log-uprn-selection-111-field", allow_label_click: true) + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(1) + expect(sales_log.uprn).to eq("111") + expect(sales_log.uprn_confirmed).to eq(1) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Some Place") + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq("Bristol") + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AA") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + end + end + + context "and possible addresses found and selected" do + before do + fill_in("sales_log[address_line1_input]", with: "Address line 1") + fill_in("sales_log[postcode_full_input]", with: "AA1 1AA") + click_button("Search") + choose("sales-log-uprn-selection-111-field", allow_label_click: true) + click_button("Save and continue") + end + + it "sets correct address fields" do + sales_log.reload + expect(sales_log.uprn_known).to eq(1) + expect(sales_log.uprn).to eq("111") + expect(sales_log.uprn_confirmed).to eq(1) + expect(sales_log.uprn_selection).to eq(nil) + expect(sales_log.pcodenk).to eq(0) + expect(sales_log.postcode_full).to eq("AA1 1AA") + expect(sales_log.address_line1).to eq("Some Place") + expect(sales_log.address_line2).to eq(nil) + expect(sales_log.town_or_city).to eq("Bristol") + expect(sales_log.address_line1_input).to eq("Address line 1") + expect(sales_log.postcode_full_input).to eq("AA1 1AA") + expect(sales_log.address_search_value_check).to eq(nil) + expect(sales_log.la).to eq("E09000033") + end + end + end + end end context "when a log becomes a duplicate" do