Browse Source

Update test suite

CLDC-1468-add-postcode-to-sales-property-info-journey
Mo Seedat 2 years ago
parent
commit
e2c7ac01f9
  1. 2
      app/models/form/sales/questions/postcode_full.rb
  2. 16
      app/models/sales_log.rb
  3. 8
      db/schema.rb
  4. 15
      spec/factories/sales_log.rb
  5. 121
      spec/features/sales_log_spec.rb
  6. 29
      spec/models/form/sales/pages/property_postcode_spec.rb
  7. 33
      spec/models/form/sales/questions/postcode_full_spec.rb
  8. 42
      spec/models/form/sales/questions/property_postcode_spec.rb
  9. 29
      spec/models/form/sales/subsections/property_postcode_spec.rb
  10. 2
      spec/request_helper.rb

2
app/models/form/sales/questions/postcode_full.rb

@ -2,7 +2,7 @@ class Form::Sales::Questions::PostcodeFull < ::Form::Question
def initialize(id, hsh, page)
super
@id = "postcode_full"
@check_answer_label = "Property postcode"
@check_answer_label = "Postcode"
@header = "What is the property's postcode?"
@type = "text"
@page = page

16
app/models/sales_log.rb

@ -61,9 +61,12 @@ class SalesLog < Log
# if the Postcode service timesout/unavailable then still
# need to proceed
def process_postcode_changes!
return if postcode_full.blank?
if postcode_full.blank?
reset_postcode_fields
return
end
self.pcodenk = nil
self.pcodenk = false
self.postcode_known = 1
if postcode_lookup&.result?
@ -79,6 +82,15 @@ class SalesLog < Log
end
end
def reset_postcode_fields
self.pcodenk = true
self.postcode_known = 0
self.pcode1 = nil
self.pcode2 = nil
self.la = nil
self.la_known = 0
end
def postcode_lookup
@postcode_lookup ||= PostcodeService.new.lookup(postcode_full)
end

8
db/schema.rb

@ -359,16 +359,16 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_19_082625) do
t.integer "hholdcount"
t.integer "age3"
t.integer "age3_known"
t.string "la"
t.integer "la_known"
t.integer "income1"
t.integer "income1nk"
t.integer "age4"
t.integer "age4_known"
t.integer "age5"
t.integer "age5_known"
t.integer "age6"
t.integer "age6_known"
t.string "la"
t.integer "la_known"
t.integer "income1"
t.integer "income1nk"
t.string "postcode_full"
t.string "pcode1"
t.string "pcode2"

15
spec/factories/sales_log.rb

@ -5,12 +5,25 @@ FactoryBot.define do
managing_organisation { created_by.organisation }
created_at { Time.utc(2022, 2, 8, 16, 52, 15) }
updated_at { Time.utc(2022, 2, 8, 16, 52, 15) }
trait :in_progress do
purchid { "PC123" }
ownershipsch { 2 }
type { 8 }
saledate { Time.utc(2022, 2, 2, 10, 36, 49) }
end
trait :before_you_start_completed do
saledate { Time.utc(2022, 2, 2, 10, 36, 49) }
purchid { "PC123" }
ownershipsch { 3 }
type { 10 }
companybuy { 2 }
jointpur { 1 }
buylivein { "1" }
jointmore { 1 }
end
trait :completed do
purchid { "PC123" }
ownershipsch { 2 }
@ -55,7 +68,7 @@ FactoryBot.define do
pcode1 { "SW9" }
pcode2 { "8LA" }
postcode_known { 1 }
pcodenk { nil }
pcodenk { false }
end
end
end

121
spec/features/sales_log_spec.rb

