Browse Source

Clear answers if conditional is unselected

pull/29/head
baarkerlounger 4 years ago
parent
commit
ad0da83136
  1. 9
      app/javascript/controllers/conditional_question_controller.js
  2. 9
      spec/features/case_log_spec.rb
  3. 2
      spec/rails_helper.rb

9
app/javascript/controllers/conditional_question_controller.js

@ -7,10 +7,15 @@ export default class extends Controller {
let conditional_for = JSON.parse(this.element.dataset.info); let conditional_for = JSON.parse(this.element.dataset.info);
Object.entries(conditional_for).forEach(([key, values]) => { Object.entries(conditional_for).forEach(([key, values]) => {
let el = document.getElementById(key + "_div");
if(values.includes(selected)) { if(values.includes(selected)) {
document.getElementById(key + "_div").style.display = "block" el.style.display = "block";
} else { } else {
document.getElementById(key + "_div").style.display = "none" el.style.display = "none";
let buttons = document.getElementsByName(key)
Object.entries(buttons).forEach(([idx, button]) => {
button.checked = false;
});
} }
}); });
} }

9
spec/features/case_log_spec.rb

@ -228,10 +228,15 @@ RSpec.describe "Test Features" do
it "shows conditional questions if the required answer is selected and hides it again when a different answer option is selected", js: true do it "shows conditional questions if the required answer is selected and hides it again when a different answer option is selected", js: true do
visit("/case_logs/#{id}/armed_forces") visit("/case_logs/#{id}/armed_forces")
find(:xpath, "//label[@for='armed-forces-0-field']").click # Someting about our styling makes the selenium webdriver think the actual radio buttons are not visible so we allow label click here
choose("armed-forces-0-field", allow_label_click: true)
expect(page).to have_selector("#armed_forces_injured_div") expect(page).to have_selector("#armed_forces_injured_div")
find(:xpath, "//label[@for='armed-forces-2-field']").click choose("armed-forces-injured-1-field", allow_label_click: true)
expect(find_field("armed-forces-injured-1-field", visible: false).checked?).to be_truthy
choose("armed-forces-2-field", allow_label_click: true)
expect(page).not_to have_selector("#armed_forces_injured_div") expect(page).not_to have_selector("#armed_forces_injured_div")
choose("armed-forces-0-field", allow_label_click: true)
expect(find_field("armed-forces-injured-1-field", visible: false).checked?).to be_falsey
end end
end end
end end

2
spec/rails_helper.rb

@ -7,7 +7,7 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ
require "rspec/rails" require "rspec/rails"
require "capybara/rspec" require "capybara/rspec"
# Uncomment to run `js: true specs` with visible browser interaction # Comment to run `js: true specs` with visible browser interaction
Capybara.javascript_driver = :selenium_headless Capybara.javascript_driver = :selenium_headless
# Add additional requires below this line. Rails is not loaded until this point! # Add additional requires below this line. Rails is not loaded until this point!

Loading…
Cancel
Save