Browse Source

CLDC-828: Total field should only be a progressive enhancement if JS is enabled (#199)

* Hide total field by default

* Total charge is inferred

* We don't need to reload if we're fetching from DB anyway

* Rubocop

* Add prefixes and suffixes

* Don't count read-only questions for section status

* Add feature test for progressive enhancement

* Progressively enhance the progressive enhancement spec
pull/202/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
305e794d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/helpers/conditional_questions_helper.rb
  2. 3
      app/models/case_log.rb
  3. 3
      app/models/form/question.rb
  4. 2
      app/models/form/subsection.rb
  5. 7
      app/webpacker/controllers/numeric_question_controller.js
  6. 13
      config/forms/2021_2022.json
  7. 35
      spec/features/form/progressive_total_field_spec.rb
  8. 3
      spec/fixtures/forms/2021_2022.json
  9. 45
      spec/models/case_log_spec.rb

2
app/helpers/conditional_questions_helper.rb

@ -4,6 +4,6 @@ module ConditionalQuestionsHelper
end
def display_question_key_div(page, question)
"style='display:none;'".html_safe if conditional_questions_for_page(page).include?(question.id)
"style='display:none;'".html_safe if conditional_questions_for_page(page).include?(question.id) || question.requires_js
end
end

3
app/models/case_log.rb

@ -132,7 +132,7 @@ class CaseLog < ApplicationRecord
enum la_known: POLAR, _suffix: true
enum net_income_known: NET_INCOME_KNOWN, _suffix: true
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild totelder totadult incfreq].freeze
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype is_la_inferred totchild totelder totadult incfreq tcharge].freeze
OPTIONAL_FIELDS = %w[postcode_known
la_known
first_time_property_let_as_social_housing].freeze
@ -245,6 +245,7 @@ private
self.totchild = get_totchild
self.totelder = get_totelder
self.totadult = get_totadult
self.tcharge = brent.to_i + scharge.to_i + pscharge.to_i + supcharg.to_i
end
def get_totelder

3
app/models/form/question.rb

@ -3,7 +3,7 @@ class Form::Question
:type, :min, :max, :step, :width, :fields_to_add, :result_field,
:conditional_for, :readonly, :answer_options, :page, :check_answer_label,
:inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value,
:guidance_partial, :prefix, :suffix
:guidance_partial, :prefix, :suffix, :requires_js
def initialize(id, hsh, page)
@id = id
@ -26,6 +26,7 @@ class Form::Question
@hidden_in_check_answers = hsh["hidden_in_check_answers"]
@prefix = hsh["prefix"]
@suffix = hsh["suffix"]
@requires_js = hsh["requires_js"]
@page = page
end

2
app/models/form/subsection.rb

@ -64,6 +64,6 @@ class Form::Subsection
end
def displayed_to_user?(case_log, question)
question.page.routed_to?(case_log) && question.enabled?(case_log)
question.page.routed_to?(case_log) && question.enabled?(case_log) && !question.read_only?
end
end

7
app/webpacker/controllers/numeric_question_controller.js

@ -1,6 +1,13 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
const affectedField = this.element.dataset.target;
const targetQuestion = affectedField.split("case-log-")[1].split("-field")[0]
const div = document.getElementById(targetQuestion + "_div");
div.style.display = "block";
}
calculateFields() {
const affectedField = this.element.dataset.target;
const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `case-log-${x.replaceAll("_","-")}-field`);

13
config/forms/2021_2022.json

@ -1979,6 +1979,8 @@
"type": "numeric",
"min": 0,
"step": 1,
"prefix": "£",
"suffix": "every week",
"fields-to-add": ["brent", "scharge", "pscharge", "supcharg"],
"result-field": "tcharge"
},
@ -1989,6 +1991,8 @@
"type": "numeric",
"min": 0,
"step": 1,
"prefix": "£",
"suffix": "every week",
"fields-to-add": ["brent", "scharge", "pscharge", "supcharg"],
"result-field": "tcharge"
},
@ -1999,6 +2003,8 @@
"type": "numeric",
"min": 0,
"step": 1,
"prefix": "£",
"suffix": "every week",
"fields-to-add": ["brent", "scharge", "pscharge", "supcharg"],
"result-field": "tcharge"
},
@ -2009,6 +2015,8 @@
"type": "numeric",
"min": 0,
"step": 1,
"prefix": "£",
"suffix": "every week",
"fields-to-add": ["brent", "scharge", "pscharge", "supcharg"],
"result-field": "tcharge"
},
@ -2019,7 +2027,10 @@
"type": "numeric",
"min": 0,
"step": 1,
"readonly": true
"prefix": "£",
"suffix": "every week",
"readonly": true,
"requires_js": true
},
"hbrentshortfall": {
"check_answer_label": "After housing benefit and/or housing element of UC payment is received, will there be an outstanding amount for basic rent and/or benefit eligible charges?",

35
spec/features/form/progressive_total_field_spec.rb

@ -0,0 +1,35 @@
require "rails_helper"
require_relative "helpers"
require_relative "../../request_helper"
RSpec.describe "Accessible Automcomplete" do
include Helpers
let(:user) { FactoryBot.create(:user) }
let(:case_log) do
FactoryBot.create(
:case_log,
:in_progress,
owning_organisation: user.organisation,
managing_organisation: user.organisation,
)
end
before do
RequestHelper.stub_http_requests
sign_in user
end
it "does not show when js is not enabled" do
visit("/logs/#{case_log.id}/rent")
expect(page).to have_selector("#tcharge_div", visible: false)
end
it "does show when js is enabled and calculates the total", js: true do
visit("/logs/#{case_log.id}/rent")
expect(page).to have_selector("#tcharge_div", visible: true)
fill_in("case-log-brent-field", with: 5)
expect(find("#case-log-tcharge-field").value).to eq("5")
fill_in("case-log-pscharge-field", with: 3)
expect(find("#case-log-tcharge-field").value).to eq("8")
end
end

3
spec/fixtures/forms/2021_2022.json vendored

@ -537,7 +537,8 @@
"min": 0,
"step": 1,
"width": 4,
"readonly": true
"readonly": true,
"requires_js": true
}
}
}