@ -3,16 +3,13 @@ require "rails_helper"
RSpec.describe "Sales Log Features" do
context "when searching for specific sales logs" do
context "when I am signed in and there are sales logs in the database" do
let(:user) { FactoryBot.create(:user, last_sign_in_at: Time.zone.now) }
let!(:user) { FactoryBot.create(:user, last_sign_in_at: Time.zone.now) }
let!(:log_to_search) { FactoryBot.create(:sales_log, owning_organisation: user.organisation) }
let!(:same_organisation_log) { FactoryBot.create(:sales_log, owning_organisation: user.organisation) }
let!(:another_organisation_log) { FactoryBot.create(:sales_log) }
before do
visit("/sales-logs")
fill_in("user[email]", with: user.email)
fill_in("user[password]", with: user.password)
click_button("Sign in")
login(user)
end
it "displays the logs belonging to the same organisation" do
@ -28,12 +25,12 @@ RSpec.describe "Sales Log Features" do
click_link("Logs")
end
it "navigates you to the lettings logs page" do
it "navigates you to the sales logs page" do
expect(page).to have_current_path("/sales-logs")
end
end
context "when completing the setup sales log section" do
context "when completing the setup this sales log section" do
it "includes the purchaser code and sale completion date questions" do
visit("/sales-logs")
click_button("Create a new sales log")
@ -65,4 +62,114 @@ RSpec.describe "Sales Log Features" do
end
end
end
describe "property information section" do
let!(:user) { FactoryBot.create(:user, last_sign_in_at: Time.zone.now) }
let!(:sales_log) do
FactoryBot.create(
:sales_log,
:before_you_start_completed,
created_by: user,
owning_organisation: user.organisation,
)
end
before do
login(user)
end
# Question 14 Property Postcode
context "when postcode is known then not known then known again" do
it "sets correct log state", js: true do
expect(sales_log.postcode_full).to be nil
expect(sales_log.postcode_known).to be nil
expect(sales_log.pcode1).to be nil
expect(sales_log.pcode2).to be nil
expect(sales_log.pcodenk).to be true
expect(sales_log.la).to be nil
expect(sales_log.la_known).to be nil
visit("/sales-logs/#{sales_log.id}/postcode-known")
# 1. User forgot to set postcode
expect(page).to have_content("Do you know the property's postcode?")
choose("Yes", allow_label_click: true)
click_button("Save and continue")
expect(page).to have_content("You must answer postcode")
# 2. User enters valid postcode
choose("Yes", allow_label_click: true)
click_button("Save and continue")
fill_in("sales_log[postcode_full]", with: "AA1 1AA")
click_button("Save and continue")
sales_log.reload
expect(sales_log.postcode_full).to eq "AA1 1AA"
expect(sales_log.postcode_known).to eq 1
expect(sales_log.pcode1).to eq "AA1"
expect(sales_log.pcode2).to eq "1AA"
expect(sales_log.pcodenk).to be false
expect(sales_log.la).to eq "Westminster"
expect(sales_log.la_known).to be 1
# 3. User decides they don't know the postcode
visit("/sales-logs/#{sales_log.id}/postcode-known")
choose("No", allow_label_click: true)
click_button("Save and continue")
sales_log.reload
expect(sales_log.postcode_full).to be nil
expect(sales_log.postcode_known).to eq 0
expect(sales_log.pcode1).to be nil
expect(sales_log.pcode2).to be nil
expect(sales_log.pcodenk).to be true
expect(sales_log.la).to be nil
expect(sales_log.la_known).to be 0
# 4. User decides they do know the password
visit("/sales-logs/#{sales_log.id}/postcode-known")
choose("Yes", allow_label_click: true)
fill_in("sales_log[postcode_full]", with: "SW9 8LA")
click_button("Save and continue")
sales_log.reload
expect(sales_log.postcode_full).to eq "SW9 8LA"
expect(sales_log.postcode_known).to eq 1
expect(sales_log.pcode1).to eq "SW9"
expect(sales_log.pcode2).to eq "8LA"
expect(sales_log.pcodenk).to be false
expect(sales_log.la).to eq "Lambeth"
expect(sales_log.la_known).to be 1
end
end
# E.g. Postcode.io service unreachable or timed out
# Note: B26 3QJ is not defined in request_helper.rb
context "when postcode is known but not found by postocde.io" do
it "sets correct log state", js: true do
visit("/sales-logs/#{sales_log.id}/postcode-known")
choose("Yes", allow_label_click: true)
click_button("Save and continue")
fill_in("sales_log[postcode_full]", with: "B26 3QJ")
click_button("Save and continue")
sales_log.reload
expect(sales_log.postcode_full).to eq "B26 3QJ"
expect(sales_log.postcode_known).to eq 1
expect(sales_log.pcode1).to be nil
expect(sales_log.pcode2).to be nil
expect(sales_log.pcodenk).to be false
expect(sales_log.la).to be nil
expect(sales_log.la_known).to be 0
end
end
end
def login(user)
visit("/sales-logs")
fill_in("user[email]", with: user.email)
fill_in("user[password]", with: user.password)
click_button("Sign in")
end
end

