From 32b8daf29d6c1c78dc92c9955a849f439443d982 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 5 Jan 2024 04:16:38 +0100 Subject: [PATCH] Add Unit system tests --- test/system/units_test.rb | 35 +++++++++++++++++++++++++++++++++++ test/test_helper.rb | 8 ++++++++ 2 files changed, 43 insertions(+) create mode 100644 test/system/units_test.rb diff --git a/test/system/units_test.rb b/test/system/units_test.rb new file mode 100644 index 0000000..5f2369f --- /dev/null +++ b/test/system/units_test.rb @@ -0,0 +1,35 @@ +require "application_system_test_case" + +class UnitsTest < ApplicationSystemTestCase + setup do + @admin = users(:admin) + sign_in + visit units_path + end + + test "index" do + assert_selector 'tbody > tr', count: Unit.count + assert_current_path units_path + end + + 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 + fill_in 'unit[symbol]', with: SecureRandom.random_symbol(rand(1..10)) + fill_in 'unit[name]', with: [nil, SecureRandom.alphanumeric(rand(1..500))].sample + assert_difference ->{ Unit.count }, 1 do + click_on t(:add) + end + end + + within('tbody') do + assert_no_selector :fillable_field + assert_selector 'tr', count: Unit.count + end + assert_selector :link_or_button, text: t('units.index.add_unit') + + # assert_selector flash + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index dfeff23..87a0161 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,14 @@ class ActiveSupport::TestCase include AbstractController::Translation include ActionMailer::TestHelper + # NOTE: use public #alphanumeric(chars: ...) from Ruby 3.3 onwards + SecureRandom.class_eval do + def self.random_symbol(n = 10) + # Unicode characters: 32-126, 160-383 + choose([*' '..'~', 160.chr, *'¡'..'ſ'], n) + end + end + def randomize_user_password!(user) random_password.tap { |p| user.update!(password: p) } end