45
spec/models/case_log_spec.rb

@ -1040,24 +1040,18 @@ RSpec.describe Form, type: :model do
end
it "correctly derives and saves partial and full postcodes" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select postcode, postcod2 from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["postcode"]).to eq("M1")
expect(record_from_db["postcod2"]).to eq("1AE")
end
it "correctly derives and saves partial and full previous postcodes" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select ppostc1, ppostc2 from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["ppostc1"]).to eq("M2")
expect(record_from_db["ppostc2"]).to eq("2AE")
end
it "correctly derives and saves partial and full major repairs date" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select mrcday, mrcmonth, mrcyear, mrcdate from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["mrcdate"].day).to eq(4)
expect(record_from_db["mrcdate"].month).to eq(5)
@ -1068,38 +1062,28 @@ RSpec.describe Form, type: :model do
end
it "correctly derives and saves incref" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select incref from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["incref"]).to eq(1)
end
it "correctly derives and saves hhmemb" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["hhmemb"]).to eq(7)
end
it "correctly derives and saves renttype" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select renttype from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.renttype).to eq("Intermediate Rent")
expect(record_from_db["renttype"]).to eq(3)
end
it "correctly derives and saves lettype" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.lettype).to eq("Intermediate Rent General needs PRP")
expect(record_from_db["lettype"]).to eq(9)
end
it "correctly derives and saves day, month, year from start date" do
case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select day, month, year, startdate from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["startdate"].day).to eq(10)
expect(record_from_db["startdate"].month).to eq(10)
@ -1125,8 +1109,6 @@ RSpec.describe Form, type: :model do
end
it "correctly infers la" do
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la from case_logs where id=#{address_case_log.id}").to_a[0]
expect(address_case_log.la).to eq("Manchester")
expect(record_from_db["la"]).to eq("E08000003")
@ -1139,7 +1121,6 @@ RSpec.describe Form, type: :model do
it "correctly resets all fields if property postcode not known" do
address_case_log.update!({ postcode_known: "No" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq(nil)
@ -1150,7 +1131,6 @@ RSpec.describe Form, type: :model do
it "changes the LA if property postcode changes from not known to known and provided" do
address_case_log.update!({ postcode_known: "No" })
address_case_log.update!({ la: "Westminster" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq(nil)
@ -1158,7 +1138,6 @@ RSpec.describe Form, type: :model do
expect(record_from_db["la"]).to eq("E09000033")
address_case_log.update!({ postcode_known: "Yes", property_postcode: "M1 1AD" })
address_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select la, property_postcode from case_logs where id=#{address_case_log.id}").to_a[0]
expect(record_from_db["property_postcode"]).to eq("M1 1AD")
@ -1178,6 +1157,24 @@ RSpec.describe Form, type: :model do
end
end
context "rent and charges" do
let!(:case_log) do
CaseLog.create({
managing_organisation: organisation,
owning_organisation: organisation,
brent: 5,
scharge: 10,
pscharge: 3,
supcharg: 12,
})
end
it "correctly sums rental charges" do
record_from_db = ActiveRecord::Base.connection.execute("select tcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["tcharge"]).to eq(30)
end
end
context "household members derived vars" do
let!(:household_case_log) do
CaseLog.create({
@ -1200,22 +1197,16 @@ RSpec.describe Form, type: :model do
end
it "correctly derives and saves totchild" do
household_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select totchild from case_logs where id=#{household_case_log.id}").to_a[0]
expect(record_from_db["totchild"]).to eq(3)
end
it "correctly derives and saves totelder" do
household_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select totelder from case_logs where id=#{household_case_log.id}").to_a[0]
expect(record_from_db["totelder"]).to eq(2)
end
it "correctly derives and saves totadult" do
household_case_log.reload
record_from_db = ActiveRecord::Base.connection.execute("select totadult from case_logs where id=#{household_case_log.id}").to_a[0]
expect(record_from_db["totadult"]).to eq(3)
end

Loading…
Cancel
Save