From 8b81ae2dc580a27c2745b4b8f9f06742c8802d66 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 13 Feb 2024 01:04:27 +0100 Subject: [PATCH] Test Unit form opening/closing --- app/views/units/_unit.html.erb | 2 +- test/application_system_test_case.rb | 2 +- test/system/units_test.rb | 57 ++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/views/units/_unit.html.erb b/app/views/units/_unit.html.erb index b787939..008fa4f 100644 --- a/app/views/units/_unit.html.erb +++ b/app/views/units/_unit.html.erb @@ -1,7 +1,7 @@ <%= link_to unit.symbol, edit_unit_path(unit), id: dom_id(unit, :edit), - data: {turbo_stream: true} %> + onclick: 'this.blur();', data: {turbo_stream: true} %> <%= unit.name %> <%= scientifize(unit.multiplier) if unit.multiplier %> diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 1031e7c..fab452d 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -28,6 +28,6 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase end #def assert_stale(element) - # assert_raise(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name } + # assert_raises(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name } #end end diff --git a/test/system/units_test.rb b/test/system/units_test.rb index 213a694..5115d0d 100644 --- a/test/system/units_test.rb +++ b/test/system/units_test.rb @@ -22,9 +22,8 @@ class UnitsTest < ApplicationSystemTestCase test "add unit" do click_on t('units.index.add_unit') - assert_no_selector :link_or_button, text: t('units.index.add_unit') - within first('tbody > tr') do + within 'tbody > tr:has(input, textarea)' do assert_selector ':focus' maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 1000] } fill_in 'unit[symbol]', @@ -36,19 +35,63 @@ class UnitsTest < ApplicationSystemTestCase end end - within('tbody') do + within 'tbody' do assert_no_selector :fillable_field assert_selector 'tr', count: @user.units.count end - assert_selector :link_or_button, text: t('units.index.add_unit') - assert_selector '.flash.notice', text: t('units.create.success') + assert_selector '.flash.notice', exact_text: t('units.create.success') end - test "close new unit form with escape" do + 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 + links = {} + link_labels = {1 => [t('units.index.add_unit'), t('units.unit.add_subunit')], + 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) } + puts link[:text] + 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 + + link = links.keys.select(&:visible?).sample + assert_difference ->{ all('tbody tr').count }, links[link] - rows do + link.tap{|l| puts l[:text]}.click + end + assert_selector 'tbody tr:has(input[type=text]:focus)', count: 1 + end + + # NOTE: extend with any add/edit link + test "close new unit form with escape key" do click_on t('units.index.add_unit') first('tbody > tr').all(:field).sample.send_keys :escape - within('tbody') do + within 'tbody' do assert_no_selector :fillable_field end end + + # NOTE: extend with any add/edit link + test "close and reopen new unit form" do + click_on t('units.index.add_unit') + within 'tbody' do + find(:link_or_button, exact_text: t(:cancel)).click + assert_no_selector :fillable_field + end + click_on t('units.index.add_unit') + assert_selector 'tbody > tr:has(input, textarea)' + end end