From bb4fbb3adc342fbb6aaaaa81e1f9085143a7cb28 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 10 Dec 2024 18:11:13 +0100 Subject: [PATCH] Refine "add and edit disallow opening multiple forms" test --- test/application_system_test_case.rb | 9 +++++++++ test/system/units_test.rb | 26 ++++++++++---------------- test/test_helper.rb | 2 +- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index e641868..9372b8e 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -1,6 +1,7 @@ require "test_helper" class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + extend ActionView::Helpers::TranslationHelper include ActionView::Helpers::UrlHelper # NOTE: geckodriver installed with Firefox, ignore incompatibility warning @@ -32,4 +33,12 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase #def assert_stale(element) # assert_raises(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name } #end + + test "click disabled link" do + # Link should be unclickable + # assert_raises(Selenium::WebDriver::Error::ElementClickInterceptedError) do + # # Use custom selector for disabled links + # find('a[disabled]').click + # end + end end diff --git a/test/system/units_test.rb b/test/system/units_test.rb index 745a20e..dbf6025 100644 --- a/test/system/units_test.rb +++ b/test/system/units_test.rb @@ -1,6 +1,8 @@ require "application_system_test_case" class UnitsTest < ApplicationSystemTestCase + ADD_UNIT_LABELS = [t('units.index.add_unit'), t('units.unit.add_subunit')] + setup do @user = sign_in visit units_path @@ -49,36 +51,28 @@ class UnitsTest < ApplicationSystemTestCase test "add unit on validation error" do end + # TODO: add non-empty form closing warning test "add and edit disallow opening multiple forms" do - # Once new/edit form is open, other actions on the same page either replace - # the form or leave it untouched - # TODO: add non-empty form closing warning + # Once new/edit form is open, attempt to open another one will close it links = {} - link_labels = {1 => [t('units.index.add_unit'), t('units.unit.add_subunit')], - 0 => units.map(&:symbol)} + # Define tr count change depending on link clicked + link_labels = {1 => ADD_UNIT_LABELS, 0 => units.map(&:symbol)} link_labels.each_pair do |row_change, labels| all(:link_or_button, exact_text: Regexp.union(labels)).map { |l| links[l] = row_change } end + link, rows = links.assoc(links.keys.sample).tap { |l, _| links.delete(l) } assert_difference ->{ all('tbody tr').count }, rows do link.click end - find 'tbody tr:has(input[type=text]:focus)' - - # Link should be now unavailable or unclickable - begin - assert_raises(Selenium::WebDriver::Error::ElementClickInterceptedError) do - link.click - end if link.visible? - rescue Selenium::WebDriver::Error::StaleElementReferenceError - link = nil - end + find('tbody tr:has(input[type=text])').assert_selector ':focus' + assert !link.visible? || link[:disabled] link = links.keys.select(&:visible?).sample assert_difference ->{ all('tbody tr').count }, links[link] - rows do link.click end - assert_selector 'tbody tr:has(input[type=text]:focus)', count: 1 + find('tbody tr:has(input[type=text])').assert_selector ':focus' end # NOTE: extend with any add/edit link diff --git a/test/test_helper.rb b/test/test_helper.rb index 1e7f8dc..63bc8df 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,8 +9,8 @@ class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - include AbstractController::Translation include ActionMailer::TestHelper + include ActionView::Helpers::TranslationHelper # NOTE: use public #alphanumeric(chars: ...) from Ruby 3.3 onwards SecureRandom.class_eval do