diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1f87cfc..4acb234 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb index f59b3a8..f02f404 100644 --- a/app/views/units/_form.html.erb +++ b/app/views/units/_form.html.erb @@ -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 %> diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 9372b8e..2f70892 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -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 tag, so it's not possible to + # easily find them using e.g. :link selector + #Capybara.add_selector(:disabled_link) do + # label " tag with [disabled] attribute" + #end + test "click disabled link" do # Link should be unclickable # assert_raises(Selenium::WebDriver::Error::ElementClickInterceptedError) do diff --git a/test/system/units_test.rb b/test/system/units_test.rb index 0a92352..4c51a22 100644 --- a/test/system/units_test.rb +++ b/test/system/units_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 63bc8df..2bb8f4f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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