29
spec/models/form/sales/pages/property_postcode_spec.rb

@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe Form::Sales::Pages::PropertyPostcode, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:page_id) { nil }
let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection) }
it "has correct subsection" do
expect(page.subsection).to eq(subsection)
end
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[postcode_known postcode_full])
end
it "has the correct id" do
expect(page.id).to eq("postcode_known")
end
it "has the correct header" do
expect(page.header).to eq("")
end
it "has the correct description" do
expect(page.description).to eq("")
end
end

33
spec/models/form/sales/questions/postcode_full_spec.rb

@ -0,0 +1,33 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::PostcodeFull, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { nil }
let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) }
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("postcode_full")
end
it "has the correct header" do
expect(question.header).to eq("What is the property's postcode?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Postcode")
end
it "has the correct type" do
expect(question.type).to eq("text")
end
it "is not marked as derived" do
expect(question.derived?).to be false
end
end

42
spec/models/form/sales/questions/property_postcode_spec.rb

@ -0,0 +1,42 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::PropertyPostcode, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { nil }
let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) }
it "has correct page" do
expect(question.page).to eq(page)
end
it "has the correct id" do
expect(question.id).to eq("postcode_known")
end
it "has the correct header" do
expect(question.header).to eq("Do you know the property's postcode?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Do you know the property's postcode?")
end
it "has the correct type" do
expect(question.type).to eq("radio")
end
it "has the correct answer_options" do
expect(question.answer_options).to eq({
"1" => { "value" => "Yes" },
"0" => { "value" => "No" },
})
end
it "has correct conditional for" do
expect(question.conditional_for).to eq({
"postcode_full" => [1],
})
end
end

29
spec/models/form/sales/subsections/property_postcode_spec.rb

@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe Form::Sales::Subsections::PropertyPostcode, type: :model do
subject(:property_information) { described_class.new(subsection_id, subsection_definition, section) }
let(:subsection_id) { nil }
let(:subsection_definition) { nil }
let(:section) { instance_double(Form::Sales::Sections::PropertyInformation) }
it "has correct section" do
expect(property_information.section).to eq(section)
end
it "has correct pages" do
expect(property_information.pages.map(&:id)).to eq(
%w[
postcode_known
],
)
end
it "has the correct id" do
expect(property_information.id).to eq("property_postcode")
end
it "has the correct label" do
expect(property_information.label).to eq("Property postcode")
end
end

2
spec/request_helper.rb

@ -14,6 +14,8 @@ module RequestHelper
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"NW1L 5DP\", \"outcode\": \"NW1L\", \"incode\": \"5DP\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/ZZ11ZZ")
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"ZZ1 1ZZ\", \"outcode\": \"ZZ1\", \"incode\": \"1ZZ\", \"admin_district\":\"Westminster\",\"codes\":{\"admin_district\":\"E09000033\"}}}", headers: {})
WebMock.stub_request(:get, "https://api.postcodes.io/postcodes/SW98LA")
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"postcode\":\"SW9 8LA\", \"outcode\": \"SW9\", \"incode\": \"8LA\", \"admin_district\":\"Lambeth\",\"codes\":{\"admin_district\":\"E09000022\"}}}", headers: {})
WebMock.stub_request(:post, /api.notifications.service.gov.uk\/v2\/notifications\/email/)
.to_return(status: 200, body: "", headers: {})

Loading…
Cancel
Save