Fill multiplier field, confirm Add button disabled

This commit is contained in:
cryptogopher 2024-12-12 00:44:26 +01:00
parent 3a25c1dbd0
commit d5719b1e9d
5 changed files with 39 additions and 7 deletions

View File

@ -132,4 +132,10 @@ module ApplicationHelper
def disabled_attributes(disabled)
disabled ? {disabled: true, aria: {disabled: true}, tabindex: -1} : {}
end
def number_attributes(type)
step = BigDecimal(10).power(-type.scale)
max = BigDecimal(10).power(type.precision - type.scale) - step
{min: -max, max: max, step: step}
end
end

View File

@ -14,8 +14,8 @@
<% unless @unit.base.nil? %>
<%= form.hidden_field :base_id, form: :unit_form %>
<%= form.number_field :multiplier, form: :unit_form, required: true,
step: BigDecimal(10).power(-@unit.class.type_for_attribute(:multiplier).scale),
size: 10, autocomplete: "off" %>
size: 10, autocomplete: "off",
**number_attributes(@unit.class.type_for_attribute(:multiplier)) %>
<% end %>
</td>

View File

@ -34,6 +34,12 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# assert_raises(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name }
#end
# HTML does not allow [disabled] attribute on <a> tag, so it's not possible to
# easily find them using e.g. :link selector
#Capybara.add_selector(:disabled_link) do
# label "<a> tag with [disabled] attribute"
#end
test "click disabled link" do
# Link should be unclickable
# assert_raises(Selenium::WebDriver::Error::ElementClickInterceptedError) do

View File

@ -23,18 +23,24 @@ class UnitsTest < ApplicationSystemTestCase
end
end
# TODO: check if Add buton is properly disabled/enabled
# TODO: extend with add subunit
test "add unit" do
click_on t('units.index.add_unit')
add_link = all(:link, exact_text: ADD_UNIT_LABELS.sample).sample
add_link.click
assert_equal 'disabled', add_link[:disabled]
within 'tbody > tr:has(input[type=text], textarea)' do
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 || 2**16] }
fill_in 'unit[symbol]',
with: SecureRandom.random_symbol(rand([1..15, 15..maxlength['unit[symbol]']].sample))
fill_in 'unit[description]',
with: [nil, SecureRandom.alphanumeric(rand(1..maxlength['unit[description]']))].sample
within :field, 'unit[multiplier]' do |field|
fill_in with: random_number(field[:max], field[:step])
end if add_link[:text] != t('units.index.add_unit')
assert_difference ->{ Unit.count }, 1 do
click_on t('helpers.submit.create')
end
@ -44,7 +50,9 @@ class UnitsTest < ApplicationSystemTestCase
assert_no_selector :fillable_field
assert_selector 'tr', count: @user.units.count
end
assert_selector '.flash.notice', text: /^#{t('units.create.success', unit: @user.units.last)}/
assert_no_selector :element, :a, 'disabled': 'disabled',
exact_text: Regexp.union(ADD_UNIT_LABELS)
assert_selector '.flash.notice', text: t('units.create.success', unit: @user.units.last.symbol)
end
# TODO: check proper form/button redisplay and flash messages

View File

@ -32,6 +32,18 @@ class ActiveSupport::TestCase
"%s@%s.%s" % (1..3).map { SecureRandom.alphanumeric(rand(1..20)) }
end
# Assumes: max >= step and step = 1e[-]N, both as strings
def random_number(max, step)
max.delete!('.')
precision = max.length
start = rand(precision) + 1
d = (rand(max.to_i) + 1) % 10**start
length = rand([0, 1..4, 4..precision].sample)
d = d.truncate(-start + length)
d = 10**(start - length) if d.zero?
BigDecimal(step) * d
end
def with_last_email
yield(ActionMailer::Base.deliveries.last)
end