From 5d23bf0584e40baf86197741c382c4c9bfe04730 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 4 Oct 2021 09:56:44 +0100 Subject: [PATCH] Spec show and hide conditional behaviour --- Gemfile | 2 +- Gemfile.lock | 7 +++++-- spec/features/case_log_spec.rb | 10 +++++++++- spec/rails_helper.rb | 8 ++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 492278eab..f28f6dee0 100644 --- a/Gemfile +++ b/Gemfile @@ -29,7 +29,7 @@ group :development, :test do gem "dotenv-rails" gem "factory_bot_rails" gem "pry-byebug" - # gem "selenium-webdriver", require: false + gem "selenium-webdriver", require: false gem "simplecov", require: false %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib| gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false diff --git a/Gemfile.lock b/Gemfile.lock index d09c26ade..11eff1ccd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -175,8 +175,6 @@ GEM minitest (5.14.4) msgpack (1.4.2) nio4r (2.5.8) - nokogiri (1.12.4-x86_64-darwin) - racc (~> 1.4) nokogiri (1.12.4-x86_64-linux) racc (~> 1.4) overcommit (0.58.0) @@ -267,6 +265,7 @@ GEM rubocop (~> 1.0) rubocop-ast (>= 1.1.0) ruby-progressbar (1.11.0) + rubyzip (2.3.2) sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -276,6 +275,9 @@ GEM sass (~> 3.5, >= 3.5.5) scss_lint-govuk (0.2.0) scss_lint + selenium-webdriver (3.142.7) + childprocess (>= 0.5, < 4.0) + rubyzip (>= 1.2.2) semantic_range (3.0.0) simplecov (0.21.2) docile (~> 1.1) @@ -349,6 +351,7 @@ DEPENDENCIES rubocop-performance rubocop-rails scss_lint-govuk + selenium-webdriver simplecov tzinfo-data web-console (>= 4.1.0) diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 5f11ff4e1..4b14680da 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -223,7 +223,15 @@ RSpec.describe "Test Features" do context "given a page where some questions are only conditionally shown, depending on how you answer the first question" do it "initially hides conditional questions" do visit("/case_logs/#{id}/armed_forces") - expect(page).to have_selector("#armed_forces_injured_div", visible: :hidden) + expect(page).not_to have_selector("#armed_forces_injured_div") + end + + 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") + find(:xpath, "//label[@for='armed-forces-0-field']").click + expect(page).to have_selector("#armed_forces_injured_div") + find(:xpath, "//label[@for='armed-forces-2-field']").click + expect(page).not_to have_selector("#armed_forces_injured_div") end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 73435201b..afda6c5df 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,6 +6,10 @@ require File.expand_path("../config/environment", __dir__) abort("The Rails environment is running in production mode!") if Rails.env.production? require "rspec/rails" require "capybara/rspec" + +# Uncomment to run `js: true specs` with visible browser interaction +Capybara.javascript_driver = :selenium_headless + # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in @@ -62,4 +66,8 @@ RSpec.configure do |config| config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") + + # Silence capybara logging puma start up messages to stdout on first js: true + # spec + Capybara.server = :puma, { Silent: true } end