From 7cfa3a0af0132941ae98a35f77a7d0aa49693fd0 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Wed, 17 Jan 2024 04:25:32 +0100 Subject: [PATCH] Refine Unit tests --- test/application_system_test_case.rb | 1 + test/fixtures/units.yml | 40 ++++++++++++++++++++++++++++ test/system/units_test.rb | 23 +++++++++++----- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/units.yml diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 0e0c578..1031e7c 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -19,6 +19,7 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase fill_in User.human_attribute_name(:email).capitalize, with: user.email fill_in User.human_attribute_name(:password).capitalize, with: password click_on t(:sign_in) + user end def inject_button_to(after, *button_options) diff --git a/test/fixtures/units.yml b/test/fixtures/units.yml new file mode 100644 index 0000000..3a5fad3 --- /dev/null +++ b/test/fixtures/units.yml @@ -0,0 +1,40 @@ +g: + user: admin + symbol: g + name: gram +kg: + user: admin + symbol: kg + name: kilogram + multiplier: 1000 + base: g +1: + user: admin + symbol: 1 + name: one +s: + user: admin + symbol: s + name: second +percent: + user: admin + symbol: '%' + name: percent + multiplier: 0.01 + base: 1 +µg: + user: admin + symbol: µg + name: microgram + multiplier: 0.000001 + base: g +mg: + user: admin + symbol: mg + name: milligram + multiplier: 0.001 + base: g +g: + user: alice + symbol: g + name: gram diff --git a/test/system/units_test.rb b/test/system/units_test.rb index 99cc044..85ccd80 100644 --- a/test/system/units_test.rb +++ b/test/system/units_test.rb @@ -2,14 +2,22 @@ require "application_system_test_case" class UnitsTest < ApplicationSystemTestCase setup do - @admin = users(:admin) - sign_in + @user = sign_in visit units_path end test "index" do - assert_selector 'tbody > tr', count: Unit.count - assert_current_path units_path + # Wait for the table to appear first, only then check row count + within 'tbody' do + assert_selector 'tr', count: @user.units.count + end + + Unit.destroy_all + visit units_path + within 'tbody' do + assert_selector 'tr', count: 1 + assert_text t('units.index.no_items') + end end test "add unit" do @@ -28,9 +36,10 @@ class UnitsTest < ApplicationSystemTestCase end end + assert_current_path units_path within('tbody') do assert_no_selector :fillable_field - assert_selector 'tr', count: Unit.count + assert_selector 'tr', count: @user.units.count end assert_selector :link_or_button, text: t('units.index.add_unit') @@ -40,6 +49,8 @@ class UnitsTest < ApplicationSystemTestCase test "close new unit form with escape" do click_on t('units.index.add_unit') first('tbody > tr').all(:field).sample.send_keys :escape - within('tbody') { assert_no_selector :fillable_field } + within('tbody') do + assert_no_selector :fillable_field + end end end