Test Unit form opening/closing

This commit is contained in:
cryptogopher 2024-02-13 01:04:27 +01:00
parent 10f866d40b
commit 8b81ae2dc5
3 changed files with 52 additions and 9 deletions

View File

@ -1,7 +1,7 @@
<tr id="<%= dom_id(unit) %>"> <tr id="<%= dom_id(unit) %>">
<td class="<%= class_names('link', {subunit: unit.base}) %>"> <td class="<%= class_names('link', {subunit: unit.base}) %>">
<%= link_to unit.symbol, edit_unit_path(unit), id: dom_id(unit, :edit), <%= link_to unit.symbol, edit_unit_path(unit), id: dom_id(unit, :edit),
data: {turbo_stream: true} %> onclick: 'this.blur();', data: {turbo_stream: true} %>
</td> </td>
<td><%= unit.name %></td> <td><%= unit.name %></td>
<td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td> <td class="number"><%= scientifize(unit.multiplier) if unit.multiplier %></td>

View File

@ -28,6 +28,6 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
end end
#def assert_stale(element) #def assert_stale(element)
# assert_raise(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name } # assert_raises(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name }
#end #end
end end

View File

@ -22,9 +22,8 @@ class UnitsTest < ApplicationSystemTestCase
test "add unit" do test "add unit" do
click_on t('units.index.add_unit') 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' assert_selector ':focus'
maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 1000] } maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 1000] }
fill_in 'unit[symbol]', fill_in 'unit[symbol]',
@ -36,19 +35,63 @@ class UnitsTest < ApplicationSystemTestCase
end end
end end
within('tbody') do within 'tbody' do
assert_no_selector :fillable_field assert_no_selector :fillable_field
assert_selector 'tr', count: @user.units.count assert_selector 'tr', count: @user.units.count
end end
assert_selector :link_or_button, text: t('units.index.add_unit') assert_selector '.flash.notice', exact_text: t('units.create.success')
assert_selector '.flash.notice', text: t('units.create.success')
end 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') click_on t('units.index.add_unit')
first('tbody > tr').all(:field).sample.send_keys :escape first('tbody > tr').all(:field).sample.send_keys :escape
within('tbody') do within 'tbody' do
assert_no_selector :fillable_field assert_no_selector :fillable_field
end end
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 end