Refine "add and edit disallow opening multiple forms" test

This commit is contained in:
cryptogopher 2024-12-10 18:11:13 +01:00
parent dc92a333be
commit bb4fbb3adc
3 changed files with 20 additions and 17 deletions

View File

@ -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

View File

@ -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
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
test "add and edit disallow opening multiple forms" do
# 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

View File

@ -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